let processor accessable

This commit is contained in:
金戟 2020-05-09 16:26:19 +08:00
parent a23cdb29d9
commit 5e2e4811fd
6 changed files with 77 additions and 9 deletions

View File

@ -30,7 +30,6 @@
<build>
<plugins>
<!--compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -39,7 +38,6 @@
<source>${project.compiler.level}</source>
<target>${project.compiler.level}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<!--指定为不编译-->
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>

View File

@ -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})

View File

@ -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)

View File

@ -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);
}
}

View File

@ -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<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
System.out.println("Enter TestableInjectProcessor !");
return true;
}
}

View File

@ -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<? extends TypeElement> annotations, RoundEnvironment roundEnv) {