如何从 JNI 中的类中提取枚举?

发布于 2025-01-03 23:34:36 字数 509 浏览 1 评论 0原文

我有一个传递给本机方法的类,如下所示,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2025-01-10 23:34:36

要获取该值,您可以使用以下

public static Color getFromInt(int id) {
    for (Color candidate : Color.values()) {
        if (candidate.getAsInt() == id) {
            return candidate;
        }
    }
    throw new RuntimeException("no Color " + id);
}

To get the value , you may use following

public static Color getFromInt(int id) {
    for (Color candidate : Color.values()) {
        if (candidate.getAsInt() == id) {
            return candidate;
        }
    }
    throw new RuntimeException("no Color " + id);
}
无边思念无边月 2025-01-10 23:34:36

好吧,我认为您本身无法获得枚举,因为 JNI 支持的类型没有提及枚举:

http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp9502

我想这很正常,Java 枚举很漂亮特定于Java。

您可以做的是将枚举实例作为对象获取,调用 name() 方法,然后获取包含枚举类型(例如“eRED”)的字符串,然后您可以使用它。如果您通过 JNI 调用的类也是可以访问枚举的 Java 类,您可以执行以下操作:

ColorEnum colorEnumInstance = ColorEnum.valueOf("eGREEN");

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 :

ColorEnum colorEnumInstance = ColorEnum.valueOf("eGREEN");
无言温柔 2025-01-10 23:34:36

使用GetObjectClass 获取类对象,然后对其调用getName()。您将获得一个 jstring,例如“eRED”。这是您的枚举,但它可能不是最有用的形式。

或者,考虑将整数与枚举相关联,如下所示:

public enum Color {
    eRED(0),
    eGREEN(1),
    eBLUE(2);

    private Color(int numer) {
        this.number = number;
    }
    public final int number; // Maybe a byte would suffice...
}

这类似于 C 枚举,它们实际上只是(命名的)整数。然后,您可以直接在 C 中访问枚举的 number 字段,而不用解析字符串。

Use GetObjectClass to get the class object, and then invoke getName() on that. You'll get a jstring, 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:

public enum Color {
    eRED(0),
    eGREEN(1),
    eBLUE(2);

    private Color(int numer) {
        this.number = number;
    }
    public final int number; // Maybe a byte would suffice...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文