2

Suppose I got a jobject of unknown (arbitrary) type in JNI code. How can I check if it's an array? Extra points for finding type of array elements.

1 Answer 1

3

So, JNI doesn't seem to provide shortcut function to check for an array, so one has to emulate call to standard Java method Class.isArray (obj is jobject to test):

jmethodID Class_isArray_m = (*env)->GetMethodID(env, Class_class, "isArray", "()Z");
jclass obj_class = (*env)->GetObjectClass(obj);
jboolean is_array = (*env)->CallBooleanMethod(obj_class, Class_isArray_mid);

Element type can be found in similar way using Class.getComponentType(). However, depending on what native code wants to do, it may be easier to just get encoded type name using Class.getName() (https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getName()) and parse it - an array type starts with '[', if there's single letter after it, it's array of primitive types, otherwise it's object array (in particular, it may be multidimensional array).

Sign up to request clarification or add additional context in comments.

1 Comment

Strange that there is no shortcut function in the JNIEnv structure...... seems like an oversight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.