From fa7907505583368164c49d7500cb1892a76cf54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Thu, 21 May 2020 22:21:17 +0800 Subject: [PATCH] rename annotations --- .../testable/annotation/EnableTestable.java | 15 ++++++ .../annotation/EnableTestableInject.java | 15 ++++++ .../alibaba/testable/annotation/Testable.java | 17 ------ .../testable/annotation/TestableInject.java | 2 +- ...ava => EnableTestableInjectProcessor.java} | 36 +++---------- .../processor/EnableTestableProcessor.java | 53 +++++++++++++++++++ .../javax.annotation.processing.Processor | 3 +- 7 files changed, 92 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/alibaba/testable/annotation/EnableTestable.java create mode 100644 src/main/java/com/alibaba/testable/annotation/EnableTestableInject.java delete mode 100644 src/main/java/com/alibaba/testable/annotation/Testable.java rename src/main/java/com/alibaba/testable/processor/{TestableProcessor.java => EnableTestableInjectProcessor.java} (70%) create mode 100644 src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java diff --git a/src/main/java/com/alibaba/testable/annotation/EnableTestable.java b/src/main/java/com/alibaba/testable/annotation/EnableTestable.java new file mode 100644 index 0000000..4bb27d6 --- /dev/null +++ b/src/main/java/com/alibaba/testable/annotation/EnableTestable.java @@ -0,0 +1,15 @@ +package com.alibaba.testable.annotation; + +import java.lang.annotation.*; + +/** + * Make test class able to access private field and method in source class + * + * @author flin + */ +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.TYPE) +@Documented +public @interface EnableTestable { + +} diff --git a/src/main/java/com/alibaba/testable/annotation/EnableTestableInject.java b/src/main/java/com/alibaba/testable/annotation/EnableTestableInject.java new file mode 100644 index 0000000..24fe925 --- /dev/null +++ b/src/main/java/com/alibaba/testable/annotation/EnableTestableInject.java @@ -0,0 +1,15 @@ +package com.alibaba.testable.annotation; + +import java.lang.annotation.*; + +/** + * Make the method in this class able to be injected by testable substitution methods + * + * @author flin + */ +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.TYPE) +@Documented +public @interface EnableTestableInject { + +} diff --git a/src/main/java/com/alibaba/testable/annotation/Testable.java b/src/main/java/com/alibaba/testable/annotation/Testable.java deleted file mode 100644 index d236d63..0000000 --- a/src/main/java/com/alibaba/testable/annotation/Testable.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.alibaba.testable.annotation; - -import java.lang.annotation.*; - -/** - * On type, make all methods in the class testable - * On method, make the method testable - * On field (in test class), fit the variable for unit test - * - * @author flin - */ -@Retention(RetentionPolicy.SOURCE) -@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 4a36b66..219d144 100644 --- a/src/main/java/com/alibaba/testable/annotation/TestableInject.java +++ b/src/main/java/com/alibaba/testable/annotation/TestableInject.java @@ -3,7 +3,7 @@ package com.alibaba.testable.annotation; import java.lang.annotation.*; /** - * Use marked variable replace the ones in testable class + * Use marked method to replace the ones in source class * * @author flin */ diff --git a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java b/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java similarity index 70% rename from src/main/java/com/alibaba/testable/processor/TestableProcessor.java rename to src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java index ea00d7a..74b631c 100644 --- a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java +++ b/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java @@ -1,12 +1,10 @@ package com.alibaba.testable.processor; -import com.alibaba.testable.annotation.Testable; +import com.alibaba.testable.annotation.EnableTestableInject; import com.alibaba.testable.generator.StaticNewClassGenerator; import com.alibaba.testable.generator.TestableClassDevRoleGenerator; -import com.alibaba.testable.translator.TestableClassTestRoleTranslator; import com.alibaba.testable.util.ConstPool; import com.sun.tools.javac.code.Symbol; -import com.sun.tools.javac.tree.JCTree; import javax.annotation.processing.FilerException; import javax.annotation.processing.RoundEnvironment; @@ -27,24 +25,20 @@ import static javax.tools.StandardLocation.SOURCE_OUTPUT; /** * @author flin */ -@SupportedAnnotationTypes("com.alibaba.testable.annotation.Testable") +@SupportedAnnotationTypes("com.alibaba.testable.annotation.EnableTestableInject") @SupportedSourceVersion(SourceVersion.RELEASE_7) -public class TestableProcessor extends BaseProcessor { +public class EnableTestableInjectProcessor extends BaseProcessor { private static final String JAVA_POSTFIX = ".java"; private static final String GENERATED_TEST_SOURCES = "generated-test-sources"; @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { - Set elements = roundEnv.getElementsAnnotatedWith(Testable.class); + Set elements = roundEnv.getElementsAnnotatedWith(EnableTestableInject.class); createStaticNewClass(); for (Element element : elements) { if (element.getKind().isClass()) { - if (isTestClass(element.getSimpleName())) { - processTestRoleClassElement((Symbol.ClassSymbol)element); - } else { - processDevRoleClassElement((Symbol.ClassSymbol)element); - } + processClassElement((Symbol.ClassSymbol)element); } } return true; @@ -72,15 +66,11 @@ 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 processDevRoleClassElement(Symbol.ClassSymbol clazz) { + private void processClassElement(Symbol.ClassSymbol clazz) { String packageName = cx.elementUtils.getPackageOf(clazz).getQualifiedName().toString(); String testableTypeName = getTestableClassName(clazz.getSimpleName()); String fullQualityTypeName = packageName + "." + testableTypeName; @@ -92,20 +82,6 @@ public class TestableProcessor extends BaseProcessor { } } - private void processTestRoleClassElement(Symbol.ClassSymbol clazz) { - JCTree tree = cx.trees.getTree(clazz); - tree.accept(new TestableClassTestRoleTranslator(getPkgName(clazz), getOriginClassName(clazz), cx)); - } - - private String getPkgName(Symbol.ClassSymbol clazz) { - return ((Symbol.PackageSymbol)clazz.owner).fullname.toString(); - } - - private String getOriginClassName(Symbol.ClassSymbol clazz) { - String testClassName = clazz.getSimpleName().toString(); - return testClassName.substring(0, testClassName.length() - "Test".length()); - } - private void writeSourceFile(String fullQualityTypeName, String content) throws IOException { JavaFileObject jfo = cx.filter.createSourceFile(fullQualityTypeName); Writer writer = jfo.openWriter(); diff --git a/src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java b/src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java new file mode 100644 index 0000000..5b40797 --- /dev/null +++ b/src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java @@ -0,0 +1,53 @@ +package com.alibaba.testable.processor; + +import com.alibaba.testable.annotation.EnableTestable; +import com.alibaba.testable.translator.TestableClassTestRoleTranslator; +import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.tree.JCTree; + +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.Element; +import javax.lang.model.element.Name; +import javax.lang.model.element.TypeElement; +import java.util.Set; + +/** + * @author flin + */ +@SupportedAnnotationTypes("com.alibaba.testable.annotation.EnableTestable") +@SupportedSourceVersion(SourceVersion.RELEASE_7) +public class EnableTestableProcessor extends BaseProcessor { + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + Set elements = roundEnv.getElementsAnnotatedWith(EnableTestable.class); + for (Element element : elements) { + if (element.getKind().isClass() && isTestClass(element.getSimpleName())) { + processClassElement((Symbol.ClassSymbol)element); + } + } + return true; + } + + private boolean isTestClass(Name name) { + return name.toString().endsWith("Test"); + } + + private void processClassElement(Symbol.ClassSymbol clazz) { + JCTree tree = cx.trees.getTree(clazz); + tree.accept(new TestableClassTestRoleTranslator(getPkgName(clazz), getOriginClassName(clazz), cx)); + } + + private String getPkgName(Symbol.ClassSymbol clazz) { + return ((Symbol.PackageSymbol)clazz.owner).fullname.toString(); + } + + private String getOriginClassName(Symbol.ClassSymbol clazz) { + String testClassName = clazz.getSimpleName().toString(); + return testClassName.substring(0, testClassName.length() - "Test".length()); + } + +} diff --git a/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/src/main/resources/META-INF/services/javax.annotation.processing.Processor index 45fc714..72eb2fe 100644 --- a/src/main/resources/META-INF/services/javax.annotation.processing.Processor +++ b/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -1 +1,2 @@ -com.alibaba.testable.processor.TestableProcessor +com.alibaba.testable.processor.EnableTestableProcessor +com.alibaba.testable.processor.EnableTestableInjectProcessor