Skip to content

Commit ca2e4b8

Browse files
committed
Support hooking class static initializer
1 parent 3248419 commit ca2e4b8

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

api/src/main/java/io/github/libxposed/api/XposedInterface.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,37 @@ interface MethodUnhooker<T> {
271271
@NonNull
272272
MethodUnhooker<Method> hook(@NonNull Method origin, @NonNull Class<? extends Hooker> hooker);
273273

274+
/**
275+
* Hook the static initializer of a class with default priority.
276+
* <p>
277+
* Note: If the class is initialized, the hook will never be called.
278+
* </p>
279+
*
280+
* @param origin The class to be hooked
281+
* @param hooker The hooker class
282+
* @return Unhooker for canceling the hook
283+
* @throws IllegalArgumentException if class has no static initializer or hooker is invalid
284+
* @throws HookFailedError if hook fails due to framework internal error
285+
*/
286+
@NonNull
287+
<T> MethodUnhooker<Constructor<T>> hookClassInitializer(@NonNull Class<T> origin, @NonNull Class<? extends Hooker> hooker);
288+
289+
/**
290+
* Hook the static initializer of a class with specified priority.
291+
* <p>
292+
* Note: If the class is initialized, the hook will never be called.
293+
* </p>
294+
*
295+
* @param origin The class to be hooked
296+
* @param priority The hook priority
297+
* @param hooker The hooker class
298+
* @return Unhooker for canceling the hook
299+
* @throws IllegalArgumentException if class has no static initializer or hooker is invalid
300+
* @throws HookFailedError if hook fails due to framework internal error
301+
*/
302+
@NonNull
303+
<T> MethodUnhooker<Constructor<T>> hookClassInitializer(@NonNull Class<T> origin, int priority, @NonNull Class<? extends Hooker> hooker);
304+
274305
/**
275306
* Hook a method with specified priority.
276307
*
@@ -358,6 +389,7 @@ interface MethodUnhooker<T> {
358389
/**
359390
* Basically the same as {@link Constructor#newInstance(Object...)}, but calls the original constructor
360391
* as it was before the interception by Xposed.
392+
*
361393
* @param constructor The constructor to create and initialize a new instance
362394
* @param thisObject The instance to be constructed
363395
* @param args The arguments used for the construction

api/src/main/java/io/github/libxposed/api/XposedInterfaceWrapper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ public final MethodUnhooker<Method> hook(@NonNull Method origin, @NonNull Class<
5555
return mBase.hook(origin, hooker);
5656
}
5757

58+
@NonNull
59+
@Override
60+
public <T> MethodUnhooker<Constructor<T>> hookClassInitializer(@NonNull Class<T> origin, @NonNull Class<? extends Hooker> hooker) {
61+
return mBase.hookClassInitializer(origin, hooker);
62+
}
63+
64+
@NonNull
65+
@Override
66+
public <T> MethodUnhooker<Constructor<T>> hookClassInitializer(@NonNull Class<T> origin, int priority, @NonNull Class<? extends Hooker> hooker) {
67+
return mBase.hookClassInitializer(origin, priority, hooker);
68+
}
69+
5870
@NonNull
5971
@Override
6072
public final MethodUnhooker<Method> hook(@NonNull Method origin, int priority, @NonNull Class<? extends Hooker> hooker) {

0 commit comments

Comments
 (0)