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
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).