Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing JNI object references

I call a Java method from c++ via JNI. The Java method returns an enum STATUS. I already have the jobjects representing the enums in my c++ code like here: https://stackoverflow.com/a/17441151/3352197

jclass clSTATUS    = env->FindClass("MyClass$STATUS");
jfieldID fidONE    = env->GetStaticFieldID(clSTATUS , "ONE", "LMyClass$STATUS;");
jobject STATUS_ONE = env->GetStaticObjectField(clSTATUS, fidONE);

So, the call

jobject o = env->CallObjectMethod(jTestobject, test);

returns a jobject representing an enum STATUS, specially ONE. So, how do I know which enum it has returned? I tried to compare it to STATUS_ONE, but they do not match.

like image 318
sosnet Avatar asked Oct 07 '14 15:10

sosnet


People also ask

What is JNI reference?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What are local and global references in JNI?

The JNI divides object references used by the native code into two categories: local and global references. Local references are valid for the duration of a native method call, and are automatically freed after the native method returns. Global references remain valid until they are explicitly freed.

What is Jclass in JNI?

typedef jobject jclass; In C++, JNI introduces a set of dummy classes to enforce the subtyping relationship. For example: class _jobject {}; class _jclass : public _jobject {}; ...

What is JNIEnv * env?

jobject NewGlobalRef(JNIEnv *env, jobject obj); Creates a new global reference to the object referred to by the obj argument. The obj argument may be a global or local reference.


1 Answers

Found it by myself, after Samhain pointed out my possible mistake. You just have to compare the objects correctly:

env->IsSameObject(o, STATUS_ONE);

Thank you!

like image 88
sosnet Avatar answered Sep 23 '22 12:09

sosnet