No need to implement Method.invoke ourselves (#1831)

This commit is contained in:
残页 2022-04-10 18:10:55 +08:00 committed by GitHub
parent ec5f7847e9
commit 9e3a3ac2b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 7 deletions

View File

@ -11,7 +11,6 @@ public class HookBridge {
public static native boolean deoptimizeMethod(Executable method); public static native boolean deoptimizeMethod(Executable method);
@FastNative
public static native Object invokeOriginalMethod(Executable method, Object thisObject, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException; public static native Object invokeOriginalMethod(Executable method, Object thisObject, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
@FastNative @FastNative

View File

@ -38,8 +38,7 @@ std::shared_mutex hooked_lock;
// Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements. // Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements.
std::unordered_map<jmethodID, HookItem> hooked_methods; std::unordered_map<jmethodID, HookItem> hooked_methods;
jobject (*native_invoke)(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jmethodID invoke = nullptr;
jobjectArray javaArgs);
} }
namespace lspd { namespace lspd {
@ -167,7 +166,7 @@ LSP_DEF_NATIVE_METHOD(jobject, HookBridge, invokeOriginalMethod, jobject hookMet
if (hook_item && hook_item->backup) { if (hook_item && hook_item->backup) {
to_call = hook_item->backup; to_call = hook_item->backup;
} }
return native_invoke(env, to_call, thiz, args); return env->CallObjectMethod(to_call, invoke, thiz, args);
} }
LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, instanceOf, jobject object, jclass expected_class) { LSP_DEF_NATIVE_METHOD(jboolean, HookBridge, instanceOf, jobject object, jclass expected_class) {
@ -184,11 +183,9 @@ static JNINativeMethod gMethods[] = {
void RegisterHookBridge(JNIEnv *env) { void RegisterHookBridge(JNIEnv *env) {
auto method = env->FindClass("java/lang/reflect/Method"); auto method = env->FindClass("java/lang/reflect/Method");
auto invoke = env->GetMethodID( invoke = env->GetMethodID(
method, "invoke", method, "invoke",
"(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
native_invoke = reinterpret_cast<decltype(native_invoke)>(
lsplant::GetNativeFunction(env, env->ToReflectedMethod(method, invoke, false)));
env->DeleteLocalRef(method); env->DeleteLocalRef(method);
REGISTER_LSP_NATIVE_METHODS(HookBridge); REGISTER_LSP_NATIVE_METHODS(HookBridge);
} }