diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java index e35fe22..181e190 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java @@ -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; diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java index 2e82b94..3c547e4 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java @@ -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; }