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.
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++).
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.
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 {}; ...
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With