Reputation: 85
I have a java method which is called from my native method and returns an object array to the native code.
The elements of the object array are set as,
Object[] arr = new Object[10];
arr[0] = new Integer(12);
arr[1] = new Float(25.5f);
I receive this array in my native code as follows,
jobjectArray a = (jobjectArray)(*env)->CallStaticObjectMethodA(env, <class_id>, <method_id>, <parameter_list>);
I have the datatype of each of the element stored in object array. So based on the datatype how can I access the corresponding integer and float value in my native method.
I tried the following,
jobject obj = (*env)->GetObjectArrayElement(env, a, 0);
int num = (jint)obj;
But the value, that is set to num is incorrect.
Upvotes: 2
Views: 4045
Reputation: 33865
Java does auto-unboxing when you convert an Integer
to an int
. But that mechanic doesn't exist in C. What you're doing is taking the memory address of the element and interpreting it as an int
.
If you want to get an int
from an Integer
on the C
side, you will have to call intValue
:
jobject objInteger = (*env)->GetObjectArrayElement(env, a, 0);
jclass cInteger = (*env)->FindClass(env, "java/lang/Integer");
jmethodID intValue = (*env)->GetMethodID(env, cInteger, "intValue", "()I");
int i = (*env)->CallIntMethod(env, objInteger, intValue);
Upvotes: 2