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?
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
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