0

This page https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html enumerates the data types that JNI handles. For instance, to handle a Java variable of data type int the equivalent in JNI is jint.

Now if I have a Java variable of data type Integer, how can I process this variable in JNI since there is no obvious equivalence for this data type?

2

1 Answer 1

1

You have to treat Integer as any other object. You have to find it's method (intValue) and call it inside JNI

JNIEXPORT void JNICALL Java_recipeNo055_PassObject_passInteger (JNIEnv * env, jclass obj, jobject objarg) { jclass cls = (*env)->GetObjectClass (env, objarg); jmethodID intValue = (*env)->GetMethodID(env, cls, "intValue", "()I"); jint result = (*env)->CallIntMethod(env, objarg, intValue); printf("%-22s: %d\n", "Passed value (int)", result); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Oo.oO ! Just one last question before I accept this question: do we need to call DeleteLocalRef on objarg at the end of this code snippet?
objarg will be removed once you leave the scope of the function. Take a look here: docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/… and paragraph: Local References. If, let's say, you have a loop where you call GetObjectClass (on the array of objects) and you create tones of references, it would be probably a good idea to remove them. In this sample, if jclass cls = (*env)->GetObjectClass (env, objarg) would have been called numerous times, it would be a good idea to "free" cls once each and every iteration of the loop is finished.