2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "JavaClassJSC.h"
29 #if ENABLE(JAVA_BRIDGE)
31 #include "JSDOMWindow.h"
32 #include "JavaFieldJSC.h"
33 #include "JavaMethodJSC.h"
34 #include <runtime/Identifier.h>
35 #include <runtime/JSLock.h>
37 using namespace JSC::Bindings;
39 JavaClass::JavaClass(jobject anInstance)
41 jobject aClass = callJNIMethod<jobject>(anInstance, "getClass", "()Ljava/lang/Class;");
44 LOG_ERROR("Unable to call getClass on instance %p", anInstance);
45 m_name = fastStrDup("<Unknown>");
49 if (jstring className = (jstring)callJNIMethod<jobject>(aClass, "getName", "()Ljava/lang/String;")) {
50 const char* classNameC = getCharactersFromJString(className);
51 m_name = fastStrDup(classNameC);
52 releaseCharactersForJString(className, classNameC);
54 m_name = fastStrDup("<Unknown>");
57 JNIEnv* env = getJNIEnv();
60 if (jarray fields = (jarray)callJNIMethod<jobject>(aClass, "getFields", "()[Ljava/lang/reflect/Field;")) {
61 int numFields = env->GetArrayLength(fields);
62 for (i = 0; i < numFields; i++) {
63 jobject aJField = env->GetObjectArrayElement((jobjectArray)fields, i);
64 JavaField* aField = new JavaField(env, aJField); // deleted in the JavaClass destructor
66 JSLock lock(SilenceAssertionsOnly);
67 m_fields.set(aField->name().impl(), aField);
69 env->DeleteLocalRef(aJField);
71 env->DeleteLocalRef(fields);
75 if (jarray methods = (jarray)callJNIMethod<jobject>(aClass, "getMethods", "()[Ljava/lang/reflect/Method;")) {
76 int numMethods = env->GetArrayLength(methods);
77 for (i = 0; i < numMethods; i++) {
78 jobject aJMethod = env->GetObjectArrayElement((jobjectArray)methods, i);
79 JavaMethod* aMethod = new JavaMethod(env, aJMethod); // deleted in the JavaClass destructor
80 MethodList* methodList;
82 JSLock lock(SilenceAssertionsOnly);
84 methodList = m_methods.get(aMethod->name().impl());
86 methodList = new MethodList();
87 m_methods.set(aMethod->name().impl(), methodList);
90 methodList->append(aMethod);
91 env->DeleteLocalRef(aJMethod);
93 env->DeleteLocalRef(methods);
96 env->DeleteLocalRef(aClass);
99 JavaClass::~JavaClass()
101 fastFree(const_cast<char*>(m_name));
103 JSLock lock(SilenceAssertionsOnly);
105 deleteAllValues(m_fields);
108 MethodListMap::const_iterator end = m_methods.end();
109 for (MethodListMap::const_iterator it = m_methods.begin(); it != end; ++it) {
110 const MethodList* methodList = it->second;
111 deleteAllValues(*methodList);
117 MethodList JavaClass::methodsNamed(PropertyName identifier, Instance*) const
119 MethodList* methodList = m_methods.get(identifier.ustring().impl());
126 Field* JavaClass::fieldNamed(PropertyName identifier, Instance*) const
128 return m_fields.get(identifier.ustring().impl());
131 bool JavaClass::isNumberClass() const
133 return (!strcmp(m_name, "java.lang.Byte")
134 || !strcmp(m_name, "java.lang.Short")
135 || !strcmp(m_name, "java.lang.Integer")
136 || !strcmp(m_name, "java.lang.Long")
137 || !strcmp(m_name, "java.lang.Float")
138 || !strcmp(m_name, "java.lang.Double"));
141 bool JavaClass::isBooleanClass() const
143 return !strcmp(m_name, "java.lang.Boolean");
146 bool JavaClass::isStringClass() const
148 return !strcmp(m_name, "java.lang.String");
151 #endif // ENABLE(JAVA_BRIDGE)