例如这个JNI教程相当详细地介绍了如何访问对象内的原始字段,以及如何访问作为显式函数参数(即jarray
子类)提供的数组。但是如何访问作为jobject
字段的Java(原始)数组呢?例如,我想操作以下Java对象的字节数组:
class JavaClass {
...
int i;
byte[] a;
}
主程序可能是这样的:
class Test {
public static void main(String[] args) {
JavaClass jc = new JavaClass();
jc.a = new byte[100];
...
process(jc);
}
public static native void process(JavaClass jc);
}
相应的C++端代码如下:
JNIEXPORT void JNICALL Java_Test_process(JNIEnv * env, jclass c, jobject jc) {
jclass jcClass = env->GetObjectClass(jc);
jfieldID iId = env->GetFieldID(jcClass, "i", "I");
// This way we can get and set the "i" field. Let's double it:
jint i = env->GetIntField(jc, iId);
env->SetIntField(jc, iId, i * 2);
// The jfieldID of the "a" field (byte array) can be got like this:
jfieldID aId = env->GetFieldID(jcClass, "a", "[B");
// But how do we operate on the array???
}
我考虑使用GetByteArrayElements
,但它需要一个ArrayType
作为其参数。 显然我错过了什么。 有没有一种方法可以做到这一点?
double * data
?而不是jdouble * data
? - Maksim Dmitriev