From 6009326150b31293654fe9dcc84a510ab51be086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 25 Jul 2020 23:12:18 +0800 Subject: [PATCH] fix incorrect type name in byte code --- .../agent/constant/{Const.java => ConstPool.java} | 2 +- .../testable/agent/handler/SourceClassHandler.java | 7 ++----- .../testable/agent/handler/TestClassHandler.java | 10 +++++++--- .../com/alibaba/testable/agent/util/ClassUtil.java | 7 +++++-- 4 files changed, 15 insertions(+), 11 deletions(-) rename agent/src/main/java/com/alibaba/testable/agent/constant/{Const.java => ConstPool.java} (93%) diff --git a/agent/src/main/java/com/alibaba/testable/agent/constant/Const.java b/agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java similarity index 93% rename from agent/src/main/java/com/alibaba/testable/agent/constant/Const.java rename to agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java index 92ba48b..ba9dcc6 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/constant/Const.java +++ b/agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java @@ -6,7 +6,7 @@ import java.util.List; /** * @author flin */ -public class Const { +public class ConstPool { public static final String DOT = "."; public static final String SLASH = "/"; diff --git a/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index aa59512..0959e21 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -1,15 +1,12 @@ package com.alibaba.testable.agent.handler; -import com.alibaba.testable.agent.constant.Const; +import com.alibaba.testable.agent.constant.ConstPool; import com.alibaba.testable.agent.util.ClassUtil; import com.alibaba.testable.agent.util.StringUtil; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; import org.objectweb.asm.tree.*; -import java.io.IOException; import java.util.HashSet; import java.util.Set; @@ -52,7 +49,7 @@ public class SourceClassHandler extends ClassHandler { instructions = replaceMemberCallOps(mn, instructions, rangeStart, i); i = rangeStart; } - } else if (CONSTRUCTOR.equals(node.name) && !Const.SYS_CLASSES.contains(node.owner)) { + } else if (CONSTRUCTOR.equals(node.name) && !ConstPool.SYS_CLASSES.contains(node.owner)) { int rangeStart = getConstructorStart(instructions, node.owner, i); if (rangeStart >= 0) { instructions = replaceNewOps(mn, instructions, rangeStart, i); diff --git a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java index 1a508a3..dbde623 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java @@ -1,5 +1,6 @@ package com.alibaba.testable.agent.handler; +import com.alibaba.testable.agent.util.ClassUtil; import com.alibaba.testable.agent.util.CollectionUtil; import org.objectweb.asm.tree.*; @@ -16,11 +17,11 @@ public class TestClassHandler extends ClassHandler { static { // JUnit4 - testAnnotations.add("org.junit.Test"); + testAnnotations.add(ClassUtil.toByteCodeClassName("org.junit.Test")); // JUnit5 - testAnnotations.add("org.junit.jupiter.api.Test"); + testAnnotations.add(ClassUtil.toByteCodeClassName("org.junit.jupiter.api.Test")); // TestNG - testAnnotations.add("org.testng.annotations.Test"); + testAnnotations.add(ClassUtil.toByteCodeClassName("org.testng.annotations.Test")); } @Override @@ -32,6 +33,9 @@ public class TestClassHandler extends ClassHandler { private void transformMethod(ClassNode cn, MethodNode mn) { List visibleAnnotationNames = new ArrayList(); + if (mn.visibleAnnotations == null) { + return; + } for (AnnotationNode n : mn.visibleAnnotations) { visibleAnnotationNames.add(n.desc); } diff --git a/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java b/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java index 9961a35..18bce5c 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java +++ b/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java @@ -1,6 +1,6 @@ package com.alibaba.testable.agent.util; -import com.alibaba.testable.agent.constant.Const; +import com.alibaba.testable.agent.constant.ConstPool; import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; @@ -37,7 +37,7 @@ public class ClassUtil { ClassNode cn = new ClassNode(); new ClassReader(className).accept(cn, 0); for (AnnotationNode an : cn.visibleAnnotations) { - String annotationName = an.desc.replace(Const.SLASH, Const.DOT).substring(1, an.desc.length() - 1); + String annotationName = an.desc.replace(ConstPool.SLASH, ConstPool.DOT).substring(1, an.desc.length() - 1); annotations.add(annotationName); } return annotations; @@ -98,4 +98,7 @@ public class ClassUtil { } } + public static String toByteCodeClassName(String className) { + return TYPE_CLASS + className.replace(ConstPool.DOT, ConstPool.SLASH) + CLASS_END; + } }