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;
if ((mn.access & ACC_STATIC) == 0) {
mn.access |= ACC_STATIC;
// remote `this` reference
mn.localVariables.remove(0);
// remove `this` reference
LocalVariableNode thisRef = null;
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) {
if (in.getOpcode() >= ILOAD && in.getOpcode() <= SASTORE && in instanceof VarInsnNode) {
if (((VarInsnNode)in).var > 0) {
((VarInsnNode)in).var--;
} 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) {
if (mn.visibleAnnotations == null) {
return false;