Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exposing enum native type to java through jni?

I have c++ class with member that is from enum type. I want to expose objects from this class in java using jni. I've done it successfully for all members from the class, but i have problem with the enum type member. I have defined enum in java in this way

 public enum Call {
    UNDEFINED(-1), INCOMING(1), OUTGOING(2), MISSED(4);
     private int type;
     private Call(int type) {
       this.type = type;
     }
     public int type() {
        return this.type; 
     }   
}

In c++ in this way

enum Call {
    UNDEFINED = -1,
    INCOMING = 1,
    OUTGOING = 2,
    MISSED = 4
};

The original class in c++ is

class LogData{
     int _id;
     Call _calltype;
     long _datetime;
     int _duration;
}

In java

public class LogDataJava{
  int _id; 
  Call _callType;
  long _dateTime;
  int _duration;
}

Any suggestions how to make mapping in jni level for the enum type?

like image 439
user2867676 Avatar asked Apr 13 '26 14:04

user2867676


1 Answers

an enum value is basically a static field in the enum class.

so for example you could do the following in your jni code, to map it to Java

LogData* l = /*...*/
jclass clCall = env->FindClass("LogDataJava$Call");
if (l->_callType == Call.UNDEFINED) {
    jfieldID fid = env->GetStaticFieldID(clCall , "UNDEFINED", "LLogDataJava$Call;"); 
} /* else ....*/

jobject callType = env->GetStaticObjectField(cl, fid);

You can also find more information about static fields here

like image 87
Naytzyrhc Avatar answered Apr 15 '26 04:04

Naytzyrhc