Mostrando entradas con la etiqueta jni. Mostrar todas las entradas
Mostrando entradas con la etiqueta jni. Mostrar todas las entradas

sábado, 12 de marzo de 2011

Accesing Java enums values from C++ with JNI

Imagine we have to acces the enum values of a Java class. For example:

public enum ComponentCode {
VAD_CODE ((byte)1),
DIARIZATOR_CODE ((byte)2),
FEATURES_EXTRACTOR_CODE((byte)3),
UBM_CODE((byte)4),
HYPERPARAMS_CODE((byte)5),
GENDER_ID_CODE((byte)6),
MODEL_TRAINER_CODE((byte)7),
STATS_GENERATOR_CODE((byte)8);

private byte code;

ComponentCode (byte code) {
this.code = code;
}

public byte getCode () {
return code;
}

@Override
public String toString () {
return "" + (int)code;
}

};


The following C function can be used to acces the enum values:

jobject getStaticFieldID(JNIEnv *env, jclass c, const char *name, const char *signature)
{
jfieldID field = env->GetStaticFieldID (c, name, signature);
if (NULL == field) {
std::string msg = std::string("Error finding field ") + std::string(name) +
std::string(" (") + std::string(signature) + std::string(")");
throwFrontEndException(env, msg.c_str());
}
jobject obj = env->GetStaticObjectField(c, field);
if (NULL == obj) {
std::string msg = std::string("Error finding field ") + std::string(name) +
std::string(" (") + std::string(signature) + std::string(")");
throwFrontEndException(env, msg.c_str());
}

return obj;
}