如果让你在jni层,找到一个目标函数,你应该怎么做?
1.找到目标类,然后打印所有的方法,然后通过方法返回值,方法参数,方法修饰符来确定我们要找的目标函数
代码如下
#include
#include
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");
}
}