Reputation: 51
I have a callback method in Java which is called by a native thread. One of the parameter of callback is an ArrayList object.
I am not able to get classref for ArrayList and its methods. Tried with "Ljava/util/ArrayList" but in vain. Please suggest a solution. thanks in advance
Upvotes: 5
Views: 3848
Reputation: 14505
I don't understood right, but i think you need anything like it:
void methodToCallJava(std::vector<YourClassCpp*> itens) {
JNIEnv* env;
YourSavedJVM->AttachCurrentThread(&env, NULL);
jclass clazzDelegate = env->FindClass("your/delegate/class");
jclass clazzYourClassJava = env->FindClass("your/class/java");
jmethodID methodIdDelegate = env->GetMethodID(clazzDelegate, "delegateMethod", "(Ljava/util/ArrayList;)V");
jobjectArray arrayListFromCpp = nullptr;
jsize arrayListFromCppLength = itens.size();
if (arrayListFromCppLength > 0)
arrayListFromCpp = env->NewObjectArray(arrayListFromCppLength, clazzYourClassJava, methodToCreateJavaObjectFromCppObject(env, itens.at(0)));
for (jsize c = 1; c < arrayListFromCppLength; c = c + 1)
env->SetObjectArrayElement(arrayListFromCpp, c, methodToCreateJavaObjectFromCppObject(env, itens.at(c)));
env->CallVoidMethod(this->delegate, methodIdDelegate, arrayListFromCpp);
}
Upvotes: 1
Reputation: 48075
The class reference would be:
jclass cls = (*env)->FindClass(env, "java/util/ArrayList");
OR
jclass cls = (*env)->FindClass(env, "Ljava/util/ArrayList;");
You have an extra L
in front of the java/util/ArrayList
. In that case you also need to append a ;
.
Upvotes: 1