Skip to content

Commit

Permalink
修复无法链式调用的问题 & 优化方法名 & 修复类型转换错误
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed Oct 3, 2024
1 parent c353fcf commit e19a0ac
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/hchen/hooktool/ToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void initZygote(IXposedHookZygoteInit.StartupParam startupParam) {

@Override
public void init() {

// 原用法
new XC_MethodHook() {
@Override
Expand Down Expand Up @@ -78,7 +78,7 @@ public void after() {
.returnResult(false)
);

hook("com.hchen.demo", "test", new IAction() {
hookMethod("com.hchen.demo", "test", new IAction() {
@Override
public void before() {
super.before();
Expand Down Expand Up @@ -126,4 +126,4 @@ public void before() {
// 设置 pkg 的 string 资源 test_res_str 值为 HC!
setObjectReplacement("com.hchen.demo", "string", "test_res_str", "HC!");
}
}
}
23 changes: 13 additions & 10 deletions app/src/main/java/com/hchen/hooktool/tool/ChainTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,18 @@ private void doFind(Class<?> clazz) {
String UUID = cacheData.mType + "#" + clazz.getName() + "#" + cacheData.mName + "#" + Arrays.toString(cacheData.mParams);
switch (cacheData.mType) {
case TYPE_METHOD -> members.add(new ChainData(
findMethod(clazz, cacheData.mName, arrayToClass(
classLoader == null ? ToolData.classLoader : classLoader,
cacheData.mParams))));
findMethod(clazz, cacheData.mName, arrayToClass(
classLoader == null ? ToolData.classLoader : classLoader,
cacheData.mParams))));
case TYPE_CONSTRUCTOR -> members.add(new ChainData(
findConstructor(clazz, arrayToClass(
classLoader == null ? ToolData.classLoader : classLoader,
cacheData.mParams))));
case TYPE_ANY_METHOD -> members.addAll(CoreTool.findAllMethod(clazz, cacheData.mName).stream().map(
findConstructor(clazz, arrayToClass(
classLoader == null ? ToolData.classLoader : classLoader,
cacheData.mParams))));
case TYPE_ANY_METHOD ->
members.addAll(CoreTool.findAllMethod(clazz, cacheData.mName).stream().map(
ChainData::new).collect(Collectors.toCollection(ArrayList::new)));
case TYPE_ANY_CONSTRUCTOR -> members.addAll(CoreTool.findAllConstructor(clazz).stream().map(
case TYPE_ANY_CONSTRUCTOR ->
members.addAll(CoreTool.findAllConstructor(clazz).stream().map(
ChainData::new).collect(Collectors.toCollection(ArrayList::new)));
default -> {
logW(tag(), "Unknown type: " + cacheData.mType + getStackTrace());
Expand All @@ -146,13 +148,14 @@ private void doFind(Class<?> clazz) {
if (memberData.member == null || existingMembers.contains(memberData.member)) {
iterator.remove();
logW(tag(), "This member maybe repeated or maybe is null, will remove it! " +
"\ndebug: " + UUID + "#member: " + memberData.member);
"\ndebug: " + UUID + "#member: " + memberData.member);
continue;
}
existingMembers.add(memberData.member);
}
if (members.isEmpty()) continue;
chainDataList.add(new ChainData(members, cacheData.iAction, HookState.NONE, UUID));
ArrayList<ChainData> finalMembers = new ArrayList<>(members);
chainDataList.add(new ChainData(finalMembers, cacheData.iAction, HookState.NONE, UUID));
} else
logW(tag(), "This member maybe repeated, will skip add it! \ndebug: " + UUID);
members.clear();
Expand Down
96 changes: 48 additions & 48 deletions app/src/main/java/com/hchen/hooktool/tool/CoreTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static Class<?> findClass(String name) {

public static Class<?> findClass(String name, ClassLoader classLoader) {
return run(() -> XposedHelpers.findClass(name, classLoader))
.orErrMag(null, tag(), "Failed to find class!");
.orErrMag(null, tag(), "Failed to find class!");
}

//------------ 检查指定方法是否存在 --------------
Expand Down Expand Up @@ -127,7 +127,7 @@ public static Method findMethod(Class<?> clazz, ClassLoader classLoader, String

public static Method findMethod(Class<?> clazz, String name, Class<?>... classes) {
return run(() -> XposedHelpers.findMethodExact(clazz, name, classes))
.orErrMag(null, tag(), "Failed to find method!");
.orErrMag(null, tag(), "Failed to find method!");
}

public static ArrayList<Method> findAllMethod(String clazz, String name) {
Expand Down Expand Up @@ -179,7 +179,7 @@ public static Constructor<?> findConstructor(Class<?> clazz, ClassLoader classLo

public static Constructor<?> findConstructor(Class<?> clazz, Class<?>... classes) {
return run(() -> XposedHelpers.findConstructorExact(clazz, classes))
.orErrMag(null, tag(), "Failed to find constructor!");
.orErrMag(null, tag(), "Failed to find constructor!");
}

public static ArrayList<Constructor<?>> findAllConstructor(String clazz) {
Expand Down Expand Up @@ -218,65 +218,65 @@ public static Field findField(String clazz, ClassLoader classLoader, String name

public static Field findField(Class<?> clazz, String name) {
return run(() -> XposedHelpers.findField(clazz, name))
.orErrMag(null, tag(), "Failed to find field!");
.orErrMag(null, tag(), "Failed to find field!");
}

// --------- 执行 hook -----------
// --------- 普通方法 -------------
public static UnHook hook(String clazz, String method, Object... params) {
return hook(clazz, ToolData.classLoader, method, params);
public static UnHook hookMethod(String clazz, String method, Object... params) {
return hookMethod(clazz, ToolData.classLoader, method, params);
}

public static UnHook hook(String clazz, ClassLoader classLoader, String method, Object... params) {
return hook(findClass(clazz, classLoader), method, toClassAsIAction(classLoader, params));
public static UnHook hookMethod(String clazz, ClassLoader classLoader, String method, Object... params) {
return hookMethod(findClass(clazz, classLoader), method, toClassAsIAction(classLoader, params));
}

public static UnHook hook(Class<?> clazz, ClassLoader classLoader, String method, Object... params) {
return hook(clazz, method, toClassAsIAction(classLoader, params));
public static UnHook hookMethod(Class<?> clazz, ClassLoader classLoader, String method, Object... params) {
return hookMethod(clazz, method, toClassAsIAction(classLoader, params));
}

public static UnHook hook(Class<?> clazz, String method, Object... params) {
public static UnHook hookMethod(Class<?> clazz, String method, Object... params) {
return hook(clazz, method, HookType.METHOD, params);
}

public static UnHookList hookAll(String clazz, String method, IAction iAction) {
return hookAll(findClass(clazz), method, iAction);
public static UnHookList hookAllMethod(String clazz, String method, IAction iAction) {
return hookAllMethod(findClass(clazz), method, iAction);
}

public static UnHookList hookAll(String clazz, ClassLoader classLoader, String method, IAction iAction) {
return hookAll(findClass(clazz, classLoader), method, iAction);
public static UnHookList hookAllMethod(String clazz, ClassLoader classLoader, String method, IAction iAction) {
return hookAllMethod(findClass(clazz, classLoader), method, iAction);
}

public static UnHookList hookAll(Class<?> clazz, String method, IAction iAction) {
public static UnHookList hookAllMethod(Class<?> clazz, String method, IAction iAction) {
return hookAll(findAllMethod(clazz, method), iAction);
}

// --------- 构造函数 ------------
public static UnHook hook(String clazz, Object... params) {
return hook(findClass(clazz), params);
public static UnHook hookConstructor(String clazz, Object... params) {
return hookConstructor(findClass(clazz), params);
}

public static UnHook hook(String clazz, ClassLoader classLoader, Object... params) {
return hook(findClass(clazz, classLoader), toClassAsIAction(classLoader, params));
public static UnHook hookConstructor(String clazz, ClassLoader classLoader, Object... params) {
return hookConstructor(findClass(clazz, classLoader), toClassAsIAction(classLoader, params));
}

public static UnHook hook(Class<?> clazz, ClassLoader classLoader, Object... params) {
return hook(clazz, toClassAsIAction(classLoader, params));
public static UnHook hookConstructor(Class<?> clazz, ClassLoader classLoader, Object... params) {
return hookConstructor(clazz, toClassAsIAction(classLoader, params));
}

public static UnHook hook(Class<?> clazz, Object... params) {
public static UnHook hookConstructor(Class<?> clazz, Object... params) {
return hook(clazz, null, HookType.CONSTRUCTOR, params);
}

public static UnHookList hookAll(String clazz, IAction iAction) {
public static UnHookList hookAllConstructor(String clazz, IAction iAction) {
return hookAll(findAllConstructor(clazz), iAction);
}

public static UnHookList hookAll(String clazz, ClassLoader classLoader, IAction iAction) {
public static UnHookList hookAllConstructor(String clazz, ClassLoader classLoader, IAction iAction) {
return hookAll(findAllConstructor(clazz, classLoader), iAction);
}

public static UnHookList hookAll(Class<?> clazz, IAction iAction) {
public static UnHookList hookAllConstructor(Class<?> clazz, IAction iAction) {
return hookAll(findAllConstructor(clazz), iAction);
}

Expand All @@ -287,24 +287,24 @@ private enum HookType {
}

private static UnHook hook(Class<?> clazz, String method, HookType hookType, Object... params) {
String debug = hookType.toString() + "#" + clazz.getName() + "#" + method + "#" + Arrays.toString(params);
String debug = hookType.toString() + "#" + (clazz == null ? "null" : clazz.getName()) + "#" + method + "#" + Arrays.toString(params);
if (params == null || params.length == 0 || !(params[params.length - 1] instanceof IAction iAction)) {
logW(tag(), "Params is null or length is 0 or last param not is IAction! \ndebug: " + debug + getStackTrace());
return null;
}

return run(() -> {
Class<?>[] classes = Arrays.stream(params)
.limit(params.length - 1)
.map(o -> {
if (o instanceof String s) {
Class<?> c = findClass(s);
if (c == null)
throw new RuntimeException("Found class is null, stop to hook!");
return c;
} else if (o instanceof Class<?> c) return c;
else throw new RuntimeException("Unknown type: " + o);
}).toArray(Class<?>[]::new);
.limit(params.length - 1)
.map(o -> {
if (o instanceof String s) {
Class<?> c = findClass(s);
if (c == null)
throw new RuntimeException("Found class is null, stop to hook!");
return c;
} else if (o instanceof Class<?> c) return c;
else throw new RuntimeException("Unknown type: " + o);
}).toArray(Class<?>[]::new);
Member member = null;
switch (hookType) {
case METHOD -> member = findMethod(clazz, method, classes);
Expand Down Expand Up @@ -473,12 +473,12 @@ public static long timeConsumption(Runnable runnable) {
// ---------- 非静态 -----------
public static <T> T callMethod(Object instance, String name, Object... objs) {
return run(() -> (T) XposedHelpers.callMethod(instance, name, objs))
.orErrMag(null, tag(), "Failed to call method!");
.orErrMag(null, tag(), "Failed to call method!");
}

public static <T> T getField(Object instance, String name) {
return run(() -> (T) XposedHelpers.getObjectField(instance, name))
.orErrMag(null, tag(), "Failed to get field!");
.orErrMag(null, tag(), "Failed to get field!");
}

public static <T> T getField(Object instance, Field field) {
Expand All @@ -505,23 +505,23 @@ public static boolean setField(Object instance, Field field, Object value) {

public static <T> T setAdditionalInstanceField(Object instance, String key, Object value) {
return run(() -> (T) XposedHelpers.setAdditionalInstanceField(instance, key, value))
.orErrMag(null, tag(), "Failed to set additional instance!");
.orErrMag(null, tag(), "Failed to set additional instance!");
}

public static <T> T getAdditionalInstanceField(Object instance, String key) {
return run(() -> (T) XposedHelpers.getAdditionalInstanceField(instance, key))
.orErrMag(null, tag(), "Failed to get additional instance!");
.orErrMag(null, tag(), "Failed to get additional instance!");
}

public static <T> T removeAdditionalInstanceField(Object instance, String key) {
return run(() -> (T) XposedHelpers.removeAdditionalInstanceField(instance, key))
.orErrMag(null, tag(), "Failed to remove additional instance!");
.orErrMag(null, tag(), "Failed to remove additional instance!");
}

// ---------- 静态 ------------
public static <T> T newInstance(Class<?> clz, Object... objects) {
return run(() -> (T) XposedHelpers.newInstance(clz, objects))
.orErrMag(null, tag(), "Failed to create new instance!");
.orErrMag(null, tag(), "Failed to create new instance!");
}

public static <T> T newInstance(String clz, Object... objects) {
Expand All @@ -534,7 +534,7 @@ public static <T> T newInstance(String clz, ClassLoader classLoader, Object... o

public static <T> T callStaticMethod(Class<?> clz, String name, Object... objs) {
return run(() -> (T) XposedHelpers.callStaticMethod(clz, name, objs))
.orErrMag(null, tag(), "Failed to call static method!");
.orErrMag(null, tag(), "Failed to call static method!");
}

public static <T> T callStaticMethod(String clz, String name, Object... objs) {
Expand All @@ -554,7 +554,7 @@ public static <T> T callStaticMethod(Method method, Object... objs) {

public static <T> T getStaticField(Class<?> clz, String name) {
return run(() -> (T) XposedHelpers.getStaticObjectField(clz, name))
.orErrMag(null, tag(), "Failed to get static field!");
.orErrMag(null, tag(), "Failed to get static field!");
}

public static <T> T getStaticField(String clz, String name) {
Expand Down Expand Up @@ -597,17 +597,17 @@ public static boolean setStaticField(String clz, ClassLoader classLoader, String

public static <T> T setAdditionalStaticField(Class<?> clz, String key, Object value) {
return run(() -> (T) XposedHelpers.setAdditionalStaticField(clz, key, value))
.orErrMag(null, tag(), "Failed to set static additional instance!");
.orErrMag(null, tag(), "Failed to set static additional instance!");
}

public static <T> T getAdditionalStaticField(Class<?> clz, String key) {
return run(() -> (T) XposedHelpers.getAdditionalStaticField(clz, key))
.orErrMag(null, tag(), "Failed to get static additional instance!");
.orErrMag(null, tag(), "Failed to get static additional instance!");
}

public static <T> T removeAdditionalStaticField(Class<?> clz, String key) {
return run(() -> (T) XposedHelpers.removeAdditionalStaticField(clz, key))
.orErrMag(null, tag(), "Failed to remove static additional instance!");
.orErrMag(null, tag(), "Failed to remove static additional instance!");
}

public static <T> T setAdditionalStaticField(String clz, String key, Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,11 @@ private static void applyHooks() {
}

private static void hookResMethod(String name, Object... args) {
unhooks.add(CoreTool.hook(Resources.class, name, args));
unhooks.add(CoreTool.hookMethod(Resources.class, name, args));
}

private static void hookTypedMethod(String name, Object... args) {
unhooks.add(CoreTool.hook(TypedArray.class, name, args));
unhooks.add(CoreTool.hookMethod(TypedArray.class, name, args));
}

public static void unHookRes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

/**
* 动作
*
*
* @author 焕晨HChen
*/
public class ActAchieve {
Expand Down Expand Up @@ -131,15 +131,15 @@ final public boolean setThisField(String name, Object value) {
}

// ---------- 设置自定义字段 --------------
final public boolean setThisAdditionalInstanceField(String key, Object value) {
final public <T> T setThisAdditionalInstanceField(String key, Object value) {
return setAdditionalInstanceField(MethodHookParam.thisObject, key, value);
}

final public <T> T getThisAdditionalInstanceField(String key) {
return getAdditionalInstanceField(MethodHookParam.thisObject, key);
}

final public boolean removeThisAdditionalInstanceField(String key) {
final public <T> T removeThisAdditionalInstanceField(String key) {
return removeAdditionalInstanceField(MethodHookParam.thisObject, key);
}
}

0 comments on commit e19a0ac

Please sign in to comment.