refactor and remove unit test framework dependence

This commit is contained in:
金戟 2020-10-12 19:01:01 +08:00
parent 78d3b2ea90
commit efccedc996
6 changed files with 21 additions and 25 deletions

View File

@ -8,6 +8,7 @@ public class ConstPool {
public static final String DOT = "."; public static final String DOT = ".";
public static final String SLASH = "/"; public static final String SLASH = "/";
public static final String CONSTRUCTOR = "<init>";
public static final String TEST_POSTFIX = "Test"; public static final String TEST_POSTFIX = "Test";
public static final String TESTABLE_INJECT_REF = "_testableInternalRef"; public static final String TESTABLE_INJECT_REF = "_testableInternalRef";

View File

@ -16,7 +16,6 @@ import java.util.Set;
*/ */
public class SourceClassHandler extends BaseClassHandler { public class SourceClassHandler extends BaseClassHandler {
private static final String CONSTRUCTOR = "<init>";
private final List<MethodInfo> injectMethods; private final List<MethodInfo> injectMethods;
public SourceClassHandler(List<MethodInfo> injectMethods) { public SourceClassHandler(List<MethodInfo> injectMethods) {
@ -27,7 +26,7 @@ public class SourceClassHandler extends BaseClassHandler {
protected void transform(ClassNode cn) { protected void transform(ClassNode cn) {
List<MethodInfo> methods = new ArrayList<MethodInfo>(); List<MethodInfo> methods = new ArrayList<MethodInfo>();
for (MethodNode m : cn.methods) { for (MethodNode m : cn.methods) {
if (!CONSTRUCTOR.equals(m.name)) { if (!ConstPool.CONSTRUCTOR.equals(m.name)) {
methods.add(new MethodInfo(m.name, m.desc)); methods.add(new MethodInfo(m.name, m.desc));
} }
} }
@ -51,7 +50,7 @@ public class SourceClassHandler extends BaseClassHandler {
instructions = replaceMemberCallOps(cn, mn, instructions, rangeStart, i); instructions = replaceMemberCallOps(cn, mn, instructions, rangeStart, i);
i = rangeStart; i = rangeStart;
} }
} else if (CONSTRUCTOR.equals(node.name)) { } else if (ConstPool.CONSTRUCTOR.equals(node.name)) {
String newOperatorInjectMethodName = getNewOperatorInjectMethodName(newOperatorInjectMethods, node); String newOperatorInjectMethodName = getNewOperatorInjectMethodName(newOperatorInjectMethods, node);
if (newOperatorInjectMethodName.length() > 0) { if (newOperatorInjectMethodName.length() > 0) {
int rangeStart = getConstructorStart(instructions, node.owner, i); int rangeStart = getConstructorStart(instructions, node.owner, i);

View File

@ -2,7 +2,6 @@ package com.alibaba.testable.agent.handler;
import com.alibaba.testable.agent.constant.ConstPool; 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.CollectionUtil;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -13,17 +12,6 @@ import java.util.List;
*/ */
public class TestClassHandler extends BaseClassHandler { public class TestClassHandler extends BaseClassHandler {
private static final List<String> TEST_ANNOTATIONS = new ArrayList<String>();
static {
// JUnit4
TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.junit.Test"));
// JUnit5
TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.junit.jupiter.api.Test"));
// TestNG
TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.testng.annotations.Test"));
}
@Override @Override
protected void transform(ClassNode cn) { protected void transform(ClassNode cn) {
for (MethodNode m : cn.methods) { for (MethodNode m : cn.methods) {
@ -39,7 +27,7 @@ public class TestClassHandler extends BaseClassHandler {
for (AnnotationNode n : mn.visibleAnnotations) { for (AnnotationNode n : mn.visibleAnnotations) {
visibleAnnotationNames.add(n.desc); visibleAnnotationNames.add(n.desc);
} }
if (CollectionUtil.containsAny(visibleAnnotationNames, TEST_ANNOTATIONS)) { if (!visibleAnnotationNames.contains(ConstPool.TESTABLE_INJECT) && couldBeTestMethod(mn)) {
InsnList il = new InsnList(); InsnList il = new InsnList();
il.add(new VarInsnNode(ALOAD, 0)); il.add(new VarInsnNode(ALOAD, 0));
il.add(new FieldInsnNode(PUTSTATIC, cn.name, ConstPool.TESTABLE_INJECT_REF, ClassUtil.toByteCodeClassName(cn.name))); il.add(new FieldInsnNode(PUTSTATIC, cn.name, ConstPool.TESTABLE_INJECT_REF, ClassUtil.toByteCodeClassName(cn.name)));
@ -47,4 +35,8 @@ public class TestClassHandler extends BaseClassHandler {
} }
} }
private boolean couldBeTestMethod(MethodNode mn) {
return (mn.access & (ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC)) == 0 ;
}
} }

View File

@ -82,8 +82,7 @@ public class ClassUtil {
travelingClass = false; travelingClass = false;
} }
} else { } else {
if (b == TYPE_BYTE || b == TYPE_CHAR || b == TYPE_DOUBLE || b == TYPE_FLOAT if (isPrimaryType(b)) {
|| b == TYPE_INT || b == TYPE_LONG || b == TYPE_SHORT || b == TYPE_BOOL) {
parameterTypes.add(b); parameterTypes.add(b);
} else if (b == TYPE_CLASS) { } else if (b == TYPE_CLASS) {
travelingClass = true; travelingClass = true;
@ -133,4 +132,9 @@ public class ClassUtil {
return className.replace(ConstPool.SLASH, ConstPool.DOT).substring(1, className.length() - 1); return className.replace(ConstPool.SLASH, ConstPool.DOT).substring(1, className.length() - 1);
} }
private static boolean isPrimaryType(byte b) {
return b == TYPE_BYTE || b == TYPE_CHAR || b == TYPE_DOUBLE || b == TYPE_FLOAT
|| b == TYPE_INT || b == TYPE_LONG || b == TYPE_SHORT || b == TYPE_BOOL;
}
} }

View File

@ -69,10 +69,10 @@ public class EnableTestableProcessor extends BaseProcessor {
private void writeBinaryFile(String path, String fileName, byte[] content) { private void writeBinaryFile(String path, String fileName, byte[] content) {
try { try {
FileObject resource = cx.filter.createResource(StandardLocation.SOURCE_OUTPUT, path, fileName); FileObject resource = cx.filter.createResource(StandardLocation.SOURCE_OUTPUT, path, fileName);
OutputStream out = resource.openOutputStream(); try (OutputStream out = resource.openOutputStream()) {
out.write(content); out.write(content);
out.flush(); out.flush();
out.close(); }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
cx.logger.error("Failed to write " + fileName); cx.logger.error("Failed to write " + fileName);

View File

@ -7,11 +7,11 @@ import com.alibaba.testable.core.constant.ConstPool;
*/ */
public class TestableUtil { public class TestableUtil {
public static String currentMemberMethodName(Object testClassRef) { public static String sourceMemberMethodName(Object testClassRef) {
return currentMemberMethodName(testClassRef.getClass()); return sourceMemberMethodName(testClassRef.getClass());
} }
public static String currentMemberMethodName(Class<?> testClass) { public static String sourceMemberMethodName(Class<?> testClass) {
StackTraceElement[] stack = getMainThread().getStackTrace(); StackTraceElement[] stack = getMainThread().getStackTrace();
String testClassName = getRealClassName(testClass); String testClassName = getRealClassName(testClass);
String sourceClassName = testClassName.substring(0, testClassName.length() - ConstPool.TEST_POSTFIX.length()); String sourceClassName = testClassName.substring(0, testClassName.length() - ConstPool.TEST_POSTFIX.length());