mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 12:20:09 +08:00
search this reference properly
This commit is contained in:
parent
8a8065e179
commit
d14c570c96
@ -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;
|
||||
|
@ -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) {
|
||||
currentLogLevel = enable ? LogLevel.LEVEL_DIAGNOSE : LogLevel.LEVEL_MUTE;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user