tags:

views:

121

answers:

1

Say I have a MyClass class in Java, is there a way to check in JNI that a jobject is a MyClass[][]?

My initial idea was to use env->IsInstanceOf(myobj, myArrayClass), but calling env->FindClass("[MyClass") throws a NoClassDefFoundError.

A: 

A little rusty on JNI, but a couple of things:

Call FindClass() on your fully qualified classname, using a "/" as a separator instead of dots. So, for instance if your class is "my.package.MyClass", you would call env->FindClass("my/package/MyClass")

Since you have a two-dimensional array of your object type, you need to call env->GetObjectArrayElement() twice; once to get a row and another time to get a distinct element. Then you can call env->IsInstanceOf() on that element. Make sure you look up the correct signatures for these JNI calls, I've left them as an exercise for the reader :)

Rob Heiser
The problem is that I don't know if the object I get is even an array, and the JNI spec doesn't even say what would happen if I try to call GetObjectArrayElement on an object that's not an array. I certainly can't rely on C++ to tell me that I can't convert jobject to jobjectArray.
Dan Berindei
You can call GetObjectClass() to get the class of the object. In your example above it would return "[LMyClass;" If the class is contained in a package, the name would be fully qualified with "/" instead of dots (i.e., "[Lmy/package/MyClass;")
Rob Heiser
Indeed, but I don't want to get the class, then get the name, then compare the strings on each call. I guess the best solution for my case would be to create an array in the first JNI call and cache its class so that I can do IsInstanceOf on it.
Dan Berindei