10.3 Confusing jclass with jobject
注意:不要混淆jclass和jobject
Class references(类引用) correspond to java.lang.Class instances, which represent class types.
Instance references(实例引用) correspond to arrays and instances of java.lang.Object or one of its subclasses.
An operation such as
GetFieldID, which takes a jclass, is
a class operation because it gets the field descriptor from a class.
In contrast,
GetIntField, which takes a jobject, is
an instance operation because it gets the value of a field from an instance.
10.4 Truncating(截断) jboolean Arguments
注意:截断jboolean类型
jboolean是一个8位的unsigned类型,值范围为0~255.0对应JNI_FALSE,1~255对应JNI_TRUE.
对于32位或16位的变量a赋值给jboolean时,如果a的低8位为0会出现逻辑问题.
代码:
void print(jboolean condition)
{
if (condition) {
printf("true\n");
} else {
printf("false\n");
}
}
//错误
int n = 256;
print (n); //==>JNI_FALSE
对于32位变量n(256的低8位为0),本应该判断为JNI_TRUE,实际判断为JNI_FLASE.
改为如下后逻辑正确:
//正确
int n = 256;
print(n ? JNI_TRUE : JNI_FALSE);
10.6
Confusing
IDs
with
References
注意:不要混淆ID和引用
The JNI exposes
objects as references. Classes, Strings, and Arrays are special types of references.
The JNI exposes
methods and fields as IDs. An ID is not a reference.
Native code may create
multiple references that refer to the same object.
In contrast,
a unique field or method ID is derived for the same definition of a field or a method.
If class A defines method f and class B inherits f from A, the two GetMethodID calls in the following code always return the same result:
jmethodID MID_A_f = (*env)->GetMethodID(env, A, "f", "()V");
jmethodID MID_B_f = (*env)->GetMethodID(env, B, "f", "()V");
10.7 Caching Field and Method IDs
缓存域ID(Filed ID)和方法ID(Method ID)
下面举例说明了没有缓存域ID可能带来的bug:
class C {
private int i;
native void f();
}
(1)不缓存域ID:
1) 获取对象的类,GetObjectClass.
2) 找出变量i的域ID,
GetFieldID.
3) 基于域ID获取域值,
GetIntField.
// No field IDs cached.
JNIEXPORT void JNICALL Java_C_f(JNIEnv *env, jobject this) {
jclass cls = (*env)->GetObjectClass(env, this);
jfieldID fid = (*env)->GetFieldID(env, cls, "i", "I");
int ival = (*env)->GetIntField(env, this, fid);
}
如果我们定义C类的子类D,D类里也声明一个变量i,会出现问题:
// Trouble in the absence of ID caching
class D extends C {
private int i;
D() {
f(); // inherited from C
}
}
当D的构造函数调用C的接口f时,native接口Java_C_f里的jobject参数是D的对象,cls指向D类,fid代表的是D.i。所以ival里的值是D.i而不是C.i。这可能不是我们想要的。
如何解决?
(2)缓存域ID:
解决办法是当你确定要使用哪个类时,缓存对应的域ID。见如下代码:
// Version that caches IDs in static initializers
class C {
private int i;
native void f();
private static native void initIDs();
static {
initIDs(); // Call an initializing native method
}
}
static jfieldID FID_C_i; //缓存域ID
JNIEXPORT void JNICALL Java_C_initIDs(JNIEnv *env, jclass cls) {
/* Get IDs to all fields/methods of C that native methods will need. */
FID_C_i = (*env)->GetFieldID(env, cls, "i", "I");
}
JNIEXPORT void JNICALL Java_C_f(JNIEnv *env, jobject this) {
int ival = (*env)->GetIntField(env, this, FID_C_i);
/* ival is always C.i, not D.i */
}
域ID缓存在C类的静态方法initIDs里。这就保证了
FID_C_i缓存的是C类变量i的域.
10.8 Terminating(结束) Unicode Strings
用GetStringChars或GetStringCritical接口获取的Unicode字符串不是以null结尾的。一般用GetStringLength来获取Unicode字符串的长度。
10.11 Retaining Virtual Machine Resources 防止内存泄漏
A common mistake in native methods is
forgetting to free virtual machine resources.
JNIEXPORT void JNICALL
Java_pkg_Cls_f(JNIEnv *env, jclass cls, jstring jstr)
{
const jchar *cstr =
(*env)->GetStringChars(env, jstr, NULL);
if (cstr == NULL) {
return;
}
...
if (...) { /* exception occurred */
/* misses a ReleaseStringChars call */
return;
}
...
/* normal return */
(*env)->ReleaseStringChars(env, jstr, cstr);
}