use testable annotation for both dev and test role

This commit is contained in:
金戟 2020-05-16 12:29:37 +08:00
parent 44eda2be06
commit 12ddb9398c
8 changed files with 30 additions and 47 deletions

View File

@ -10,7 +10,7 @@ import java.lang.annotation.*;
* @author flin * @author flin
*/ */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) @Target(ElementType.TYPE)
@Documented @Documented
public @interface Testable { public @interface Testable {

View File

@ -8,7 +8,7 @@ import java.lang.annotation.*;
* @author flin * @author flin
*/ */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD) @Target(ElementType.METHOD)
@Documented @Documented
public @interface TestableInject { public @interface TestableInject {
} }

View File

@ -14,7 +14,7 @@ import java.util.List;
* *
* @author flin * @author flin
*/ */
public class CallSuperMethodStatement { public class CallSuperMethodStatementGenerator {
public Statement[] fetch(String className, JCTree.JCMethodDecl method) { public Statement[] fetch(String className, JCTree.JCMethodDecl method) {
if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) { if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) {

View File

@ -1,7 +1,7 @@
package com.alibaba.testable.generator; package com.alibaba.testable.generator;
import com.alibaba.testable.generator.model.Statement; 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.alibaba.testable.util.ConstPool;
import com.squareup.javapoet.*; import com.squareup.javapoet.*;
import com.sun.tools.javac.api.JavacTrees; import com.sun.tools.javac.api.JavacTrees;
@ -21,19 +21,19 @@ import java.util.Set;
* *
* @author flin * @author flin
*/ */
public class TestableClassGenerator { public class TestableClassDevRoleGenerator {
private final JavacTrees trees; private final JavacTrees trees;
private final TreeMaker treeMaker; private final TreeMaker treeMaker;
public TestableClassGenerator(JavacTrees trees, TreeMaker treeMaker) { public TestableClassDevRoleGenerator(JavacTrees trees, TreeMaker treeMaker) {
this.trees = trees; this.trees = trees;
this.treeMaker = treeMaker; this.treeMaker = treeMaker;
} }
public String fetch(Element clazz, String packageName, String className) { public String fetch(Element clazz, String packageName, String className) {
JCTree tree = trees.getTree(clazz); JCTree tree = trees.getTree(clazz);
TestableClassTranslator translator = new TestableClassTranslator(treeMaker); TestableClassDevRoleTranslator translator = new TestableClassDevRoleTranslator(treeMaker);
tree.accept(translator); tree.accept(translator);
List<MethodSpec> methodSpecs = new ArrayList<>(); List<MethodSpec> methodSpecs = new ArrayList<>();
@ -90,7 +90,7 @@ public class TestableClassGenerator {
private void addCallSuperStatements(MethodSpec.Builder builder, Element classElement, JCTree.JCMethodDecl method) { private void addCallSuperStatements(MethodSpec.Builder builder, Element classElement, JCTree.JCMethodDecl method) {
String className = classElement.getSimpleName().toString(); String className = classElement.getSimpleName().toString();
Statement[] statements = new CallSuperMethodStatement().fetch(className, method); Statement[] statements = new CallSuperMethodStatementGenerator().fetch(className, method);
for (Statement s : statements) { for (Statement s : statements) {
builder.addStatement(s.getLine(), s.getParams()); builder.addStatement(s.getLine(), s.getParams());
} }

View File

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

View File

@ -2,8 +2,8 @@ package com.alibaba.testable.processor;
import com.alibaba.testable.annotation.Testable; import com.alibaba.testable.annotation.Testable;
import com.alibaba.testable.generator.StaticNewClassGenerator; import com.alibaba.testable.generator.StaticNewClassGenerator;
import com.alibaba.testable.generator.TestableClassGenerator; import com.alibaba.testable.generator.TestableClassDevRoleGenerator;
import com.alibaba.testable.translator.TestableFieldTranslator; import com.alibaba.testable.translator.TestableClassTestRoleTranslator;
import com.alibaba.testable.util.ConstPool; import com.alibaba.testable.util.ConstPool;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
@ -39,9 +39,11 @@ public class TestableProcessor extends BaseProcessor {
createStaticNewClass(); createStaticNewClass();
for (Element element : elements) { for (Element element : elements) {
if (element.getKind().isClass()) { if (element.getKind().isClass()) {
processClassElement(element); if (isTestClass(element.getSimpleName())) {
} else if (element.getKind().isField()) { processTestRoleClassElement(element);
processFieldElement(element); } else {
processDevRoleClassElement(element);
}
} }
} }
return true; 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) { private boolean isCompilingTestClass(FileObject staticNewClassFile) {
return staticNewClassFile.getName().contains(GENERATED_TEST_SOURCES); return staticNewClassFile.getName().contains(GENERATED_TEST_SOURCES);
} }
private void processFieldElement(Element field) { private void processDevRoleClassElement(Element clazz) {
JCTree tree = trees.getTree(field);
tree.accept(new TestableFieldTranslator(treeMaker));
}
private void processClassElement(Element clazz) {
String packageName = elementUtils.getPackageOf(clazz).getQualifiedName().toString(); String packageName = elementUtils.getPackageOf(clazz).getQualifiedName().toString();
String testableTypeName = getTestableClassName(clazz.getSimpleName()); String testableTypeName = getTestableClassName(clazz.getSimpleName());
String fullQualityTypeName = packageName + "." + testableTypeName; String fullQualityTypeName = packageName + "." + testableTypeName;
try { try {
writeSourceFile(fullQualityTypeName, writeSourceFile(fullQualityTypeName,
new TestableClassGenerator(trees, treeMaker).fetch(clazz, packageName, testableTypeName)); new TestableClassDevRoleGenerator(trees, treeMaker).fetch(clazz, packageName, testableTypeName));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); 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 { private void writeSourceFile(String fullQualityTypeName, String content) throws IOException {
JavaFileObject jfo = filter.createSourceFile(fullQualityTypeName); JavaFileObject jfo = filter.createSourceFile(fullQualityTypeName);
Writer writer = jfo.openWriter(); Writer writer = jfo.openWriter();

View File

@ -18,7 +18,7 @@ import java.lang.reflect.Modifier;
* *
* @author flin * @author flin
*/ */
public class TestableClassTranslator extends TreeTranslator { public class TestableClassDevRoleTranslator extends TreeTranslator {
private final TreeMaker treeMaker; private final TreeMaker treeMaker;
@ -31,7 +31,7 @@ public class TestableClassTranslator extends TreeTranslator {
return methods; return methods;
} }
public TestableClassTranslator(TreeMaker treeMaker) { public TestableClassDevRoleTranslator(TreeMaker treeMaker) {
this.treeMaker = treeMaker; this.treeMaker = treeMaker;
} }

View File

@ -11,11 +11,11 @@ import com.sun.tools.javac.util.Name;
* *
* @author flin * @author flin
*/ */
public class TestableFieldTranslator extends TreeTranslator { public class TestableClassTestRoleTranslator extends TreeTranslator {
private TreeMaker treeMaker; private TreeMaker treeMaker;
public TestableFieldTranslator(TreeMaker treeMaker) {this.treeMaker = treeMaker;} public TestableClassTestRoleTranslator(TreeMaker treeMaker) {this.treeMaker = treeMaker;}
@Override @Override
public void visitVarDef(JCTree.JCVariableDecl decl) { public void visitVarDef(JCTree.JCVariableDecl decl) {