]>
git.saurik.com Git - apple/javascriptcore.git/blob - bindings/jni/jni_class.cpp
2 * Copyright (C) 2003 Apple Computer, 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.
28 #if ENABLE(JAVA_BINDINGS)
30 #include <jni_class.h>
32 #include "identifier.h"
33 #include <jni_utility.h>
34 #include <jni_runtime.h>
36 using namespace KJS::Bindings
;
38 JavaClass::JavaClass(jobject anInstance
)
40 jobject aClass
= callJNIObjectMethod(anInstance
, "getClass", "()Ljava/lang/Class;");
43 fprintf(stderr
, "%s: unable to call getClass on instance %p\n", __PRETTY_FUNCTION__
, anInstance
);
47 jstring className
= (jstring
)callJNIObjectMethod(aClass
, "getName", "()Ljava/lang/String;");
48 const char *classNameC
= getCharactersFromJString(className
);
49 _name
= strdup(classNameC
);
50 releaseCharactersForJString(className
, classNameC
);
53 JNIEnv
*env
= getJNIEnv();
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
63 _fields
.set(Identifier(aField
->name()).ustring().rep(), aField
);
65 env
->DeleteLocalRef(aJField
);
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
;
78 methodList
= _methods
.get(Identifier(aMethod
->name()).ustring().rep());
80 methodList
= new MethodList();
81 _methods
.set(Identifier(aMethod
->name()).ustring().rep(), methodList
);
84 methodList
->append(aMethod
);
85 env
->DeleteLocalRef(aJMethod
);
89 JavaClass::~JavaClass() {
94 deleteAllValues(_fields
);
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
);
106 MethodList
JavaClass::methodsNamed(const Identifier
& identifier
, Instance
*) const
108 MethodList
*methodList
= _methods
.get(identifier
.ustring().rep());
115 Field
*JavaClass::fieldNamed(const Identifier
& identifier
, Instance
*) const
117 return _fields
.get(identifier
.ustring().rep());
120 bool JavaClass::isNumberClass() const
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) );
130 bool JavaClass::isBooleanClass() const
132 return strcmp(_name
, "java.lang.Boolean") == 0;
135 bool JavaClass::isStringClass() const
137 return strcmp(_name
, "java.lang.String") == 0;
140 #endif // ENABLE(JAVA_BINDINGS)