]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/jni/jni_class.cpp
JavaScriptCore-466.1.tar.gz
[apple/javascriptcore.git] / bindings / jni / jni_class.cpp
1 /*
2 * Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27
28 #if ENABLE(JAVA_BINDINGS)
29
30 #include <jni_class.h>
31
32 #include "identifier.h"
33 #include <jni_utility.h>
34 #include <jni_runtime.h>
35
36 using namespace KJS::Bindings;
37
38 JavaClass::JavaClass(jobject anInstance)
39 {
40 jobject aClass = callJNIObjectMethod(anInstance, "getClass", "()Ljava/lang/Class;");
41
42 if (!aClass) {
43 fprintf(stderr, "%s: unable to call getClass on instance %p\n", __PRETTY_FUNCTION__, anInstance);
44 return;
45 }
46
47 jstring className = (jstring)callJNIObjectMethod(aClass, "getName", "()Ljava/lang/String;");
48 const char *classNameC = getCharactersFromJString(className);
49 _name = strdup(classNameC);
50 releaseCharactersForJString(className, classNameC);
51
52 int i;
53 JNIEnv *env = getJNIEnv();
54
55 // Get the fields
56 jarray fields = (jarray)callJNIObjectMethod(aClass, "getFields", "()[Ljava/lang/reflect/Field;");
57 int numFields = env->GetArrayLength(fields);
58 for (i = 0; i < numFields; i++) {
59 jobject aJField = env->GetObjectArrayElement((jobjectArray)fields, i);
60 Field *aField = new JavaField(env, aJField); // deleted in the JavaClass destructor
61 {
62 JSLock lock;
63 _fields.set(Identifier(aField->name()).ustring().rep(), aField);
64 }
65 env->DeleteLocalRef(aJField);
66 }
67
68 // Get the methods
69 jarray methods = (jarray)callJNIObjectMethod(aClass, "getMethods", "()[Ljava/lang/reflect/Method;");
70 int numMethods = env->GetArrayLength(methods);
71 for (i = 0; i < numMethods; i++) {
72 jobject aJMethod = env->GetObjectArrayElement((jobjectArray)methods, i);
73 Method *aMethod = new JavaMethod(env, aJMethod); // deleted in the JavaClass destructor
74 MethodList* methodList;
75 {
76 JSLock lock;
77
78 methodList = _methods.get(Identifier(aMethod->name()).ustring().rep());
79 if (!methodList) {
80 methodList = new MethodList();
81 _methods.set(Identifier(aMethod->name()).ustring().rep(), methodList);
82 }
83 }
84 methodList->append(aMethod);
85 env->DeleteLocalRef(aJMethod);
86 }
87 }
88
89 JavaClass::~JavaClass() {
90 free((void *)_name);
91
92 JSLock lock;
93
94 deleteAllValues(_fields);
95 _fields.clear();
96
97 MethodListMap::const_iterator end = _methods.end();
98 for (MethodListMap::const_iterator it = _methods.begin(); it != end; ++it) {
99 const MethodList* methodList = it->second;
100 deleteAllValues(*methodList);
101 delete methodList;
102 }
103 _methods.clear();
104 }
105
106 MethodList JavaClass::methodsNamed(const Identifier& identifier, Instance*) const
107 {
108 MethodList *methodList = _methods.get(identifier.ustring().rep());
109
110 if (methodList)
111 return *methodList;
112 return MethodList();
113 }
114
115 Field *JavaClass::fieldNamed(const Identifier& identifier, Instance*) const
116 {
117 return _fields.get(identifier.ustring().rep());
118 }
119
120 bool JavaClass::isNumberClass() const
121 {
122 return ((strcmp(_name, "java.lang.Byte") == 0 ||
123 strcmp(_name, "java.lang.Short") == 0 ||
124 strcmp(_name, "java.lang.Integer") == 0 ||
125 strcmp(_name, "java.lang.Long") == 0 ||
126 strcmp(_name, "java.lang.Float") == 0 ||
127 strcmp(_name, "java.lang.Double") == 0) );
128 }
129
130 bool JavaClass::isBooleanClass() const
131 {
132 return strcmp(_name, "java.lang.Boolean") == 0;
133 }
134
135 bool JavaClass::isStringClass() const
136 {
137 return strcmp(_name, "java.lang.String") == 0;
138 }
139
140 #endif // ENABLE(JAVA_BINDINGS)