如何从 JNI 中的类中提取枚举?
我有一个传递给本机方法的类,如下所示,
public enum Color{
eRED,
eGREEN,
eBLUE};
public class ConfigColor{
public ColorE color;
public int value;};
public native int HelloWord(ConfigColor ConfigColorcls);
ConfigColor clsConfigColor = new ConfigColor();
clsConfigColor .color = eGREEN;
clsConfigColor . value = 255;
HelloWord(clsConfigColor);
我可以使用 GetIntField 和 GetObjectClass 提取 int 值
。但是如何提取ColorE颜色
呢?请帮忙
I have a class which is passing to a native method as shown here
public enum Color{
eRED,
eGREEN,
eBLUE};
public class ConfigColor{
public ColorE color;
public int value;};
public native int HelloWord(ConfigColor ConfigColorcls);
ConfigColor clsConfigColor = new ConfigColor();
clsConfigColor .color = eGREEN;
clsConfigColor . value = 255;
HelloWord(clsConfigColor);
I can extract the int value
using GetIntField and GetObjectClass. But how to extract the ColorE color
? Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取该值,您可以使用以下
To get the value , you may use following
好吧,我认为您本身无法获得枚举,因为 JNI 支持的类型没有提及枚举:
http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp9502
我想这很正常,Java 枚举很漂亮特定于Java。
您可以做的是将枚举实例作为对象获取,调用 name() 方法,然后获取包含枚举类型(例如“eRED”)的字符串,然后您可以使用它。如果您通过 JNI 调用的类也是可以访问枚举的 Java 类,您可以执行以下操作:
Well, I don't think you get get the enum per-se, as the JNI supported types don't say nothing about enum:
http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp9502
That's pretty normal I guess, the Java enum is pretty specific to Java.
What you can do is get your enum instance as an object, invoke the name() method and you get the String containing your enum type (such as "eRED"), then you can use that. If the class you call via JNI is also a Java class that has access to the enum, you can do :
使用
GetObjectClass
获取类对象,然后对其调用getName()
。您将获得一个jstring
,例如“eRED”。这是您的枚举,但它可能不是最有用的形式。或者,考虑将整数与枚举相关联,如下所示:
这类似于 C 枚举,它们实际上只是(命名的)整数。然后,您可以直接在 C 中访问枚举的
number
字段,而不用解析字符串。Use
GetObjectClass
to get the class object, and then invokegetName()
on that. You'll get ajstring
, for example "eRED". That's your enum, but it may not be the most useful form.Alternatively, consider associating an integer with your enum like this:
This is analogous to C enums, which are really just (named) integers. Then you could access the enum's
number
field directly in C, instead of parsing strings.