Wednesday, April 24, 2013

jni jnienv FromReflectedField example c c++ java


FromReflectedField

jfieldID FromReflectedField(JNIEnv *env, jobject field);
Converts a java.lang.reflect.Field to a field ID.
LINKAGE:
Index 8 in the JNIEnv interface function table.

Example of FromReflectedField


#include <jni.h>
/*
 * Method: org.apache.harmony.vts.test.vm.jni.reflection.FromReflectedFieldTest.nativeExecute(Ljava/lang/reflect/Field;Ljava/lang/reflect/Field;)Z
 */
JNIEXPORT jboolean JNICALL
Java_org_apache_harmony_vts_test_vm_jni_reflection_FromReflectedFieldTest_nativeExecute
    (JNIEnv *env, jobject this_object, jobject objField, jobject statField)
{
 jfieldID fID_s1,fID_s2,fID_o1,fID_o2;
 jclass cls;

 fID_s1 = (*env)->FromReflectedField(env, statField);
 fID_o1 = (*env)->FromReflectedField(env, objField);
 if(!fID_o1 || !fID_s1) return JNI_FALSE;

 cls  = (*env)->GetObjectClass(env, this_object);
 if(!cls) return JNI_FALSE;
 fID_s2 = (*env)->GetStaticFieldID(env, cls, "staticField", "I");
 fID_o2 = (*env)->GetFieldID(env, cls, "objectField", "D");
 if(!fID_o2 || !fID_s2) return JNI_FALSE;

    if(fID_o1 != fID_o2) return JNI_FALSE;
    if(fID_s1 != fID_s2) return JNI_FALSE;
 return JNI_TRUE;
}