First day working with JNI and in my search I have not found a solution that quite matches the problem I am having. I have three classes, and a class method I am trying use like this:
Class1EobjectClass Method1("some text")
Class2EobjectClass
What my execution looked like in source code was:
Class1 class1Instance = new Class1();
// class1Instance.Method1() returns an EobjectClass which is cast to Class2
Class2 result = (Class2) class1Instance.Method1("Some string of text");
result then had the object I desired. I am struggling with how to do this from the JNI interface. Here is what I have so far.
jclass lookForClass(JNIEnv* env, char* name)
{
jclass clazz = env->FindClass(name);
if (!clazz) {
printf("Unable to find class %s\n", name);
exit(1);
}
printf("Class %s found\n", name);
fflush(stdout);
return clazz;
}
jobject invokeClassObj(JNIEnv* env, jclass classInDll) {
jmethodID init;
jobject result;
init = env->GetMethodID(classInDll, "<init>", "()V");
if (!init) {
printf("Error: Could not find class Constructor\n");
return;
}
result = env->NewObject(classInDll, init);
if (!result) {
printf("Error: failed to allocate an object\n");
return;
}
return result;
}
jclass Parser = lookForClass(env, "com/Path/Parser");
jclass TextModel = lookForClass(env, "com/Path/TextModel");
jobject ParserInstance = invokeClassObj(env, Parser);
jmethodID parse = GetMethodID(Parser, "parse", "(Ljava/lang/String;)Lorg/Path/Eobject;");
Now here is where I lose what I am supposed to do. What it looks like logically to me is:
TextModel model = static_cast<TextModel> (ParserInstance.parse("some text here"));
But how would I execute that in this JNI environment? If there is any information or clarifications I missed please comment.