Wednesday, April 24, 2013

jni jnienv ToReflectedField example c c++ java


ToReflectedField

jobject ToReflectedField(JNIEnv *env, jclass cls,
   jfieldID fieldID);
Converts a field ID derived from cls to a java.lang.reflect.Field object.
Throws OutOfMemoryError and returns 0 if fails.
LINKAGE:
Index 12 in the JNIEnv interface function table.
Example of ToReflectedField
/*
    Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

    See the License for the specific language governing permissions and
    limitations under the License.
*/
  
#include <jni.h>

/*
 * Method: org.apache.harmony.vts.test.vm.jni.reflection.ToReflectedFieldTest.nativeExecute()Ljava/lang/reflect/Field;
 */
JNIEXPORT jobject JNICALL
Java_org_apache_harmony_vts_test_vm_jni_reflection_ToReflectedFieldTest_nativeExecute
    (JNIEnv *env, jobject this_object)
{
 jfieldID fID;
 jclass clazz = (*env)->GetObjectClass(env, this_object);
 if(!clazz) return NULL;
 fID = (*env)->GetStaticFieldID(env, clazz, "staticField", "I");
 return (*env)->ToReflectedField(env, clazz, fID, JNI_TRUE);
}

/*
 * Method: org.apache.harmony.vts.test.vm.jni.reflection.ToReflectedFieldTest.nativeExecute1()Ljava/lang/reflect/Field;
 */
JNIEXPORT jobject JNICALL
Java_org_apache_harmony_vts_test_vm_jni_reflection_ToReflectedFieldTest_nativeExecute1
    (JNIEnv *env, jobject this_object)
{
 jfieldID fID;
 jclass clazz = (*env)->GetObjectClass(env, this_object);
 if(!clazz) return NULL;
 fID = (*env)->GetFieldID(env, clazz, "objectField", "D");
 return (*env)->ToReflectedField(env, clazz, fID, JNI_FALSE);
}