vendredi 27 septembre 2019

Finding if a Java class is final in JNI by using reflection

I'm trying to find if a java class is final from C++ (jni) by using reflection. So, having the next Java methods in Jni:

int modifiers = com.package_name.class_name.class.getModifiers();
Modifier.isFinal(mofidiers);

Everything works fine until calling the reflection for Modifiers.isFinal, which reports incorrectly that a none final class is actually final.

I've verified the Modifiers.getModifiers results, and as expected when not final it returns correctly 1, and when final returns 17. Yet Modifiers.IsFinal() also returns True for the "1" value result, which is public and not final.

This issue doesn't happen if Java, only in Jni. And I would prefer not to compare directly against the numeric results.

jboolean test(JNIEnv* env)
{
    jclass class_modifier = env->FindClass("java/lang/reflect/Modifier");
    jmethodID method_isFinal = env->GetStaticMethodID(class_modifier, "isFinal", "(I)Z");

    jclass class_Class = env->FindClass("java/lang/Class");
    jclass class_app = env->FindClass("com.package_name.Test");
    jmethodID method_getModifiers = env->GetMethodID(class_Class, "getModifiers", "()I");

    jint modifiers = env->CallIntMethod(class_app, method_getModifiers);
    return env->CallBooleanMethod(class_modifier, method_isFinal, modifiers);
} 




Aucun commentaire:

Enregistrer un commentaire