rename annotations

This commit is contained in:
金戟 2020-05-21 22:21:17 +08:00
parent 70b68425b4
commit fa79075055
7 changed files with 92 additions and 49 deletions

View File

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

View File

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

View File

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

View File

@ -3,7 +3,7 @@ package com.alibaba.testable.annotation;
import java.lang.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 * @author flin
*/ */

View File

@ -1,12 +1,10 @@
package com.alibaba.testable.processor; 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.StaticNewClassGenerator;
import com.alibaba.testable.generator.TestableClassDevRoleGenerator; import com.alibaba.testable.generator.TestableClassDevRoleGenerator;
import com.alibaba.testable.translator.TestableClassTestRoleTranslator;
import com.alibaba.testable.util.ConstPool; import com.alibaba.testable.util.ConstPool;
import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import javax.annotation.processing.FilerException; import javax.annotation.processing.FilerException;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
@ -27,24 +25,20 @@ import static javax.tools.StandardLocation.SOURCE_OUTPUT;
/** /**
* @author flin * @author flin
*/ */
@SupportedAnnotationTypes("com.alibaba.testable.annotation.Testable") @SupportedAnnotationTypes("com.alibaba.testable.annotation.EnableTestableInject")
@SupportedSourceVersion(SourceVersion.RELEASE_7) @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 JAVA_POSTFIX = ".java";
private static final String GENERATED_TEST_SOURCES = "generated-test-sources"; private static final String GENERATED_TEST_SOURCES = "generated-test-sources";
@Override @Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Testable.class); Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(EnableTestableInject.class);
createStaticNewClass(); createStaticNewClass();
for (Element element : elements) { for (Element element : elements) {
if (element.getKind().isClass()) { if (element.getKind().isClass()) {
if (isTestClass(element.getSimpleName())) { processClassElement((Symbol.ClassSymbol)element);
processTestRoleClassElement((Symbol.ClassSymbol)element);
} else {
processDevRoleClassElement((Symbol.ClassSymbol)element);
}
} }
} }
return true; 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) { private boolean isCompilingTestClass(FileObject staticNewClassFile) {
return staticNewClassFile.getName().contains(GENERATED_TEST_SOURCES); 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 packageName = cx.elementUtils.getPackageOf(clazz).getQualifiedName().toString();
String testableTypeName = getTestableClassName(clazz.getSimpleName()); String testableTypeName = getTestableClassName(clazz.getSimpleName());
String fullQualityTypeName = packageName + "." + testableTypeName; 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 { private void writeSourceFile(String fullQualityTypeName, String content) throws IOException {
JavaFileObject jfo = cx.filter.createSourceFile(fullQualityTypeName); JavaFileObject jfo = cx.filter.createSourceFile(fullQualityTypeName);
Writer writer = jfo.openWriter(); Writer writer = jfo.openWriter();

View File

@ -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<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> 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());
}
}

View File

@ -1 +1,2 @@
com.alibaba.testable.processor.TestableProcessor com.alibaba.testable.processor.EnableTestableProcessor
com.alibaba.testable.processor.EnableTestableInjectProcessor