fix incorrect type name in byte code

This commit is contained in:
金戟 2020-07-25 23:12:18 +08:00
parent a947939d08
commit 6009326150
4 changed files with 15 additions and 11 deletions

View File

@ -6,7 +6,7 @@ import java.util.List;
/** /**
* @author flin * @author flin
*/ */
public class Const { public class ConstPool {
public static final String DOT = "."; public static final String DOT = ".";
public static final String SLASH = "/"; public static final String SLASH = "/";

View File

@ -1,15 +1,12 @@
package com.alibaba.testable.agent.handler; 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.ClassUtil;
import com.alibaba.testable.agent.util.StringUtil; 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.Opcodes;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -52,7 +49,7 @@ public class SourceClassHandler extends ClassHandler {
instructions = replaceMemberCallOps(mn, instructions, rangeStart, i); instructions = replaceMemberCallOps(mn, instructions, rangeStart, i);
i = rangeStart; 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); int rangeStart = getConstructorStart(instructions, node.owner, i);
if (rangeStart >= 0) { if (rangeStart >= 0) {
instructions = replaceNewOps(mn, instructions, rangeStart, i); instructions = replaceNewOps(mn, instructions, rangeStart, i);

View File

@ -1,5 +1,6 @@
package com.alibaba.testable.agent.handler; package com.alibaba.testable.agent.handler;
import com.alibaba.testable.agent.util.ClassUtil;
import com.alibaba.testable.agent.util.CollectionUtil; import com.alibaba.testable.agent.util.CollectionUtil;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
@ -16,11 +17,11 @@ public class TestClassHandler extends ClassHandler {
static { static {
// JUnit4 // JUnit4
testAnnotations.add("org.junit.Test"); testAnnotations.add(ClassUtil.toByteCodeClassName("org.junit.Test"));
// JUnit5 // JUnit5
testAnnotations.add("org.junit.jupiter.api.Test"); testAnnotations.add(ClassUtil.toByteCodeClassName("org.junit.jupiter.api.Test"));
// TestNG // TestNG
testAnnotations.add("org.testng.annotations.Test"); testAnnotations.add(ClassUtil.toByteCodeClassName("org.testng.annotations.Test"));
} }
@Override @Override
@ -32,6 +33,9 @@ public class TestClassHandler extends ClassHandler {
private void transformMethod(ClassNode cn, MethodNode mn) { private void transformMethod(ClassNode cn, MethodNode mn) {
List<String> visibleAnnotationNames = new ArrayList<String>(); List<String> visibleAnnotationNames = new ArrayList<String>();
if (mn.visibleAnnotations == null) {
return;
}
for (AnnotationNode n : mn.visibleAnnotations) { for (AnnotationNode n : mn.visibleAnnotations) {
visibleAnnotationNames.add(n.desc); visibleAnnotationNames.add(n.desc);
} }

View File

@ -1,6 +1,6 @@
package com.alibaba.testable.agent.util; 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.ClassReader;
import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
@ -37,7 +37,7 @@ public class ClassUtil {
ClassNode cn = new ClassNode(); ClassNode cn = new ClassNode();
new ClassReader(className).accept(cn, 0); new ClassReader(className).accept(cn, 0);
for (AnnotationNode an : cn.visibleAnnotations) { 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); annotations.add(annotationName);
} }
return annotations; 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;
}
} }