Complex data from Java to C ++
Good night,
This is my first post here.
I am working on integration and I have some problems.
I am trying to pass some structured data to / from Java ↔ C ++ using JNI but I am having a situation.
Imagine something like this (despite the ugly format)
Class Detail {
public int v1;
public long v2;
}
Class Info {
public int Number;
pubinc int Size;
public Detail InfoExtra[] = new Detail[ 3 ];
Info(){
InfoExtra[0] = new Detail();
InfoExtra[1] = new Detail();
InfoExtra[2] = new Detail();
InfoExtra[3] = new Detail();
}
}
I am accessing "Number" and "Size" using GetFieldID () / GetIntField ().
My problem is I am trying to access the "InfoExtra" members and your attributes.
I can find "InfoExtra" using:
lfieldID = (env*)->GetFieldID( localClass, "InfoExtra", "[LInfoExtra;" )
But I don't know how to get it. How can i do this?
Regards Paulo
source to share
The first thing to do is change
public Detail InfoExtra[] = new Detail[ 3 ];
to
public Detail InfoExtra[] = new Detail[ 4 ];
to avoid the nasty ArrayIndexOfOutBounds exception .
You now have the wrong field signature. A quick way to generate signatures is the following command:
javap -s p <ClassName>
Valid signature for InfoExtra [LDetail; ...
To access the array, you must do something like this:
jclass clazz = (*env)->GetObjectClass(env,obj);
jfieldID infoExtra = (*env)->GetFieldID(env, clazz, "InfoExtra", "[LDetail;");
jobjectArray extras = (*env)->GetObjectField(env, clazz,infoExtra);
for (int i=0; i< ((*env)->GetArrayLength(env,extras)); i++) {
jobject element = (*env)->GetObjectArrayElement(env,extras,i);
//Do something with it, then release it
(*env)->DeleteLocalRef(env,element);
}
//Don't forget to release the array as well
(*env)->DeleteLocalRef(env,extras);
Hope this helps!
source to share