Java Native Interface Programming |
The JNI provides a slightly cleaner interface for C++ programmers. Thejni.h
file contains a set of inline C++ functions so that the native method programmer can simply write:jclass cls = env->FindClass("java/lang/String");instead of:jclass cls = (*env)->FindClass(env, "java/lang/String");The extra level of indirection on
env
and theenv
argument toFindClass
are hidden from the programmer. The C++ compiler simply expands out the C++ member function calls to their C counterparts; therefore, the resulting code is exactly the same.The
jni.h
file also defines a set of dummy C++ classes to enforce the subtyping relationships among different variations ofjobject
types:class _jobject {}; class _jclass : public _jobject {}; class _jthrowable : public _jobject {}; class _jstring : public _jobject {}; ... /* more on jarray */ typedef _jobject *jobject; typedef _jclass *jclass; typedef _jthrowable *jthrowable; typedef _jstring *jstring; ... /* more on jarray */The C++ compiler is therefore able to detect if you pass in, for example, ajobject
toGetMethodID
:jmethodID GetMethodID(jclass clazz, const char *name, const char *sig);becauseGetMethodID
expects ajclass
. In C,jclass
is simply the same asjobject
:typedef jobject jclass;Therefore a C compiler is not able to detect that you have mistakenly passed ajobject
instead ofjclass
.The added type safety in C++ does come which a small inconvenience. Recall from Accessing Java Arrays that in C, you can fetch a Java string from an array of strings and directly assign the result to a
jstring
:jstring jstr = (*env)->GetObjectArrayElement(env, arr, i);In C++, however, you need to insert an explicit conversion:jstring jstr = (jstring)env->GetObjectArrayElement(arr, i);becausejstring
is a subtype ofjobject
, the return type ofGetObjectArrayElement
.
Java Native Interface Programming |