如何通过JNI向Java公开本机枚举? [英] How to expose a native enum to Java through JNI?

查看:195
本文介绍了如何通过JNI向Java公开本机枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从现有项目中导入标头,以将其移植到Android-NDK.在某些情况下,我想在Java层中使用在本机头中定义的枚举.怎么能做到这一点?

I am importing headers from an existing project for a port to Android-NDK. In a few cases, there are enums defined in the native headers which I would like to use from the Java layer. How can one go about doing that?

理想情况下,我只是想以某种方式将常量公开给Java层,但是我看不到这样做的方法.

Ideally, I'd like to just expose the constants to the Java layer somehow, but I don't see a way to do that.

最明显的可能性是在Java和C ++中双重定义枚举.但是,现有的标头a)没有显式编号,b)包含#ifdef'ed的元素,并且c)通过SVN外部与现有项目共享.因此,双重定义枚举似乎比典型情况要脆弱得多.

The most obvious possibility is to doubly-define the enums in both Java and C++. However, the existing headers a) have no explicit numbering, b) have elements which are #ifdef'ed, and c) are shared with existing projects through SVN Externals. Therefore, doubly-defining the enums seems substantially more brittle than even the typical case.

下一个想法是使用一些构建时代码生成器基于预处理的标头在Java中创建枚举-可能只是整数常量而不是Java枚举?

The next idea is to use some build-time code-gen to create the enum in Java based on the pre-processed header -- possibly just as integer constants rather than a Java enum?

我的第三个也是最模糊的想法是用Java定义枚举,将这些对象传递给JNI胶,然后将其与对FindClass(),GetStaticFieldID()和GetStaticObjectField()的调用进行比较;然后让JNI胶将它们重新映射到本机枚举.不过,那一切似乎效率低下.

The third and most-nebulous idea I have is to define the enum in Java, pass those objects to the JNI glue, and have it compare against the some invocation of FindClass(), GetStaticFieldID(), and GetStaticObjectField(); then have the JNI glue re-map those to the native enum. That all seems inefficient, though.

建议?

推荐答案

我将制作一组完全独立的Java枚举,并在两个都可用的JNI级别之间进行映射.确保在枚举类上运行javah,以便为它们的常规指定一些#define.

I would make a completely independent set of Java enums, and map between them at the JNI level where you have both available. Make sure to run javah on the enum classes so you get some #defines for their ordinals.

这篇关于如何通过JNI向Java公开本机枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆