From 126c7257d84e2723d97958f15ee4eaa2e2b575a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 6 Feb 2021 10:44:51 +0800 Subject: [PATCH] fix out of index exception when mocking native method --- .../testable/agent/handler/SourceClassHandler.java | 4 ++++ .../agent/transformer/TestableClassTransformer.java | 8 ++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index 8b8374b..e5a9e29 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -60,6 +60,10 @@ public class SourceClassHandler extends BaseClassHandler { Set newOperatorInjectMethods) { LogUtil.diagnose(" Handling method %s", mn.name); AbstractInsnNode[] instructions = mn.instructions.toArray(); + if (instructions.length == 0) { + // native method (issue-52) + return; + } int i = 0; int maxStackDiff = 0; do { diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index 46d8799..e36ab19 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -61,17 +61,17 @@ public class TestableClassTransformer implements ClassFileTransformer { LogUtil.diagnose("Handling source class %s", className); bytes = new SourceClassHandler(injectMethods).getBytes(classFileBuffer); dumpByte(className, bytes); - resetMockContext(); } else if (shouldTransformAsTestClass(className)) { // it's a test class with testable enabled LogUtil.diagnose("Handling test class %s", className); bytes = new TestClassHandler().getBytes(classFileBuffer); dumpByte(className, bytes); - resetMockContext(); } } catch (Throwable t) { LogUtil.warn("Failed to transform class " + className); LogUtil.diagnose(t.toString()); + } finally { + LogUtil.resetLogLevel(); } return bytes; } @@ -226,10 +226,6 @@ public class TestableClassTransformer implements ClassFileTransformer { } } - private void resetMockContext() { - LogUtil.resetLogLevel(); - } - /** * Split desc to "first parameter" and "desc of rest parameters" * @param desc method desc