From 5e2e4811fda17d3ad3f68b650f01a4aad024b34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 9 May 2020 16:26:19 +0800 Subject: [PATCH] let processor accessable --- pom.xml | 2 - .../alibaba/testable/annotation/Testable.java | 2 +- .../testable/annotation/TestableInject.java | 2 +- .../testable/processor/BaseProcessor.java | 55 +++++++++++++++++++ .../processor/TestableInjectProcessor.java | 14 ++++- .../testable/processor/TestableProcessor.java | 11 +++- 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/alibaba/testable/processor/BaseProcessor.java diff --git a/pom.xml b/pom.xml index a22bb8d..1dc9bbb 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,6 @@ - org.apache.maven.plugins maven-compiler-plugin @@ -39,7 +38,6 @@ ${project.compiler.level} ${project.compiler.level} ${project.build.sourceEncoding} - -proc:none diff --git a/src/main/java/com/alibaba/testable/annotation/Testable.java b/src/main/java/com/alibaba/testable/annotation/Testable.java index 5eaf1b8..e8076d0 100644 --- a/src/main/java/com/alibaba/testable/annotation/Testable.java +++ b/src/main/java/com/alibaba/testable/annotation/Testable.java @@ -7,7 +7,7 @@ import java.lang.annotation.*; * On method, make the method testable * On field (in test class), fit the variable for unit test * - * @author linfan + * @author flin */ @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) diff --git a/src/main/java/com/alibaba/testable/annotation/TestableInject.java b/src/main/java/com/alibaba/testable/annotation/TestableInject.java index ae43884..64bf60d 100644 --- a/src/main/java/com/alibaba/testable/annotation/TestableInject.java +++ b/src/main/java/com/alibaba/testable/annotation/TestableInject.java @@ -5,7 +5,7 @@ import java.lang.annotation.*; /** * Use marked variable replace the ones in testable class * - * @author linfan + * @author flin */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.FIELD) diff --git a/src/main/java/com/alibaba/testable/processor/BaseProcessor.java b/src/main/java/com/alibaba/testable/processor/BaseProcessor.java new file mode 100644 index 0000000..3d286d2 --- /dev/null +++ b/src/main/java/com/alibaba/testable/processor/BaseProcessor.java @@ -0,0 +1,55 @@ +package com.alibaba.testable.processor; + +import com.sun.tools.javac.api.JavacTrees; +import com.sun.tools.javac.processing.JavacProcessingEnvironment; +import com.sun.tools.javac.tree.TreeMaker; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.Names; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; +import javax.annotation.processing.ProcessingEnvironment; + +/** + * @author flin + */ +public abstract class BaseProcessor extends AbstractProcessor { + + /** + * Messager used for printing log during compilation + */ + protected Messager messager; + + /** + * Filer used for generate source file + */ + protected Filer filter; + + /** + * JavacTrees provide the source AST + */ + protected JavacTrees trees; + + /** + * TreeMaker used for creating AST node + */ + protected TreeMaker treeMaker; + + /** + * Names used for creating resource name + */ + protected Names names; + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + Context context = ((JavacProcessingEnvironment)processingEnv).getContext(); + messager = processingEnv.getMessager(); + filter = processingEnv.getFiler(); + trees = JavacTrees.instance(processingEnv); + treeMaker = TreeMaker.instance(context); + names = Names.instance(context); + } + +} diff --git a/src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java b/src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java index 960a231..0f2f545 100644 --- a/src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java +++ b/src/main/java/com/alibaba/testable/processor/TestableInjectProcessor.java @@ -1,15 +1,23 @@ package com.alibaba.testable.processor; -import javax.annotation.processing.AbstractProcessor; 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; -public class TestableInjectProcessor extends AbstractProcessor { +/** + * @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) { - return false; + System.out.println("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 15d2236..da84f97 100644 --- a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java +++ b/src/main/java/com/alibaba/testable/processor/TestableProcessor.java @@ -1,11 +1,18 @@ package com.alibaba.testable.processor; -import javax.annotation.processing.AbstractProcessor; 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; -public class TestableProcessor extends AbstractProcessor { +/** + * @author flin + */ +@SupportedAnnotationTypes("com.alibaba.testable.annotation.Testable") +@SupportedSourceVersion(SourceVersion.RELEASE_7) +public class TestableProcessor extends BaseProcessor { @Override public boolean process(Set annotations, RoundEnvironment roundEnv) {