package com.hook.game.demo.hook;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
/**
* @author Pudding
* @content
* @time 2018/7/9
*/
public class MethodUtils {
private static Map<String, Method> sMethodCache = new HashMap<String, Method>();
private static String getKey(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) {
StringBuilder sb = new StringBuilder();
sb.append(cls.toString()).append("#").append(methodName);
if (parameterTypes != null && parameterTypes.length > 0) {
for (Class<?> parameterType : parameterTypes) {
sb.append(parameterType.toString()).append("#");
}
} else {
sb.append(Void.class.toString());
}
return sb.toString();
}
private static Method getAccessibleMethodFromSuperclass(final Class<?> cls,
final String methodName, final Class<?>... parameterTypes) {
Class<?> parentClass = cls.getSuperclass();
while (parentClass != null) {
if (Modifier.isPublic(parentClass.getModifiers())) {
try {
return parentClass.getMethod(methodName, parameterTypes);
} catch (final NoSuchMethodException e) {
return null;
}
}
parentClass = parentClass.getSuperclass();
}
return null;
}
private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls,
final String methodName, final Class<?>... parameterTypes) {
// Search up the superclass chain
for (; cls != null; cls = cls.getSuperclass()) {
// Check the implemented interfaces of the parent class
final Class<?>[] interfaces = cls.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
// Is this interface public?
if (!Modifier.isPublic(interfaces[i].getModifiers())) {
continue;
}
// Does the method exist on this interface?
try {
return interfaces[i].getDeclaredMethod(methodName,
parameterTypes);
} catch (final NoSuchMethodException e) { // NOPMD
/*
* Swallow, if no method is found after the loop then this
* method returns null.
*/
}
// Recursively check our parent interfaces
Method method = getAccessibleMethodFromInterfaceNest(interfaces[i],
methodName, parameterTypes);
if (method != null) {
return method;
}
}
}
return null;
}
private static Method getAccessibleMethod(Method method) {
if (!MemberUtils.isAccessible(method)) {
return null;
}
// If the declaring class is public, we are done
final Class<?> cls = method.getDeclaringClass();
if (Modifier.isPublic(cls.getModifiers())) {
return method;
}
final String methodName = method.getName();
final Class<?>[] parameterTypes = method.getParameterTypes();
// Check the implemented interfaces and subinterfaces
method = getAccessibleMethodFromInterfaceNest(cls, methodName,
parameterTypes);
// Check the superclass chain
if (method == null) {
method = getAccessibleMethodFromSuperclass(cls, methodName,
parameterTypes);
}
return method;
}
public static Method getAccessibleMethod(final Class<?> cls, final String methodName,
final Class<?>... parameterTypes) throws NoSuchMethodException {
String key = getKey(cls, methodName, parameterTypes);
Method method;
synchronized (sMethodCache) {
method = sMethodCache.get(key);
}
if (method != null) {
if (!method.isAccessible()) {
method.setAccessible(true);
}
return method;
}
Method accessibleMethod = getAccessibleMethod(cls.getMethod(methodName,
parameterTypes));
synchronized (sMethodCache) {
sMethodCache.put(key, accessibleMethod);
}
return accessibleMethod;
}
private static Method getMatchingAccessibleMethod(final Class<?> cls,
final String methodName, final Class<?>... parameterTypes) {
String key = getKey(cls, methodName, parameterTypes);
Method cachedMethod;
synchronized (sMethodCache) {
cachedMethod = sMethodCache.get(key);
}
if (cachedMethod != null) {
if (!cachedMethod.isAccessible()) {
cachedMethod.setAccessible(true);
}
return cachedMethod;
}
try {
final Method method = cls.getMethod(methodName, parameterTypes);
MemberUtils.setAccessibleWorkaround(method);
synchronized (sMethodCache) {
sMethodCache.put(key, method);
}
return method;
} catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception
}
// search through all methods
Method bestMatch = null;
final Method[] methods = cls.getMethods();
for (final Method method : methods) {
// compare name and parameters
if (method.getName().equals(methodName) && MemberUtils.isAssignable(parameterTypes, method.getParameterTypes(), true)) {
// get accessible version of method
final Method accessibleMethod = getAccessibleMethod(method);
if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareParameterTypes(
accessibleMethod.getParameterTypes(),
bestMatch.getParameterTypes(),
parameterTypes) < 0)) {
bestMatch = accessibleMethod;
}
}
}
if (bestMatch != null) {
MemberUtils.setAccessibleWorkaround(bestMatch);
}
synchronized (sMethodCache) {
sMethodCache.put(key, bestMatch);
}
return bestMatch;
}
public static Object invokeMethod(final Object object, final String methodName,
Object[] args, Class<?>[] parameterTypes)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
parameterTypes = Utils.nullToEmpty(parameterTypes);
args = Utils.nullToEmpty(args);
final Method method = getMatchingAccessibleMethod(object.getClass(),
methodName, parameterTypes);
if (method == null) {
throw new NoSuchMethodException("No such accessible method: "
+ methodName + "() on object: "
+ object.getClass().getName());
}
return method.invoke(object, args);
}
public static Object invokeStaticMethod(final Class clazz, final String methodName,
Object[] args, Class<?>[] parameterTypes)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
parameterTy
Android Hook 技术之 绕过系统对Activity验证
需积分: 34 192 浏览量
2018-07-18
18:08:22
上传
评论
收藏 606KB ZIP 举报
PuddingGuo
- 粉丝: 48
- 资源: 29
最新资源
- 基于yolov5识别算法实现的DNF自动脚本源码.zip
- 基于Python实现的自动化办公项目.zip
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈