From 5ac7f354f3a695d769ebbcb98f4e7dd43d022e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sun, 26 Jul 2020 16:55:53 +0800 Subject: [PATCH] standalone annotation jar is not necessary --- .../agent/handler/SourceClassHandler.java | 17 +++++++++-------- .../transformer/TestableClassTransformer.java | 2 +- annotation/pom.xml | 13 ------------- .../core}/annotation/EnableTestableInject.java | 2 +- pom.xml | 1 - 5 files changed, 11 insertions(+), 24 deletions(-) delete mode 100644 annotation/pom.xml rename {annotation/src/main/java/com/alibaba/testable => core/src/main/java/com/alibaba/testable/core}/annotation/EnableTestableInject.java (85%) 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 8ee0620..e67d2ad 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 @@ -8,6 +8,7 @@ import org.objectweb.asm.Type; import org.objectweb.asm.tree.*; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -83,9 +84,10 @@ public class SourceClassHandler extends ClassHandler { String classType = ((TypeInsnNode)instructions[start]).desc; String constructorDesc = ((MethodInsnNode)instructions[end]).desc; mn.instructions.insertBefore(instructions[start], new LdcInsnNode(Type.getType("L" + classType + ";"))); + List parameterTypes = ClassUtil.getParameterTypes(constructorDesc); InsnList il = new InsnList(); il.add(new MethodInsnNode(INVOKESTATIC, TESTABLE_NE, TESTABLE_W, - getConstructorSubstitutionDesc(constructorDesc), false)); + getConstructorSubstitutionDesc(parameterTypes.size()), false)); il.add(new TypeInsnNode(CHECKCAST, classType)); mn.instructions.insertBefore(instructions[end], il); mn.instructions.remove(instructions[start]); @@ -95,9 +97,8 @@ public class SourceClassHandler extends ClassHandler { return mn.instructions.toArray(); } - private String getConstructorSubstitutionDesc(String constructorDesc) { - int paramCount = ClassUtil.getParameterTypes(constructorDesc).size(); - return CONSTRUCTOR_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, paramCount) + METHOD_DESC_POSTFIX; + private String getConstructorSubstitutionDesc(int parameterCount) { + return CONSTRUCTOR_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, parameterCount) + METHOD_DESC_POSTFIX; } private AbstractInsnNode[] replaceMemberCallOps(MethodNode mn, AbstractInsnNode[] instructions, int start, int end) { @@ -105,9 +106,10 @@ public class SourceClassHandler extends ClassHandler { String returnType = ClassUtil.getReturnType(methodDesc); String methodName = ((MethodInsnNode)instructions[end]).name; mn.instructions.insert(instructions[start], new LdcInsnNode(methodName)); + List parameterTypes = ClassUtil.getParameterTypes(methodDesc); InsnList il = new InsnList(); il.add(new MethodInsnNode(INVOKESTATIC, TESTABLE_NE, TESTABLE_F, - getMethodSubstitutionDesc(methodDesc), false)); + getMethodSubstitutionDesc(parameterTypes.size()), false)); il.add(new TypeInsnNode(CHECKCAST, returnType)); mn.instructions.insertBefore(instructions[end], il); mn.instructions.remove(instructions[end]); @@ -115,9 +117,8 @@ public class SourceClassHandler extends ClassHandler { return mn.instructions.toArray(); } - private String getMethodSubstitutionDesc(String methodDesc) { - int paramCount = ClassUtil.getParameterTypes(methodDesc).size(); - return METHOD_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, paramCount) + METHOD_DESC_POSTFIX; + private String getMethodSubstitutionDesc(int parameterCount) { + return METHOD_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, parameterCount) + METHOD_DESC_POSTFIX; } } diff --git a/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index 614562f..dfbc678 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -18,7 +18,7 @@ import java.util.Set; public class TestableClassTransformer implements ClassFileTransformer { private static final String ENABLE_TESTABLE = "com.alibaba.testable.core.annotation.EnableTestable"; - private static final String ENABLE_TESTABLE_INJECT = "com.alibaba.testable.annotation.EnableTestableInject"; + private static final String ENABLE_TESTABLE_INJECT = "com.alibaba.testable.core.annotation.EnableTestableInject"; private static final String TEST_POSTFIX = "Test"; private static final Set loadedClassNames = new HashSet(); diff --git a/annotation/pom.xml b/annotation/pom.xml deleted file mode 100644 index 53c8694..0000000 --- a/annotation/pom.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - 4.0.0 - - Testable - Unit test enhancement toolkit - com.alibaba.testable - annotation - 0.0.3-SNAPSHOT - - diff --git a/annotation/src/main/java/com/alibaba/testable/annotation/EnableTestableInject.java b/core/src/main/java/com/alibaba/testable/core/annotation/EnableTestableInject.java similarity index 85% rename from annotation/src/main/java/com/alibaba/testable/annotation/EnableTestableInject.java rename to core/src/main/java/com/alibaba/testable/core/annotation/EnableTestableInject.java index 8b8eac5..ead675b 100644 --- a/annotation/src/main/java/com/alibaba/testable/annotation/EnableTestableInject.java +++ b/core/src/main/java/com/alibaba/testable/core/annotation/EnableTestableInject.java @@ -1,4 +1,4 @@ -package com.alibaba.testable.annotation; +package com.alibaba.testable.core.annotation; import java.lang.annotation.*; diff --git a/pom.xml b/pom.xml index a43105b..09fbd31 100755 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,6 @@ agent - annotation core