2 * Copyright (C) 2003, 2004, 2005, 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.
29 #if ENABLE(JAVA_BRIDGE)
34 #include <JavaVM/jni.h>
43 const char* getCharactersFromJString(jstring);
44 void releaseCharactersForJString(jstring, const char*);
46 const char* getCharactersFromJStringInEnv(JNIEnv*, jstring);
47 void releaseCharactersForJStringInEnv(JNIEnv*, jstring, const char*);
48 const jchar* getUCharactersFromJStringInEnv(JNIEnv*, jstring);
49 void releaseUCharactersForJStringInEnv(JNIEnv*, jstring, const jchar*);
51 JavaType javaTypeFromClassName(const char* name);
52 JavaType javaTypeFromPrimitiveType(char type);
53 const char* signatureFromJavaType(JavaType);
55 jvalue getJNIField(jobject, JavaType, const char* name, const char* signature);
56 jvalue callJNIMethod(jobject, JavaType returnType, const char* name, const char* signature, jvalue* args);
58 jmethodID getMethodID(jobject, const char* name, const char* sig);
61 void setJavaVM(JavaVM*);
64 template <typename T> struct JNICaller;
66 template<> struct JNICaller<void> {
67 static void callA(jobject obj, jmethodID mid, jvalue* args)
69 getJNIEnv()->CallVoidMethodA(obj, mid, args);
71 static void callV(jobject obj, jmethodID mid, va_list args)
73 getJNIEnv()->CallVoidMethodV(obj, mid, args);
77 template<> struct JNICaller<jobject> {
78 static jobject callA(jobject obj, jmethodID mid, jvalue* args)
80 return getJNIEnv()->CallObjectMethodA(obj, mid, args);
82 static jobject callV(jobject obj, jmethodID mid, va_list args)
84 return getJNIEnv()->CallObjectMethodV(obj, mid, args);
88 template<> struct JNICaller<jboolean> {
89 static jboolean callA(jobject obj, jmethodID mid, jvalue* args)
91 return getJNIEnv()->CallBooleanMethodA(obj, mid, args);
93 static jboolean callV(jobject obj, jmethodID mid, va_list args)
95 return getJNIEnv()->CallBooleanMethodV(obj, mid, args);
97 static jboolean callStaticV(jclass cls, jmethodID mid, va_list args)
99 return getJNIEnv()->CallStaticBooleanMethod(cls, mid, args);
103 template<> struct JNICaller<jbyte> {
104 static jbyte callA(jobject obj, jmethodID mid, jvalue* args)
106 return getJNIEnv()->CallByteMethodA(obj, mid, args);
108 static jbyte callV(jobject obj, jmethodID mid, va_list args)
110 return getJNIEnv()->CallByteMethodV(obj, mid, args);
114 template<> struct JNICaller<jchar> {
115 static jchar callA(jobject obj, jmethodID mid, jvalue* args)
117 return getJNIEnv()->CallCharMethodA(obj, mid, args);
119 static jchar callV(jobject obj, jmethodID mid, va_list args)
121 return getJNIEnv()->CallCharMethodV(obj, mid, args);
125 template<> struct JNICaller<jshort> {
126 static jshort callA(jobject obj, jmethodID mid, jvalue* args)
128 return getJNIEnv()->CallShortMethodA(obj, mid, args);
130 static jshort callV(jobject obj, jmethodID mid, va_list args)
132 return getJNIEnv()->CallShortMethodV(obj, mid, args);
136 template<> struct JNICaller<jint> {
137 static jint callA(jobject obj, jmethodID mid, jvalue* args)
139 return getJNIEnv()->CallIntMethodA(obj, mid, args);
141 static jint callV(jobject obj, jmethodID mid, va_list args)
143 return getJNIEnv()->CallIntMethodV(obj, mid, args);
147 template<> struct JNICaller<jlong> {
148 static jlong callA(jobject obj, jmethodID mid, jvalue* args)
150 return getJNIEnv()->CallLongMethodA(obj, mid, args);
152 static jlong callV(jobject obj, jmethodID mid, va_list args)
154 return getJNIEnv()->CallLongMethodV(obj, mid, args);
158 template<> struct JNICaller<jfloat> {
159 static jfloat callA(jobject obj, jmethodID mid, jvalue* args)
161 return getJNIEnv()->CallFloatMethodA(obj, mid, args);
163 static jfloat callV(jobject obj, jmethodID mid, va_list args)
165 return getJNIEnv()->CallFloatMethodV(obj, mid, args);
169 template<> struct JNICaller<jdouble> {
170 static jdouble callA(jobject obj, jmethodID mid, jvalue* args)
172 return getJNIEnv()->CallDoubleMethodA(obj, mid, args);
174 static jdouble callV(jobject obj, jmethodID mid, va_list args)
176 return getJNIEnv()->CallDoubleMethodV(obj, mid, args);
180 template<typename T> T callJNIMethodIDA(jobject obj, jmethodID mid, jvalue *args)
182 return JNICaller<T>::callA(obj, mid, args);
186 static T callJNIMethodV(jobject obj, const char* name, const char* sig, va_list args)
188 JavaVM* jvm = getJavaVM();
189 JNIEnv* env = getJNIEnv();
191 if (obj && jvm && env) {
192 jclass cls = env->GetObjectClass(obj);
194 jmethodID mid = env->GetMethodID(cls, name, sig);
196 // Avoids references to cls without popping the local frame.
197 env->DeleteLocalRef(cls);
198 return JNICaller<T>::callV(obj, mid, args);
200 LOG_ERROR("Could not find method: %s for %p", name, obj);
201 env->ExceptionDescribe();
202 env->ExceptionClear();
203 fprintf(stderr, "\n");
205 env->DeleteLocalRef(cls);
207 LOG_ERROR("Could not find class for %p", obj);
214 T callJNIMethod(jobject obj, const char* methodName, const char* methodSignature, ...)
217 va_start(args, methodSignature);
219 T result = callJNIMethodV<T>(obj, methodName, methodSignature, args);
227 T callJNIStaticMethod(jclass cls, const char* methodName, const char* methodSignature, ...)
229 JavaVM* jvm = getJavaVM();
230 JNIEnv* env = getJNIEnv();
233 va_start(args, methodSignature);
237 if (cls && jvm && env) {
238 jmethodID mid = env->GetStaticMethodID(cls, methodName, methodSignature);
240 result = JNICaller<T>::callStaticV(cls, mid, args);
242 LOG_ERROR("Could not find method: %s for %p", methodName, cls);
243 env->ExceptionDescribe();
244 env->ExceptionClear();
245 fprintf(stderr, "\n");
254 } // namespace Bindings
258 #endif // ENABLE(JAVA_BRIDGE)
260 #endif // JNIUtility_h