how to return enum from JNI

后端 未结 2 809
无人及你
无人及你 2020-12-05 14:47

In java I have:

public class MyClass{

    public enum STATUS {
        ZERO,
        ONE ,
        TWO
    }

    public native STATUS nativeGetStatus();

          


        
相关标签:
2条回答
  • 2020-12-05 15:26

    I struggled with the accepted answer since I couldn't figure out the signature of the static field for a while. So here's the JNI implementation that should work with the example above (not tested):

    jclass clSTATUS    = env->FindClass("MyClass$STATUS");
    jfieldID fidONE    = env->GetStaticFieldID(clSTATUS , "ONE", "LMyClass$STATUS;");
    jobject STATUS_ONE = env->GetStaticObjectField(clSTATUS, fidONE);
    
    return STATUS_ONE;
    
    0 讨论(0)
  • 2020-12-05 15:33

    Of course, you can do it. Enum values are public static fields of Enum class, so you can use this official manual to write the code. Just get the field from JNI and return it as jobject.

    0 讨论(0)
提交回复
热议问题