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

PerracoLabs :

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 Modifier.isFinal(), which reports incorrectly that a non-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");
    jmethodID method_getModifiers = env->GetMethodID(class_Class, "getModifiers", "()I");

    jclass class_Test = env->FindClass("com/package_name/Test");
    jint modifiers = env->CallIntMethod(class_Test, method_getModifiers);
    return env->CallBooleanMethod(class_Modifier, method_isFinal, modifiers);
} 
API_1024 :

The problem is that isFinal is a static method, so:

Replace this:

env->CallBooleanMethod(class_modifier, method_isFinal, modifiers)

By this:

env->CallStaticBooleanMethod(class_modifier, method_isFinal, modifiers)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325699&siteId=1