standalone annotation jar is not necessary

This commit is contained in:
金戟 2020-07-26 16:55:53 +08:00
parent 6473efd652
commit 5ac7f354f3
5 changed files with 11 additions and 24 deletions

View File

@ -8,6 +8,7 @@ import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
@ -83,9 +84,10 @@ public class SourceClassHandler extends ClassHandler {
String classType = ((TypeInsnNode)instructions[start]).desc; String classType = ((TypeInsnNode)instructions[start]).desc;
String constructorDesc = ((MethodInsnNode)instructions[end]).desc; String constructorDesc = ((MethodInsnNode)instructions[end]).desc;
mn.instructions.insertBefore(instructions[start], new LdcInsnNode(Type.getType("L" + classType + ";"))); mn.instructions.insertBefore(instructions[start], new LdcInsnNode(Type.getType("L" + classType + ";")));
List<Byte> parameterTypes = ClassUtil.getParameterTypes(constructorDesc);
InsnList il = new InsnList(); InsnList il = new InsnList();
il.add(new MethodInsnNode(INVOKESTATIC, TESTABLE_NE, TESTABLE_W, il.add(new MethodInsnNode(INVOKESTATIC, TESTABLE_NE, TESTABLE_W,
getConstructorSubstitutionDesc(constructorDesc), false)); getConstructorSubstitutionDesc(parameterTypes.size()), false));
il.add(new TypeInsnNode(CHECKCAST, classType)); il.add(new TypeInsnNode(CHECKCAST, classType));
mn.instructions.insertBefore(instructions[end], il); mn.instructions.insertBefore(instructions[end], il);
mn.instructions.remove(instructions[start]); mn.instructions.remove(instructions[start]);
@ -95,9 +97,8 @@ public class SourceClassHandler extends ClassHandler {
return mn.instructions.toArray(); return mn.instructions.toArray();
} }
private String getConstructorSubstitutionDesc(String constructorDesc) { private String getConstructorSubstitutionDesc(int parameterCount) {
int paramCount = ClassUtil.getParameterTypes(constructorDesc).size(); return CONSTRUCTOR_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, parameterCount) + METHOD_DESC_POSTFIX;
return CONSTRUCTOR_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, paramCount) + METHOD_DESC_POSTFIX;
} }
private AbstractInsnNode[] replaceMemberCallOps(MethodNode mn, AbstractInsnNode[] instructions, int start, int end) { 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 returnType = ClassUtil.getReturnType(methodDesc);
String methodName = ((MethodInsnNode)instructions[end]).name; String methodName = ((MethodInsnNode)instructions[end]).name;
mn.instructions.insert(instructions[start], new LdcInsnNode(methodName)); mn.instructions.insert(instructions[start], new LdcInsnNode(methodName));
List<Byte> parameterTypes = ClassUtil.getParameterTypes(methodDesc);
InsnList il = new InsnList(); InsnList il = new InsnList();
il.add(new MethodInsnNode(INVOKESTATIC, TESTABLE_NE, TESTABLE_F, il.add(new MethodInsnNode(INVOKESTATIC, TESTABLE_NE, TESTABLE_F,
getMethodSubstitutionDesc(methodDesc), false)); getMethodSubstitutionDesc(parameterTypes.size()), false));
il.add(new TypeInsnNode(CHECKCAST, returnType)); il.add(new TypeInsnNode(CHECKCAST, returnType));
mn.instructions.insertBefore(instructions[end], il); mn.instructions.insertBefore(instructions[end], il);
mn.instructions.remove(instructions[end]); mn.instructions.remove(instructions[end]);
@ -115,9 +117,8 @@ public class SourceClassHandler extends ClassHandler {
return mn.instructions.toArray(); return mn.instructions.toArray();
} }
private String getMethodSubstitutionDesc(String methodDesc) { private String getMethodSubstitutionDesc(int parameterCount) {
int paramCount = ClassUtil.getParameterTypes(methodDesc).size(); return METHOD_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, parameterCount) + METHOD_DESC_POSTFIX;
return METHOD_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, paramCount) + METHOD_DESC_POSTFIX;
} }
} }

View File

@ -18,7 +18,7 @@ import java.util.Set;
public class TestableClassTransformer implements ClassFileTransformer { public class TestableClassTransformer implements ClassFileTransformer {
private static final String ENABLE_TESTABLE = "com.alibaba.testable.core.annotation.EnableTestable"; 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 String TEST_POSTFIX = "Test";
private static final Set<String> loadedClassNames = new HashSet<String>(); private static final Set<String> loadedClassNames = new HashSet<String>();

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>Testable</name>
<description>Unit test enhancement toolkit</description>
<groupId>com.alibaba.testable</groupId>
<artifactId>annotation</artifactId>
<version>0.0.3-SNAPSHOT</version>
</project>

View File

@ -1,4 +1,4 @@
package com.alibaba.testable.annotation; package com.alibaba.testable.core.annotation;
import java.lang.annotation.*; import java.lang.annotation.*;

View File

@ -10,7 +10,6 @@
<modules> <modules>
<module>agent</module> <module>agent</module>
<module>annotation</module>
<module>core</module> <module>core</module>
</modules> </modules>