From b2ecd2fd14d86dec4aa4c549f49d44ecfdea436d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Mon, 12 Oct 2020 20:09:11 +0800 Subject: [PATCH] generate testable internal ref at runtime instead of compile time --- .../agent/handler/SourceClassHandler.java | 4 +++ .../agent/handler/TestClassHandler.java | 6 ++++ .../testable/core/constant/ConstPool.java | 2 -- .../generator/TestableRefFieldGenerator.java | 30 ------------------- .../translator/EnableTestableTranslator.java | 21 ------------- 5 files changed, 10 insertions(+), 53 deletions(-) delete mode 100644 core/src/main/java/com/alibaba/testable/core/generator/TestableRefFieldGenerator.java 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 bdf9ac2..e9889bc 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 @@ -22,6 +22,10 @@ public class SourceClassHandler extends BaseClassHandler { this.injectMethods = injectMethods; } + /** + * Handle bytecode of source class + * @param cn original class node + */ @Override protected void transform(ClassNode cn) { List methods = new ArrayList(); 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 d53bd33..28b323b 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 @@ -12,11 +12,17 @@ import java.util.List; */ public class TestClassHandler extends BaseClassHandler { + /** + * Handle bytecode of test class + * @param cn original class node + */ @Override protected void transform(ClassNode cn) { for (MethodNode m : cn.methods) { transformMethod(cn, m); } + cn.fields.add(new FieldNode(ACC_PUBLIC | ACC_STATIC, ConstPool.TESTABLE_INJECT_REF, + ClassUtil.toByteCodeClassName(cn.name), null, null)); } private void transformMethod(ClassNode cn, MethodNode mn) { diff --git a/core/src/main/java/com/alibaba/testable/core/constant/ConstPool.java b/core/src/main/java/com/alibaba/testable/core/constant/ConstPool.java index 3d225d7..fc0d115 100644 --- a/core/src/main/java/com/alibaba/testable/core/constant/ConstPool.java +++ b/core/src/main/java/com/alibaba/testable/core/constant/ConstPool.java @@ -6,8 +6,6 @@ package com.alibaba.testable.core.constant; public final class ConstPool { public static final String TESTABLE_PRIVATE_ACCESSOR = "com.alibaba.testable.core.accessor.PrivateAccessor"; - public static final String ANNOTATION_TESTABLE_INJECT = "com.alibaba.testable.core.annotation.TestableInject"; - public static final String TESTABLE_REF_FIELD_NAME = "_testableInternalRef"; public static final String TEST_POSTFIX = "Test"; } diff --git a/core/src/main/java/com/alibaba/testable/core/generator/TestableRefFieldGenerator.java b/core/src/main/java/com/alibaba/testable/core/generator/TestableRefFieldGenerator.java deleted file mode 100644 index 0f78d5a..0000000 --- a/core/src/main/java/com/alibaba/testable/core/generator/TestableRefFieldGenerator.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.alibaba.testable.core.generator; - -import com.alibaba.testable.core.constant.ConstPool; -import com.alibaba.testable.core.model.TestableContext; -import com.sun.tools.javac.tree.JCTree.JCModifiers; -import com.sun.tools.javac.tree.JCTree.JCVariableDecl; - -import java.lang.reflect.Modifier; - -/** - * Generate test class reference field - * - * @author flin - */ -public class TestableRefFieldGenerator extends BaseGenerator { - - private final String testClassFullName; - - public TestableRefFieldGenerator(TestableContext cx, String testClassFullName) { - super(cx); - this.testClassFullName = testClassFullName; - } - - public JCVariableDecl fetch() { - JCModifiers mods = cx.treeMaker.Modifiers(Modifier.PUBLIC | Modifier.STATIC); - return cx.treeMaker.VarDef(mods, cx.names.fromString(ConstPool.TESTABLE_REF_FIELD_NAME), - nameToExpression(testClassFullName), null); - } - -} diff --git a/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java b/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java index 4d56bcd..11c56f3 100644 --- a/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java +++ b/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java @@ -2,10 +2,7 @@ package com.alibaba.testable.core.translator; import com.alibaba.testable.core.constant.ConstPool; import com.alibaba.testable.core.generator.PrivateAccessStatementGenerator; -import com.alibaba.testable.core.generator.TestableRefFieldGenerator; import com.alibaba.testable.core.model.TestableContext; -import com.alibaba.testable.core.util.TypeUtil; -import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.*; import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Name; @@ -21,19 +18,15 @@ import java.lang.reflect.Modifier; */ public class EnableTestableTranslator extends BaseTranslator { - private final String testClassName; private final String sourceClassName; private final ListBuffer sourceClassIns = new ListBuffer<>(); private final ListBuffer privateOrFinalFields = new ListBuffer<>(); private final ListBuffer privateMethods = new ListBuffer<>(); private final PrivateAccessStatementGenerator privateAccessStatementGenerator; - private final TestableRefFieldGenerator testableRefFieldGenerator; public EnableTestableTranslator(String pkgName, String testClassName, TestableContext cx) { - this.testClassName = testClassName; this.sourceClassName = testClassName.substring(0, testClassName.length() - ConstPool.TEST_POSTFIX.length()); this.privateAccessStatementGenerator = new PrivateAccessStatementGenerator(cx); - this.testableRefFieldGenerator = new TestableRefFieldGenerator(cx, pkgName + "." + testClassName); try { Class cls = Class.forName(pkgName + "." + sourceClassName); Field[] fields = cls.getDeclaredFields(); @@ -77,20 +70,6 @@ public class EnableTestableTranslator extends BaseTranslator { super.visitExec(jcExpressionStatement); } - /** - * Generate test setup method to initialize n.e.pool - */ - @Override - public void visitClassDef(JCClassDecl jcClassDecl) { - super.visitClassDef(jcClassDecl); - if (jcClassDecl.name.toString().equals(testClassName)) { - ListBuffer ndefs = new ListBuffer<>(); - ndefs.addAll(jcClassDecl.defs); - ndefs.add(testableRefFieldGenerator.fetch()); - jcClassDecl.defs = ndefs.toList(); - } - } - /** * For private invoke invocation break point */