search this reference properly

This commit is contained in:
金戟 2020-12-27 13:11:26 +08:00
parent 8a8065e179
commit d14c570c96
2 changed files with 23 additions and 4 deletions

View File

@ -56,17 +56,28 @@ public class TestClassHandler extends BaseClassHandler {
mn.access |= ACC_PUBLIC; mn.access |= ACC_PUBLIC;
if ((mn.access & ACC_STATIC) == 0) { if ((mn.access & ACC_STATIC) == 0) {
mn.access |= ACC_STATIC; mn.access |= ACC_STATIC;
// remote `this` reference // remove `this` reference
mn.localVariables.remove(0); LocalVariableNode thisRef = null;
for (LocalVariableNode vn : mn.localVariables) { for (LocalVariableNode vn : mn.localVariables) {
vn.index--; if (vn.index == 0) {
thisRef = vn;
} else {
vn.index--;
}
}
if (thisRef != null) {
mn.localVariables.remove(thisRef);
} else {
LogUtil.error("Fail to find `this` reference in none-static method " + getName(cn, mn));
return;
} }
for (AbstractInsnNode in : mn.instructions) { for (AbstractInsnNode in : mn.instructions) {
if (in.getOpcode() >= ILOAD && in.getOpcode() <= SASTORE && in instanceof VarInsnNode) { if (in.getOpcode() >= ILOAD && in.getOpcode() <= SASTORE && in instanceof VarInsnNode) {
if (((VarInsnNode)in).var > 0) { if (((VarInsnNode)in).var > 0) {
((VarInsnNode)in).var--; ((VarInsnNode)in).var--;
} else if (in.getOpcode() == ALOAD) { } else if (in.getOpcode() == ALOAD) {
LogUtil.warn("Attempt to access none-static member in mock method !"); LogUtil.error("Attempt to access none-static member in mock method " + getName(cn, mn));
return;
} }
} }
} }
@ -74,6 +85,10 @@ public class TestClassHandler extends BaseClassHandler {
} }
} }
private String getName(ClassNode cn, MethodNode mn) {
return cn.name + ":" + mn.name;
}
private boolean isMockMethod(MethodNode mn) { private boolean isMockMethod(MethodNode mn) {
if (mn.visibleAnnotations == null) { if (mn.visibleAnnotations == null) {
return false; return false;

View File

@ -50,6 +50,10 @@ public class LogUtil {
} }
} }
public static void error(String msg, Object... args) {
System.err.println(String.format("[FAIL] " + msg, args));
}
public static void enableDiagnose(boolean enable) { public static void enableDiagnose(boolean enable) {
currentLogLevel = enable ? LogLevel.LEVEL_DIAGNOSE : LogLevel.LEVEL_MUTE; currentLogLevel = enable ? LogLevel.LEVEL_DIAGNOSE : LogLevel.LEVEL_MUTE;
} }