From ca2e4b8da8b21a60578f290278fbfe5d1e3e3c07 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Fri, 23 Aug 2024 00:41:26 +0800 Subject: [PATCH] Support hooking class static initializer --- .../github/libxposed/api/XposedInterface.java | 32 +++++++++++++++++++ .../libxposed/api/XposedInterfaceWrapper.java | 12 +++++++ 2 files changed, 44 insertions(+) diff --git a/api/src/main/java/io/github/libxposed/api/XposedInterface.java b/api/src/main/java/io/github/libxposed/api/XposedInterface.java index d00ff5a..3c4ac86 100644 --- a/api/src/main/java/io/github/libxposed/api/XposedInterface.java +++ b/api/src/main/java/io/github/libxposed/api/XposedInterface.java @@ -271,6 +271,37 @@ public interface XposedInterface { @NonNull MethodUnhooker hook(@NonNull Method origin, @NonNull Class hooker); + /** + * Hook the static initializer of a class with default priority. + *

+ * Note: If the class is initialized, the hook will never be called. + *

+ * + * @param origin The class to be hooked + * @param hooker The hooker class + * @return Unhooker for canceling the hook + * @throws IllegalArgumentException if class has no static initializer or hooker is invalid + * @throws HookFailedError if hook fails due to framework internal error + */ + @NonNull + MethodUnhooker> hookClassInitializer(@NonNull Class origin, @NonNull Class hooker); + + /** + * Hook the static initializer of a class with specified priority. + *

+ * Note: If the class is initialized, the hook will never be called. + *

+ * + * @param origin The class to be hooked + * @param priority The hook priority + * @param hooker The hooker class + * @return Unhooker for canceling the hook + * @throws IllegalArgumentException if class has no static initializer or hooker is invalid + * @throws HookFailedError if hook fails due to framework internal error + */ + @NonNull + MethodUnhooker> hookClassInitializer(@NonNull Class origin, int priority, @NonNull Class hooker); + /** * Hook a method with specified priority. * @@ -358,6 +389,7 @@ public interface XposedInterface { /** * Basically the same as {@link Constructor#newInstance(Object...)}, but calls the original constructor * as it was before the interception by Xposed. + * * @param constructor The constructor to create and initialize a new instance * @param thisObject The instance to be constructed * @param args The arguments used for the construction diff --git a/api/src/main/java/io/github/libxposed/api/XposedInterfaceWrapper.java b/api/src/main/java/io/github/libxposed/api/XposedInterfaceWrapper.java index be8a4f5..425596f 100644 --- a/api/src/main/java/io/github/libxposed/api/XposedInterfaceWrapper.java +++ b/api/src/main/java/io/github/libxposed/api/XposedInterfaceWrapper.java @@ -55,6 +55,18 @@ public class XposedInterfaceWrapper implements XposedInterface { return mBase.hook(origin, hooker); } + @NonNull + @Override + public MethodUnhooker> hookClassInitializer(@NonNull Class origin, @NonNull Class hooker) { + return mBase.hookClassInitializer(origin, hooker); + } + + @NonNull + @Override + public MethodUnhooker> hookClassInitializer(@NonNull Class origin, int priority, @NonNull Class hooker) { + return mBase.hookClassInitializer(origin, priority, hooker); + } + @NonNull @Override public final MethodUnhooker hook(@NonNull Method origin, int priority, @NonNull Class hooker) {