Revert "Throw HookFailedException"

This reverts commit 06cb7abedbd1af4d58d42ebe79b5458f7dad62c5.
This commit is contained in:
Nullptr 2023-01-12 01:00:47 +08:00 committed by LoveSy
parent c69bd3fbcc
commit b228c224c1
1 changed files with 45 additions and 48 deletions

View File

@ -1,5 +1,7 @@
package org.lsposed.lspd.impl;
import static de.robv.android.xposed.callbacks.XCallback.PRIORITY_DEFAULT;
import android.app.ActivityThread;
import android.app.LoadedApk;
import android.content.BroadcastReceiver;
@ -850,115 +852,110 @@ public class LSPosedContext extends XposedContext {
}
private <T, U extends Executable> MethodUnhooker<T, U> doHook(U hookMethod, int priority, T callback) {
try {
if (Modifier.isAbstract(hookMethod.getModifiers())) {
throw new IllegalArgumentException("Cannot hook abstract methods: " + hookMethod);
} else if (hookMethod.getDeclaringClass().getClassLoader() == LSPosedContext.class.getClassLoader()) {
throw new IllegalArgumentException("Do not allow hooking inner methods");
} else if (hookMethod.getDeclaringClass() == Method.class && hookMethod.getName().equals("invoke")) {
throw new IllegalArgumentException("Cannot hook Method.invoke");
}
if (callback == null) {
throw new IllegalArgumentException("hooker should not be null!");
}
if (HookBridge.hookMethod(hookMethod, XposedBridge.AdditionalHookInfo.class, priority, callback)) {
return new MethodUnhooker<>() {
@NonNull
@Override
public U getOrigin() {
return hookMethod;
}
@NonNull
@Override
public T getHooker() {
return callback;
}
@Override
public void unhook() {
HookBridge.unhookMethod(hookMethod, callback);
}
};
}
} catch (Throwable t) {
throw new HookFailedException(t);
if (Modifier.isAbstract(hookMethod.getModifiers())) {
throw new IllegalArgumentException("Cannot hook abstract methods: " + hookMethod);
} else if (hookMethod.getDeclaringClass().getClassLoader() == LSPosedContext.class.getClassLoader()) {
throw new IllegalArgumentException("Do not allow hooking inner methods");
} else if (hookMethod.getDeclaringClass() == Method.class && hookMethod.getName().equals("invoke")) {
throw new IllegalArgumentException("Cannot hook Method.invoke");
}
if (callback == null) {
throw new IllegalArgumentException("hooker should not be null!");
}
if (HookBridge.hookMethod(hookMethod, XposedBridge.AdditionalHookInfo.class, priority, callback)) {
return new MethodUnhooker<>() {
@NonNull
@Override
public U getOrigin() {
return hookMethod;
}
@NonNull
@Override
public T getHooker() {
return callback;
}
@Override
public void unhook() {
HookBridge.unhookMethod(hookMethod, callback);
}
};
}
log("Cannot hook " + hookMethod);
throw new HookFailedException("Cannot hook " + hookMethod);
return null;
}
@Override
@NonNull
@Nullable
public MethodUnhooker<BeforeHooker<Method>, Method> hookBefore(@NonNull Method origin, @NonNull BeforeHooker<Method> hooker) {
return doHook(origin, PRIORITY_DEFAULT, hooker);
}
@Override
@NonNull
@Nullable
public MethodUnhooker<AfterHooker<Method>, Method> hookAfter(@NonNull Method origin, @NonNull AfterHooker<Method> hooker) {
return doHook(origin, PRIORITY_DEFAULT, hooker);
}
@Override
@NonNull
@Nullable
public MethodUnhooker<Hooker<Method>, Method> hook(@NonNull Method origin, @NonNull Hooker<Method> hooker) {
return doHook(origin, PRIORITY_DEFAULT, hooker);
}
@Override
@NonNull
@Nullable
public MethodUnhooker<BeforeHooker<Method>, Method> hookBefore(@NonNull Method origin, int priority, @NonNull BeforeHooker<Method> hooker) {
return doHook(origin, priority, hooker);
}
@Override
@NonNull
@Nullable
public MethodUnhooker<AfterHooker<Method>, Method> hookAfter(@NonNull Method origin, int priority, @NonNull AfterHooker<Method> hooker) {
return doHook(origin, priority, hooker);
}
@Override
@NonNull
@Nullable
public MethodUnhooker<Hooker<Method>, Method> hook(@NonNull Method origin, int priority, @NonNull Hooker<Method> hooker) {
return doHook(origin, priority, hooker);
}
@Override
@NonNull
@Nullable
public <T> MethodUnhooker<BeforeHooker<Constructor<T>>, Constructor<T>> hookBefore(@NonNull Constructor<T> origin, @NonNull BeforeHooker<Constructor<T>> hooker) {
return doHook(origin, PRIORITY_DEFAULT, hooker);
}
@Override
@NonNull
@Nullable
public <T> MethodUnhooker<AfterHooker<Constructor<T>>, Constructor<T>> hookAfter(@NonNull Constructor<T> origin, @NonNull AfterHooker<Constructor<T>> hooker) {
return doHook(origin, PRIORITY_DEFAULT, hooker);
}
@Override
@NonNull
@Nullable
public <T> MethodUnhooker<Hooker<Constructor<T>>, Constructor<T>> hook(@NonNull Constructor<T> origin, @NonNull Hooker<Constructor<T>> hooker) {
return doHook(origin, PRIORITY_DEFAULT, hooker);
}
@Override
@NonNull
@Nullable
public <T> MethodUnhooker<BeforeHooker<Constructor<T>>, Constructor<T>> hookBefore(@NonNull Constructor<T> origin, int priority, @NonNull BeforeHooker<Constructor<T>> hooker) {
return doHook(origin, priority, hooker);
}
@Override
@NonNull
@Nullable
public <T> MethodUnhooker<AfterHooker<Constructor<T>>, Constructor<T>> hookAfter(@NonNull Constructor<T> origin, int priority, @NonNull AfterHooker<Constructor<T>> hooker) {
return doHook(origin, priority, hooker);
}
@Override
@NonNull
@Nullable
public <T> MethodUnhooker<Hooker<Constructor<T>>, Constructor<T>> hook(@NonNull Constructor<T> origin, int priority, @NonNull Hooker<Constructor<T>> hooker) {
return doHook(origin, priority, hooker);
}