|
|
Java Native Interface Programming |
The JNI provides a slightly cleaner interface for C++ programmers. Thejni.hfile 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
envand theenvargument toFindClassare 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.hfile also defines a set of dummy C++ classes to enforce the subtyping relationships among different variations ofjobjecttypes: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, ajobjecttoGetMethodID:jmethodID GetMethodID(jclass clazz, const char *name, const char *sig);becauseGetMethodIDexpects ajclass. In C,jclassis simply the same asjobject:typedef jobject jclass;Therefore a C compiler is not able to detect that you have mistakenly passed ajobjectinstead 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);becausejstringis a subtype ofjobject, the return type ofGetObjectArrayElement.
|
|
Java Native Interface Programming |