From 12ddb9398cc0ba1c1406f00f958e6ef80fcec4f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 16 May 2020 12:29:37 +0800 Subject: [PATCH] use testable annotation for both dev and test role --- .../alibaba/testable/annotation/Testable.java | 2 +- .../testable/annotation/TestableInject.java | 2 +- ...=> CallSuperMethodStatementGenerator.java} | 2 +- ...ava => TestableClassDevRoleGenerator.java} | 10 +++---- .../processor/TestableInjectProcessor.java | 23 -------------- .../testable/processor/TestableProcessor.java | 30 +++++++++++-------- ...va => TestableClassDevRoleTranslator.java} | 4 +-- ...a => TestableClassTestRoleTranslator.java} | 4 +-- 8 files changed, 30 insertions(+), 47 deletions(-) rename src/main/java/com/alibaba/testable/generator/{CallSuperMethodStatement.java => CallSuperMethodStatementGenerator.java} (98%) rename src/main/java/com/alibaba/testable/generator/{TestableClassGenerator.java => TestableClassDevRoleGenerator.java} (91%) delete mode 100644 src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java rename src/main/java/com/alibaba/testable/translator/{TestableClassTranslator.java => TestableClassDevRoleTranslator.java} (97%) rename src/main/java/com/alibaba/testable/translator/{TestableFieldTranslator.java => TestableClassTestRoleTranslator.java} (85%) diff --git a/src/main/java/com/alibaba/testable/annotation/Testable.java b/src/main/java/com/alibaba/testable/annotation/Testable.java index e8076d0..d236d63 100644 --- a/src/main/java/com/alibaba/testable/annotation/Testable.java +++ b/src/main/java/com/alibaba/testable/annotation/Testable.java @@ -10,7 +10,7 @@ import java.lang.annotation.*; * @author flin */ @Retention(RetentionPolicy.SOURCE) -@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) +@Target(ElementType.TYPE) @Documented public @interface Testable { diff --git a/src/main/java/com/alibaba/testable/annotation/TestableInject.java b/src/main/java/com/alibaba/testable/annotation/TestableInject.java index 64bf60d..4a36b66 100644 --- a/src/main/java/com/alibaba/testable/annotation/TestableInject.java +++ b/src/main/java/com/alibaba/testable/annotation/TestableInject.java @@ -8,7 +8,7 @@ import java.lang.annotation.*; * @author flin */ @Retention(RetentionPolicy.SOURCE) -@Target(ElementType.FIELD) +@Target(ElementType.METHOD) @Documented public @interface TestableInject { } diff --git a/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatement.java b/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatementGenerator.java similarity index 98% rename from src/main/java/com/alibaba/testable/generator/CallSuperMethodStatement.java rename to src/main/java/com/alibaba/testable/generator/CallSuperMethodStatementGenerator.java index 50823c9..e52bbe6 100644 --- a/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatement.java +++ b/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatementGenerator.java @@ -14,7 +14,7 @@ import java.util.List; * * @author flin */ -public class CallSuperMethodStatement { +public class CallSuperMethodStatementGenerator { public Statement[] fetch(String className, JCTree.JCMethodDecl method) { if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) { diff --git a/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java b/src/main/java/com/alibaba/testable/generator/TestableClassDevRoleGenerator.java similarity index 91% rename from src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java rename to src/main/java/com/alibaba/testable/generator/TestableClassDevRoleGenerator.java index 5647c6c..44ade24 100644 --- a/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java +++ b/src/main/java/com/alibaba/testable/generator/TestableClassDevRoleGenerator.java @@ -1,7 +1,7 @@ package com.alibaba.testable.generator; import com.alibaba.testable.generator.model.Statement; -import com.alibaba.testable.translator.TestableClassTranslator; +import com.alibaba.testable.translator.TestableClassDevRoleTranslator; import com.alibaba.testable.util.ConstPool; import com.squareup.javapoet.*; import com.sun.tools.javac.api.JavacTrees; @@ -21,19 +21,19 @@ import java.util.Set; * * @author flin */ -public class TestableClassGenerator { +public class TestableClassDevRoleGenerator { private final JavacTrees trees; private final TreeMaker treeMaker; - public TestableClassGenerator(JavacTrees trees, TreeMaker treeMaker) { + public TestableClassDevRoleGenerator(JavacTrees trees, TreeMaker treeMaker) { this.trees = trees; this.treeMaker = treeMaker; } public String fetch(Element clazz, String packageName, String className) { JCTree tree = trees.getTree(clazz); - TestableClassTranslator translator = new TestableClassTranslator(treeMaker); + TestableClassDevRoleTranslator translator = new TestableClassDevRoleTranslator(treeMaker); tree.accept(translator); List methodSpecs = new ArrayList<>(); @@ -90,7 +90,7 @@ public class TestableClassGenerator { private void addCallSuperStatements(MethodSpec.Builder builder, Element classElement, JCTree.JCMethodDecl method) { String className = classElement.getSimpleName().toString(); - Statement[] statements = new CallSuperMethodStatement().fetch(className, method); + Statement[] statements = new CallSuperMethodStatementGenerator().fetch(className, method); for (Statement s : statements) { builder.addStatement(s.getLine(), s.getParams()); } diff --git a/src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java b/src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java deleted file mode 100644 index ca1fb59..0000000 --- a/src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.alibaba.testable.processor; - -import javax.annotation.processing.RoundEnvironment; -import javax.annotation.processing.SupportedAnnotationTypes; -import javax.annotation.processing.SupportedSourceVersion; -import javax.lang.model.SourceVersion; -import javax.lang.model.element.TypeElement; -import java.util.Set; - -/** - * @author flin - */ -@SupportedAnnotationTypes("com.alibaba.testable.annotation.TestableInject") -@SupportedSourceVersion(SourceVersion.RELEASE_7) -public class TestableInjectProcessor extends BaseProcessor { - - @Override - public boolean process(Set annotations, RoundEnvironment roundEnv) { - info("Enter TestableInjectProcessor !"); - return true; - } - -} diff --git a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java b/src/main/java/com/alibaba/testable/processor/TestableProcessor.java index 05889b8..1ae13cd 100644 --- a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java +++ b/src/main/java/com/alibaba/testable/processor/TestableProcessor.java @@ -2,8 +2,8 @@ package com.alibaba.testable.processor; import com.alibaba.testable.annotation.Testable; import com.alibaba.testable.generator.StaticNewClassGenerator; -import com.alibaba.testable.generator.TestableClassGenerator; -import com.alibaba.testable.translator.TestableFieldTranslator; +import com.alibaba.testable.generator.TestableClassDevRoleGenerator; +import com.alibaba.testable.translator.TestableClassTestRoleTranslator; import com.alibaba.testable.util.ConstPool; import com.sun.tools.javac.tree.JCTree; @@ -39,9 +39,11 @@ public class TestableProcessor extends BaseProcessor { createStaticNewClass(); for (Element element : elements) { if (element.getKind().isClass()) { - processClassElement(element); - } else if (element.getKind().isField()) { - processFieldElement(element); + if (isTestClass(element.getSimpleName())) { + processTestRoleClassElement(element); + } else { + processDevRoleClassElement(element); + } } } return true; @@ -69,27 +71,31 @@ public class TestableProcessor extends BaseProcessor { } } + private boolean isTestClass(Name name) { + return name.toString().endsWith("Test"); + } + private boolean isCompilingTestClass(FileObject staticNewClassFile) { return staticNewClassFile.getName().contains(GENERATED_TEST_SOURCES); } - private void processFieldElement(Element field) { - JCTree tree = trees.getTree(field); - tree.accept(new TestableFieldTranslator(treeMaker)); - } - - private void processClassElement(Element clazz) { + private void processDevRoleClassElement(Element clazz) { String packageName = elementUtils.getPackageOf(clazz).getQualifiedName().toString(); String testableTypeName = getTestableClassName(clazz.getSimpleName()); String fullQualityTypeName = packageName + "." + testableTypeName; try { writeSourceFile(fullQualityTypeName, - new TestableClassGenerator(trees, treeMaker).fetch(clazz, packageName, testableTypeName)); + new TestableClassDevRoleGenerator(trees, treeMaker).fetch(clazz, packageName, testableTypeName)); } catch (IOException e) { e.printStackTrace(); } } + private void processTestRoleClassElement(Element field) { + JCTree tree = trees.getTree(field); + tree.accept(new TestableClassTestRoleTranslator(treeMaker)); + } + private void writeSourceFile(String fullQualityTypeName, String content) throws IOException { JavaFileObject jfo = filter.createSourceFile(fullQualityTypeName); Writer writer = jfo.openWriter(); diff --git a/src/main/java/com/alibaba/testable/translator/TestableClassTranslator.java b/src/main/java/com/alibaba/testable/translator/TestableClassDevRoleTranslator.java similarity index 97% rename from src/main/java/com/alibaba/testable/translator/TestableClassTranslator.java rename to src/main/java/com/alibaba/testable/translator/TestableClassDevRoleTranslator.java index bf723d3..ccf6de0 100644 --- a/src/main/java/com/alibaba/testable/translator/TestableClassTranslator.java +++ b/src/main/java/com/alibaba/testable/translator/TestableClassDevRoleTranslator.java @@ -18,7 +18,7 @@ import java.lang.reflect.Modifier; * * @author flin */ -public class TestableClassTranslator extends TreeTranslator { +public class TestableClassDevRoleTranslator extends TreeTranslator { private final TreeMaker treeMaker; @@ -31,7 +31,7 @@ public class TestableClassTranslator extends TreeTranslator { return methods; } - public TestableClassTranslator(TreeMaker treeMaker) { + public TestableClassDevRoleTranslator(TreeMaker treeMaker) { this.treeMaker = treeMaker; } diff --git a/src/main/java/com/alibaba/testable/translator/TestableFieldTranslator.java b/src/main/java/com/alibaba/testable/translator/TestableClassTestRoleTranslator.java similarity index 85% rename from src/main/java/com/alibaba/testable/translator/TestableFieldTranslator.java rename to src/main/java/com/alibaba/testable/translator/TestableClassTestRoleTranslator.java index 573bc20..f976b35 100644 --- a/src/main/java/com/alibaba/testable/translator/TestableFieldTranslator.java +++ b/src/main/java/com/alibaba/testable/translator/TestableClassTestRoleTranslator.java @@ -11,11 +11,11 @@ import com.sun.tools.javac.util.Name; * * @author flin */ -public class TestableFieldTranslator extends TreeTranslator { +public class TestableClassTestRoleTranslator extends TreeTranslator { private TreeMaker treeMaker; - public TestableFieldTranslator(TreeMaker treeMaker) {this.treeMaker = treeMaker;} + public TestableClassTestRoleTranslator(TreeMaker treeMaker) {this.treeMaker = treeMaker;} @Override public void visitVarDef(JCTree.JCVariableDecl decl) {