我有一个 C++ 类,其成员来自枚举类型。我想使用 jni 在 java 中公开此类中的对象。我已经为类(class)的所有成员成功完成了,但我对枚举类型成员有疑问。我用这种方式在 java 中定义了枚举
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;
}
}
在c++中是这样
enum Call {
UNDEFINED = -1,
INCOMING = 1,
OUTGOING = 2,
MISSED = 4
};
c++中原来的类是
class LogData{
int _id;
Call _calltype;
long _datetime;
int _duration;
}
在Java中
public class LogDataJava{
int _id;
Call _callType;
long _dateTime;
int _duration;
}
关于如何在 jni 级别为枚举类型进行映射有什么建议吗?
最佳答案
枚举值基本上是枚举类中的静态字段。
例如,您可以在 jni 代码中执行以下操作,将其映射到 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);
您还可以找到有关静态字段的更多信息 here
关于java - 通过 jni 将枚举 native 类型公开给 java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19793082/