From a43767fa6647953157e5dd26683c46806380e350 Mon Sep 17 00:00:00 2001 From: Nullptr Date: Sat, 14 Jan 2023 22:52:36 +0800 Subject: [PATCH] Add HookFailedError --- .../libxposed/XposedContextWrapper.java | 24 +++---- .../io/github/libxposed/XposedInterface.java | 63 +++++++++++++++---- .../libxposed/errors/HookFailedError.java | 20 ++++++ .../errors/XposedFrameworkError.java | 19 ++++++ 4 files changed, 102 insertions(+), 24 deletions(-) create mode 100644 api/src/main/java/io/github/libxposed/errors/HookFailedError.java create mode 100644 api/src/main/java/io/github/libxposed/errors/XposedFrameworkError.java diff --git a/api/src/main/java/io/github/libxposed/XposedContextWrapper.java b/api/src/main/java/io/github/libxposed/XposedContextWrapper.java index 8e5dd5c..d228ad3 100644 --- a/api/src/main/java/io/github/libxposed/XposedContextWrapper.java +++ b/api/src/main/java/io/github/libxposed/XposedContextWrapper.java @@ -80,73 +80,73 @@ public class XposedContextWrapper extends ContextWrapper implements XposedInterf return getBaseContext().featuredMethod(name, args); } - @Nullable + @NonNull @Override final public MethodUnhooker, Method> hookBefore(@NonNull Method origin, @NonNull BeforeHooker hooker) { return getBaseContext().hookBefore(origin, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker, Method> hookAfter(@NonNull Method origin, @NonNull AfterHooker hooker) { return getBaseContext().hookAfter(origin, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker, Method> hook(@NonNull Method origin, @NonNull Hooker hooker) { return getBaseContext().hook(origin, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker, Method> hookBefore(@NonNull Method origin, int priority, @NonNull BeforeHooker hooker) { return getBaseContext().hookBefore(origin, priority, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker, Method> hookAfter(@NonNull Method origin, int priority, @NonNull AfterHooker hooker) { return getBaseContext().hookAfter(origin, priority, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker, Method> hook(@NonNull Method origin, int priority, @NonNull Hooker hooker) { return getBaseContext().hook(origin, priority, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker>, Constructor> hookBefore(@NonNull Constructor origin, @NonNull BeforeHooker> hooker) { return getBaseContext().hookBefore(origin, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker>, Constructor> hookAfter(@NonNull Constructor origin, @NonNull AfterHooker> hooker) { return getBaseContext().hookAfter(origin, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker>, Constructor> hook(@NonNull Constructor origin, @NonNull Hooker> hooker) { return getBaseContext().hook(origin, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker>, Constructor> hookBefore(@NonNull Constructor origin, int priority, @NonNull BeforeHooker> hooker) { return getBaseContext().hookBefore(origin, priority, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker>, Constructor> hookAfter(@NonNull Constructor origin, int priority, @NonNull AfterHooker> hooker) { return getBaseContext().hookAfter(origin, priority, hooker); } - @Nullable + @NonNull @Override final public MethodUnhooker>, Constructor> hook(@NonNull Constructor origin, int priority, @NonNull Hooker> hooker) { return getBaseContext().hook(origin, priority, hooker); diff --git a/api/src/main/java/io/github/libxposed/XposedInterface.java b/api/src/main/java/io/github/libxposed/XposedInterface.java index 6ca2e9c..b8166cc 100644 --- a/api/src/main/java/io/github/libxposed/XposedInterface.java +++ b/api/src/main/java/io/github/libxposed/XposedInterface.java @@ -16,6 +16,7 @@ import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.util.ConcurrentModificationException; +import io.github.libxposed.errors.HookFailedError; import io.github.libxposed.utils.DexParser; /** @@ -45,6 +46,19 @@ public interface XposedInterface { */ int FRAMEWORK_PRIVILEGE_EMBEDDED = 3; + /** + * The default hook priority. + */ + int PRIORITY_DEFAULT = 50; + /** + * Execute the hook callback late. + */ + int PRIORITY_LOWEST = -10000; + /** + * Execute the hook callback early. + */ + int PRIORITY_HIGHEST = 10000; + /** * The interface Before hook callback. * @@ -343,6 +357,7 @@ public interface XposedInterface { * @param args the args * @return the object */ + @Nullable Object featuredMethod(String name, Object... args); /** @@ -351,8 +366,10 @@ public interface XposedInterface { * @param origin the origin * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker, Method> hookBefore(@NonNull Method origin, @NonNull BeforeHooker hooker); /** @@ -361,8 +378,10 @@ public interface XposedInterface { * @param origin the origin * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker, Method> hookAfter(@NonNull Method origin, @NonNull AfterHooker hooker); /** @@ -371,8 +390,10 @@ public interface XposedInterface { * @param origin the origin * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker, Method> hook(@NonNull Method origin, @NonNull Hooker hooker); /** @@ -382,8 +403,10 @@ public interface XposedInterface { * @param priority the priority * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker, Method> hookBefore(@NonNull Method origin, int priority, @NonNull BeforeHooker hooker); /** @@ -393,8 +416,10 @@ public interface XposedInterface { * @param priority the priority * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker, Method> hookAfter(@NonNull Method origin, int priority, @NonNull AfterHooker hooker); /** @@ -404,8 +429,10 @@ public interface XposedInterface { * @param priority the priority * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker, Method> hook(@NonNull Method origin, int priority, @NonNull Hooker hooker); /** @@ -415,8 +442,10 @@ public interface XposedInterface { * @param origin the origin * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker>, Constructor> hookBefore(@NonNull Constructor origin, @NonNull BeforeHooker> hooker); /** @@ -426,8 +455,10 @@ public interface XposedInterface { * @param origin the origin * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker>, Constructor> hookAfter(@NonNull Constructor origin, @NonNull AfterHooker> hooker); /** @@ -437,8 +468,10 @@ public interface XposedInterface { * @param origin the origin * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker>, Constructor> hook(@NonNull Constructor origin, @NonNull Hooker> hooker); /** @@ -449,8 +482,10 @@ public interface XposedInterface { * @param priority the priority * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker>, Constructor> hookBefore(@NonNull Constructor origin, int priority, @NonNull BeforeHooker> hooker); /** @@ -461,8 +496,10 @@ public interface XposedInterface { * @param priority the priority * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker>, Constructor> hookAfter(@NonNull Constructor origin, int priority, @NonNull AfterHooker> hooker); /** @@ -473,8 +510,10 @@ public interface XposedInterface { * @param priority the priority * @param hooker the hooker * @return the method unhooker + * @throws IllegalArgumentException if origin is abstract, framework internal or {@link Method#invoke} + * @throws HookFailedError if hook fails due to framework internal error */ - @Nullable + @NonNull MethodUnhooker>, Constructor> hook(@NonNull Constructor origin, int priority, @NonNull Hooker> hooker); /** diff --git a/api/src/main/java/io/github/libxposed/errors/HookFailedError.java b/api/src/main/java/io/github/libxposed/errors/HookFailedError.java new file mode 100644 index 0000000..1e155c3 --- /dev/null +++ b/api/src/main/java/io/github/libxposed/errors/HookFailedError.java @@ -0,0 +1,20 @@ +package io.github.libxposed.errors; + +/** + * Thrown to indicate that a hook failed due to framework internal error. + */ +@SuppressWarnings("unused") +public class HookFailedError extends XposedFrameworkError { + + public HookFailedError(String message) { + super(message); + } + + public HookFailedError(String message, Throwable cause) { + super(message, cause); + } + + public HookFailedError(Throwable cause) { + super(cause); + } +} diff --git a/api/src/main/java/io/github/libxposed/errors/XposedFrameworkError.java b/api/src/main/java/io/github/libxposed/errors/XposedFrameworkError.java new file mode 100644 index 0000000..bab03a7 --- /dev/null +++ b/api/src/main/java/io/github/libxposed/errors/XposedFrameworkError.java @@ -0,0 +1,19 @@ +package io.github.libxposed.errors; + +/** + * Thrown to indicate that the Xposed framework function is broken. + */ +public abstract class XposedFrameworkError extends Error { + + public XposedFrameworkError(String message) { + super(message); + } + + public XposedFrameworkError(String message, Throwable cause) { + super(message, cause); + } + + public XposedFrameworkError(Throwable cause) { + super(cause); + } +}