Wednesday, April 24, 2013

jni jnienv ToReflectedMethod example c c++ java


ToReflectedMethod

jobject ToReflectedMethod(JNIEnv *env, jclass cls,
   jmethodID methodID);
Converts a method ID derived from cls to a java.lang.reflect.Method or java.lang.reflect.Constructor object.
Throws OutOfMemoryError and returns 0 if fails.
LINKAGE:
Index 9 in the JNIEnv interface function table.
Example of ToReflectedMethod
/*
    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.ToReflectedMethodTest.nativeExecute(Ljava/lang/String;)Ljava/lang/reflect/Method;
 */
JNIEXPORT jobject JNICALL
Java_org_apache_harmony_vts_test_vm_jni_reflection_ToReflectedMethodTest_nativeExecute
    (JNIEnv *env, jobject this_object, jstring str)
{
 jmethodID mID;
 jclass clazz;
 jobject ret;

 clazz = (*env)->GetObjectClass(env, str);
 if(!clazz) return NULL;
 mID = (*env)->GetMethodID(env, clazz, "length", "()I");
 if(!mID) return NULL;
 return (*env)->ToReflectedMethod(env, clazz, mID, JNI_FALSE); 
}

/*
 * Method: org.apache.harmony.vts.test.vm.jni.reflection.ToReflectedMethodTest.nativeExecute1(Ljava/lang/String;)Ljava/lang/reflect/Constructor;
 */
JNIEXPORT jobject JNICALL
Java_org_apache_harmony_vts_test_vm_jni_reflection_ToReflectedMethodTest_nativeExecute1
    (JNIEnv *env, jobject this_object, jstring str)
{
 jmethodID mID;
 jclass clazz;

 clazz = (*env)->GetObjectClass(env, str);
 if(!clazz) return NULL;
 mID = (*env)->GetMethodID(env, clazz, "<init>", "(Ljava/lang/String;)V");
 if(!mID) return NULL;
 return (*env)->ToReflectedMethod(env, clazz, mID, JNI_FALSE); 
}