Commit 92f2647b authored by swift_gan's avatar swift_gan

fix hook for ctor

parent 3be93512
...@@ -2,6 +2,8 @@ package com.swift.sandhook.wrapper; ...@@ -2,6 +2,8 @@ package com.swift.sandhook.wrapper;
import com.swift.sandhook.SandHook; import com.swift.sandhook.SandHook;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.HashMap; import java.util.HashMap;
...@@ -19,16 +21,16 @@ public class HookWrapper { ...@@ -19,16 +21,16 @@ public class HookWrapper {
Class targetHookClass = getTargetHookClass(clazz); Class targetHookClass = getTargetHookClass(clazz);
if (targetHookClass == null) if (targetHookClass == null)
throw new HookErrorException("error hook wrapper class :" + clazz.getName()); throw new HookErrorException("error hook wrapper class :" + clazz.getName());
Map<Method,HookEntity> hookEntityMap = getHookMethods(targetHookClass, clazz); Map<Member,HookEntity> hookEntityMap = getHookMethods(targetHookClass, clazz);
for (HookEntity entity:hookEntityMap.values()) { for (HookEntity entity:hookEntityMap.values()) {
if (entity.target != null && entity.hook != null) { if (entity.target != null && entity.hook != null) {
SandHook.hook(entity.target, entity.hook, entity.backup);
} }
} }
} }
private static Map<Method, HookEntity> getHookMethods(Class targetHookClass, Class<?> hookWrapperClass) throws HookErrorException { private static Map<Member, HookEntity> getHookMethods(Class targetHookClass, Class<?> hookWrapperClass) throws HookErrorException {
Map<Method,HookEntity> hookEntityMap = new HashMap<>(); Map<Member,HookEntity> hookEntityMap = new HashMap<>();
Method[] methods = hookWrapperClass.getDeclaredMethods(); Method[] methods = hookWrapperClass.getDeclaredMethods();
if (methods == null && methods.length == 0) if (methods == null && methods.length == 0)
throw new HookErrorException("error hook wrapper class :" + targetHookClass.getName()); throw new HookErrorException("error hook wrapper class :" + targetHookClass.getName());
...@@ -36,13 +38,17 @@ public class HookWrapper { ...@@ -36,13 +38,17 @@ public class HookWrapper {
HookMethod hookMethodAnno = method.getAnnotation(HookMethod.class); HookMethod hookMethodAnno = method.getAnnotation(HookMethod.class);
HookMethodBackup hookMethodBackupAnno = method.getAnnotation(HookMethodBackup.class); HookMethodBackup hookMethodBackupAnno = method.getAnnotation(HookMethodBackup.class);
String methodName; String methodName;
Method foundMethod; Member foundMethod;
Class[] pars; Class[] pars;
if (hookMethodAnno != null) { if (hookMethodAnno != null) {
methodName = hookMethodAnno.value(); methodName = hookMethodAnno.value();
pars = parseMethodPars(method); pars = parseMethodPars(method);
try { try {
if (methodName.equals("<init>")) {
foundMethod = targetHookClass.getConstructor(pars);
} else {
foundMethod = targetHookClass.getDeclaredMethod(methodName, pars); foundMethod = targetHookClass.getDeclaredMethod(methodName, pars);
}
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new HookErrorException("can not find target method: " + methodName, e); throw new HookErrorException("can not find target method: " + methodName, e);
} }
...@@ -57,7 +63,11 @@ public class HookWrapper { ...@@ -57,7 +63,11 @@ public class HookWrapper {
methodName = hookMethodBackupAnno.value(); methodName = hookMethodBackupAnno.value();
pars = parseMethodPars(method); pars = parseMethodPars(method);
try { try {
if (methodName.equals("<init>")) {
foundMethod = targetHookClass.getConstructor(pars);
} else {
foundMethod = targetHookClass.getDeclaredMethod(methodName, pars); foundMethod = targetHookClass.getDeclaredMethod(methodName, pars);
}
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new HookErrorException("can not find target method: " + methodName, e); throw new HookErrorException("can not find target method: " + methodName, e);
} }
...@@ -116,11 +126,17 @@ public class HookWrapper { ...@@ -116,11 +126,17 @@ public class HookWrapper {
} }
} }
public static void checkSignature(Method origin, Method fake, Class[] originPars) throws HookErrorException { public static void checkSignature(Member origin, Method fake, Class[] originPars) throws HookErrorException {
if (!Modifier.isStatic(fake.getModifiers())) if (!Modifier.isStatic(fake.getModifiers()))
throw new HookErrorException("hook method must static! - " + fake.getName()); throw new HookErrorException("hook method must static! - " + fake.getName());
if (origin.getReturnType() != fake.getReturnType() && !origin.getReturnType().isAssignableFrom(origin.getReturnType())) if (origin instanceof Constructor) {
if (!fake.getReturnType().equals(Void.TYPE))
throw new HookErrorException("error return type! - " + fake.getName()); throw new HookErrorException("error return type! - " + fake.getName());
} else if (origin instanceof Method) {
Class originRet = ((Method)origin).getReturnType();
if (originRet != fake.getReturnType() && !originRet.isAssignableFrom(originRet))
throw new HookErrorException("error return type! - " + fake.getName());
}
Class[] fakePars = fake.getParameterTypes(); Class[] fakePars = fake.getParameterTypes();
if (fakePars == null) if (fakePars == null)
fakePars = new Class[0]; fakePars = new Class[0];
...@@ -149,13 +165,18 @@ public class HookWrapper { ...@@ -149,13 +165,18 @@ public class HookWrapper {
public static class HookEntity { public static class HookEntity {
public Method target; public Member target;
public Method hook; public Method hook;
public Method backup; public Method backup;
public HookEntity(Method target) { public HookEntity(Member target) {
this.target = target; this.target = target;
} }
public boolean isCtor() {
return target instanceof Constructor;
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment