Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of an enum being passed to the JNI

I have a Java application and JNI (dll). I want to know how to get the value of the enum (int) that is being passed as a parameter to the JNI.

Here's the enum (Java):

public enum envelopeType
{
    NOT_SPECIFIED(-1),
    NONE(0),
    IMAGE(1),
    BITMAP(2);

    private int value;

    private envelopeType(int value)
    {
        this.value = value;
    }   
}

Here's the JNI code (C++):

JNIEXPORT jint JNICALL Java_Loader_Convert
  (JNIEnv *env, jobject obj, jobject EnvelopeType)

since the enum is being passed as an object, how could I get the value of that?

like image 817
BebzSusuma Avatar asked Dec 11 '22 12:12

BebzSusuma


1 Answers

I was not able to use the solution provided by @tbodt but his was close enough that I was able to find the solution.

Looking at the java enum documentaiton there is the method ordinal that will return the enum value as an int type.

The code I used was almost identical to that lised in @tbodts solution however the strings passed into the GetMethodID function are different. I don't need to create a getValue method and the method signature is ()I not I().

JNIEXPORT jint JNICALL Java_Loader_Convert(JNIEnv *env, jobject obj, jobject EnvelopeType) {
    jmethodID envelopeGetValueMethod = (*env)->GetMethodID(env, (*env)->FindClass(env, "package/of/envelopeType"), "ordinal", "()I");
    jint value = (*env)->CallIntMethod(env, EnvelopeType, envelopeGetValueMethod);
    switch (value) {
        case -1:
        // not specified
        break;
        case 0:
        // none
        break;
        ...
    }
    // rest of native method
}
like image 65
gnash117 Avatar answered Dec 14 '22 02:12

gnash117