Skip to content

Commit

Permalink
opt: fuck shit
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed May 6, 2024
1 parent 377ebf7 commit 5198a6c
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import de.robv.android.xposed.*
@Suppress("UNCHECKED_CAST")
object DisableHideGoogle : BaseHook() {
override fun init() {
if (InvokeUtils.invokeStaticField("miui.os.Build", "IS_INTERNATIONAL_BUILD"))
if (InvokeUtils.getStaticField("miui.os.Build", "IS_INTERNATIONAL_BUILD"))
return

XposedHelpers.findAndHookConstructor(
Expand Down
141 changes: 97 additions & 44 deletions app/src/main/java/com/sevtinge/hyperceiler/utils/InvokeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,96 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;

/**
* @author 焕晨HChen
* @noinspection unchecked
* @noinspection unused
*/
public class InvokeUtils {
private static final HashMap<String, Method> methodCache = new HashMap<>();
private static final HashMap<String, Field> fieldCache = new HashMap<>();

private final static String TAG = "invokeUtils";

public static <T> T invokeSetField(Class<?> clz, Object instance, String field, Object value) {
return baseInvokeField(clz, instance, field, value);
// ----------------------------设置字段--------------------------------
// Class 类型
public static <T> T setField(Class<?> clz, Object instance, String field, Object value) {
return baseInvokeField(clz, instance, field, true, value);
}

public static <T> T invokeSetStaticField(Class<?> clz, String field, Object value) {
return baseInvokeField(clz, null, field, value);
public static <T> T setStaticField(Class<?> clz, String field, Object value) {
return baseInvokeField(clz, null, field, true, value);
}

public static <T> T invokeField(Class<?> clz, Object instance, String field) {
return baseInvokeField(clz, instance, field, null);
public static <T> T getField(Class<?> clz, Object instance, String field) {
return baseInvokeField(clz, instance, field, false, null);
}

public static <T> T invokeStaticField(Class<?> clz, String field) {
return baseInvokeField(clz, null, field, null);
public static <T> T getStaticField(Class<?> clz, String field) {
return baseInvokeField(clz, null, field, false, null);
}

public static <T> T invokeMethod(Class<?> clz, Object instance, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(clz, instance, method, param, value);
// String类型
public static <T> T setField(String className, ClassLoader classLoader, Object instance, String field, Object value) {
return baseInvokeField(className, classLoader, instance, field, true, value);
}

public static <T> T invokeStaticMethod(Class<?> clz, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(clz, null, method, param, value);
public static <T> T setStaticField(String className, ClassLoader classLoader, String field, Object value) {
return baseInvokeField(className, classLoader, null, field, true, value);
}

public static <T> T invokeSetField(String className, ClassLoader classLoader, Object instance, String field, Object value) {
return baseInvokeField(className, classLoader, instance, field, value);
public static <T> T getField(String className, ClassLoader classLoader, Object instance, String field) {
return baseInvokeField(className, classLoader, instance, field, false, null);
}

public static <T> T invokeSetStaticField(String className, ClassLoader classLoader, String field, Object value) {
return baseInvokeField(className, classLoader, null, field, value);
public static <T> T getStaticField(String className, ClassLoader classLoader, String field) {
return baseInvokeField(className, classLoader, null, field, false, null);
}

public static <T> T invokeField(String className, ClassLoader classLoader, Object instance, String field) {
return baseInvokeField(className, classLoader, instance, field, null);
// 无 ClassLoader
public static <T> T setField(String className, Object instance, String field, Object value) {
return baseInvokeField(className, instance, field, true, value);
}

public static <T> T invokeStaticField(String className, ClassLoader classLoader, String field) {
return baseInvokeField(className, classLoader, null, field, null);
public static <T> T setStaticField(String className, String field, Object value) {
return baseInvokeField(className, null, field, true, value);
}

public static <T> T invokeMethod(String className, ClassLoader classLoader, Object instance, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(className, classLoader, instance, method, param, value);
public static <T> T getField(String className, Object instance, String field) {
return baseInvokeField(className, instance, field, false, null);
}

public static <T> T invokeStaticMethod(String className, ClassLoader classLoader, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(className, classLoader, null, method, param, value);
public static <T> T getStaticField(String className, String field) {
return baseInvokeField(className, null, field, false, null);
}

public static <T> T invokeSetField(String className, Object instance, String field, Object value) {
return baseInvokeField(className, instance, field, value);
// ----------------------------反射调用方法--------------------------------
// Class 类型
public static <T> T callMethod(Class<?> clz, Object instance, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(clz, instance, method, param, value);
}

public static <T> T invokeSetStaticField(String className, String field, Object value) {
return baseInvokeField(className, null, field, value);
public static <T> T callStaticMethod(Class<?> clz, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(clz, null, method, param, value);
}

public static <T> T invokeField(String className, Object instance, String field) {
return baseInvokeField(className, instance, field, null);
// String类型
public static <T> T callMethod(String className, ClassLoader classLoader, Object instance, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(className, classLoader, instance, method, param, value);
}

public static <T> T invokeStaticField(String className, String field) {
return baseInvokeField(className, null, field, null);
public static <T> T callStaticMethod(String className, ClassLoader classLoader, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(className, classLoader, null, method, param, value);
}

public static <T> T invokeMethod(String className, Object instance, String method, Class<?>[] param, Object... value) {
// 无 ClassLoader
public static <T> T callMethod(String className, Object instance, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(className, instance, method, param, value);
}

public static <T> T invokeStaticMethod(String className, String method, Class<?>[] param, Object... value) {
public static <T> T callStaticMethod(String className, String method, Class<?>[] param, Object... value) {
return baseInvokeMethod(className, null, method, param, value);
}

Expand All @@ -99,9 +112,25 @@ private static <T> T baseInvokeMethod(String className, ClassLoader classLoader,
}
}

/**
* @noinspection unchecked
*/
private static <T> T baseInvokeMethod(Class<?> clz, Object instance, String method, Class<?>[] param, Object... value) {
Method declaredMethod;
try {
Method declaredMethod = clz.getDeclaredMethod(method, param);
declaredMethod = methodCache.get(clz.getName() + "." + method + Arrays.toString(param));
if (declaredMethod == null) {
try {
declaredMethod = clz.getMethod(method, param);
} catch (NoSuchMethodException e) {
try {
declaredMethod = clz.getDeclaredMethod(method, param);
} catch (NoSuchMethodException ex) {
throw new NoSuchMethodException("getMethod: " + e + " getDeclaredMethod: " + ex);
}
}
methodCache.put(clz.getName() + "." + declaredMethod.getName() + Arrays.toString(declaredMethod.getParameterTypes()), declaredMethod);
}
declaredMethod.setAccessible(true);
return (T) declaredMethod.invoke(instance, value);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
Expand All @@ -110,25 +139,41 @@ private static <T> T baseInvokeMethod(Class<?> clz, Object instance, String meth
}
}

private static <T> T baseInvokeField(String className, Object instance, String field, Object value) {
return baseInvokeField(className, null, instance, field, value);
private static <T> T baseInvokeField(String className, Object instance, String field, boolean set, Object value) {
return baseInvokeField(className, null, instance, field, set, value);
}

private static <T> T baseInvokeField(String className, ClassLoader classLoader, Object instance, String field, Object value) {
private static <T> T baseInvokeField(String className, ClassLoader classLoader, Object instance, String field, boolean set, Object value) {
try {
Class<?> clz = baseClass(classLoader, className);
return baseInvokeField(clz, instance, field, value);
return baseInvokeField(clz, instance, field, set, value);
} catch (ClassNotFoundException e) {
AndroidLogUtils.logE(TAG, "Reflection call method failed! class: " + className + " field: " + field, e);
return null;
}
}

private static <T> T baseInvokeField(Class<?> clz, Object instance, String field, Object value) {
/**
* @noinspection unchecked
*/
private static <T> T baseInvokeField(Class<?> clz, Object instance, String field, boolean set, Object value) {
Field declaredField = null;
try {
Field declaredField = clz.getDeclaredField(field);
declaredField = fieldCache.get(clz.getName() + "." + field);
if (declaredField == null) {
try {
declaredField = clz.getField(field);
} catch (NoSuchFieldException e) {
try {
declaredField = clz.getDeclaredField(field);
} catch (NoSuchFieldException ex) {
throw new NoSuchFieldException("getField: " + e + " getDeclaredField: " + ex);
}
}
fieldCache.put(clz.getName() + "." + declaredField.getName(), declaredField);
}
declaredField.setAccessible(true);
if (value != null) {
if (set) {
declaredField.set(instance, value);
return null;
} else
Expand All @@ -141,7 +186,15 @@ private static <T> T baseInvokeField(Class<?> clz, Object instance, String field

private static Class<?> baseClass(ClassLoader classLoader, String className) throws ClassNotFoundException {
if (classLoader == null) {
return Class.forName(className);
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
try {
return ClassLoader.getSystemClassLoader().loadClass(className);
} catch (ClassNotFoundException f) {
throw new ClassNotFoundException("forName: " + e + " loadClass: " + f);
}
}
}
return classLoader.loadClass(className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public static void fitRect(Rect outToResize, int largestSide) {
float maxSize = Math.max(outToResize.width(), outToResize.height());
try {
Rect rectInstance = new Rect(outToResize);
InvokeUtils.invokeMethod("android.graphics.Rect", rectInstance,
InvokeUtils.callMethod("android.graphics.Rect", rectInstance,
"scale", new Class[]{float.class}, largestSide / maxSize);
} catch (Exception e) {
AndroidLogUtils.logE("Call Method scale error: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ public static boolean setProp(String name, Object vale) {

private static String classLoaderMethod(Context context, String name) throws Throwable {
ClassLoader classLoader = context.getClassLoader();
return InvokeUtils.invokeStaticMethod("android.os.SystemProperties", classLoader,
return InvokeUtils.callStaticMethod("android.os.SystemProperties", classLoader,
"get", new Class[]{String.class}, name);
}

/**
* @noinspection unchecked
*/
private static <T> T invokeMethod(Class<?> cls, String str, Class<?>[] clsArr, Object... objArr) throws Throwable {
return InvokeUtils.invokeStaticMethod(cls, str, clsArr, objArr);
return InvokeUtils.callStaticMethod(cls, str, clsArr, objArr);
}
}
Loading

0 comments on commit 5198a6c

Please sign in to comment.