如何将枚举的值传递给JNI [英] How to get the value of an enum being passed to the JNI
问题描述
这是枚举(Java):
public enum envelopeType
{
NOT_SPECIFIED(-1),
NONE(0),
IMAGE(1),
BITMAP(2);
private int value;
private envelopeType(int value)
{
this.value = value;
}
}
这是JNI代码(C ++):
JNIEXPORT jint JNICALL Java_Loader_Convert
(JNIEnv * env,jobject obj,jobject EnvelopeType)
由于枚举作为对象传递,我该如何获取该值?
我无法使用@tbodt提供的解决方案,但他足够接近我能够找到解决方案。
查看 java枚举文档有一个方法 ordinal
将返回枚举值作为 int
类型。
我使用的代码与@tbodts解决方案中的代码几乎相同,但是传入 GetMethodID
函数的字符串不同。我不需要创建一个 getValue
方法,方法签名是()I
不是 I()
。
JNIEXPORT jint JNICALL Java_Loader_Convert(JNIEnv * env,jobject obj,jobject EnvelopeType ){
jmethodID envelopeGetValueMethod =(* env) - > GetMethodID(env,(* env))> FindClass(env,package / of / envelopeType),ordinal,()I) ;
jint value =(* env) - > CallIntMethod(env,EnvelopeType,envelopeGetValueMethod);
switch(value){
case -1:
//未指定
break;
case 0:
// none
break;
...
}
//本机方法
}
I have a Java application and JNI (dll). I want to know how to get the value of the enum (int) that is being passed as a parameter to the JNI.
Here's the enum (Java):
public enum envelopeType
{
NOT_SPECIFIED(-1),
NONE(0),
IMAGE(1),
BITMAP(2);
private int value;
private envelopeType(int value)
{
this.value = value;
}
}
Here's the JNI code (C++):
JNIEXPORT jint JNICALL Java_Loader_Convert
(JNIEnv *env, jobject obj, jobject EnvelopeType)
since the enum is being passed as an object, how could I get the value of that?
I was not able to use the solution provided by @tbodt but his was close enough that I was able to find the solution.
Looking at the java enum documentaiton there is the method ordinal
that will return the enum value as an int
type.
The code I used was almost identical to that lised in @tbodts solution however the strings passed into the GetMethodID
function are different. I don't need to create a getValue
method and the method signature is ()I
not I()
.
JNIEXPORT jint JNICALL Java_Loader_Convert(JNIEnv *env, jobject obj, jobject EnvelopeType) {
jmethodID envelopeGetValueMethod = (*env)->GetMethodID(env, (*env)->FindClass(env, "package/of/envelopeType"), "ordinal", "()I");
jint value = (*env)->CallIntMethod(env, EnvelopeType, envelopeGetValueMethod);
switch (value) {
case -1:
// not specified
break;
case 0:
// none
break;
...
}
// rest of native method
}
这篇关于如何将枚举的值传递给JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!