如果让你在jni层,找到一个目标函数,你应该怎么做?
1.找到目标类,然后打印所有的方法,然后通过方法返回值,方法参数,方法修饰符来确定我们要找的目标函数
代码如下
#include <jni.h>
#include <stdio.h>void printMethod(JNIEnv *env, jobject method) {// 获取 Method 类的 Class 对象jclass methodClass = (*env)->GetObjectClass(env, method);// 获取 getName 方法的 Method 对象jmethodID getNameMethod = (*env)->GetMethodID(env, methodClass, "getName", "()Ljava/lang/String;");// 调用 getName 方法获取方法名jstring methodName = (jstring)(*env)->CallObjectMethod(env, method, getNameMethod);// 将 Java 字符串转换为 C 字符串const char *cMethodName = (*env)->GetStringUTFChars(env, methodName, NULL);// 获取返回值类型jmethodID getReturnTypeMethod = (*env)->GetMethodID(env, methodClass, "getReturnType", "()Ljava/lang/Class;");jobject returnTypeObject = (*env)->CallObjectMethod(env, method, getReturnTypeMethod);jclass returnTypeClass = (*env)->GetObjectClass(env, returnTypeObject);// 获取 getName 方法的 Method 对象jmethodID returnTypeGetNameMethod = (*env)->GetMethodID(env, returnTypeClass, "getName", "()Ljava/lang/String;");jstring returnTypeString = (jstring)(*env)->CallObjectMethod(env, returnTypeObject, returnTypeGetNameMethod);const char *cReturnType = (*env)->GetStringUTFChars(env, returnTypeString, NULL);// 获取参数类型jmethodID getParameterTypesMethod = (*env)->GetMethodID(env, methodClass, "getParameterTypes", "()[Ljava/lang/Class;");jobjectArray parameterTypesArray = (jobjectArray)(*env)->CallObjectMethod(env, method, getParameterTypesMethod);jsize parameterTypesLength = (*env)->GetArrayLength(env, parameterTypesArray);printf("Method Name: %s\n", cMethodName);printf("Return Type: %s\n", cReturnType);printf("Parameter Types: ");// 打印参数类型for (int i = 0; i < parameterTypesLength; i++) {jobject parameterTypeObject = (*env)->GetObjectArrayElement(env, parameterTypesArray, i);jclass parameterTypeClass = (*env)->GetObjectClass(env, parameterTypeObject);jmethodID parameterTypeGetNameMethod = (*env)->GetMethodID(env, parameterTypeClass, "getName", "()Ljava/lang/String;");jstring parameterTypeString = (jstring)(*env)->CallObjectMethod(env, parameterTypeObject, parameterTypeGetNameMethod);const char *cParameterType = (*env)->GetStringUTFChars(env, parameterTypeString, NULL);printf("%s ", cParameterType);// 释放资源(*env)->ReleaseStringUTFChars(env, parameterTypeString, cParameterType);}// 获取修饰符的值jmethodID getModifiersMethod = (*env)->GetMethodID(env, methodClass, "getModifiers", "()I");jint modifiers = (*env)->CallIntMethod(env, method, getModifiersMethod);printf("\nModifiers: %d\n", modifiers);// 释放资源(*env)->ReleaseStringUTFChars(env, methodName, cMethodName);(*env)->ReleaseStringUTFChars(env, returnTypeString, cReturnType);
}JNIEXPORT void JNICALL Java_com_example_MyClass_invokeGetMethods(JNIEnv *env, jobject obj) {// 获取当前类的 Class 对象jclass clazz = (*env)->GetObjectClass(env, obj);// 获取 Class 类的 Class 对象jclass classClass = (*env)->FindClass(env, "java/lang/Class");// 获取 getMethods 方法的 Method 对象jmethodID getMethodsMethod = (*env)->GetMethodID(env, classClass, "getMethods", "()[Ljava/lang/reflect/Method;");// 调用 getMethods 方法获取 Method 数组jobjectArray methodsArray = (jobjectArray)(*env)->CallObjectMethod(env, clazz, getMethodsMethod);// 获取数组长度jsize arrayLength = (*env)->GetArrayLength(env, methodsArray);// 遍历 Method 数组for (int i = 0; i < arrayLength; i++) {// 获取 Method 对象jobject methodObject = (*env)->GetObjectArrayElement(env, methodsArray, i);// 打印方法信息printf("Method %d:\n", i + 1);printMethod(env, methodObject);printf("\n");}
}