From df0e6766aaf79dc14b716323bcd65c4645778394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Fri, 22 May 2020 21:33:16 +0800 Subject: [PATCH] testable class is not needed any more --- .../generator/TestableClassGenerator.java | 121 ------------------ .../testable/generator/model/Statement.java | 28 ---- .../CallSuperMethodStatementGenerator.java | 88 ------------- .../EnableTestableInjectProcessor.java | 20 +-- 4 files changed, 5 insertions(+), 252 deletions(-) delete mode 100644 src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java delete mode 100644 src/main/java/com/alibaba/testable/generator/model/Statement.java delete mode 100644 src/main/java/com/alibaba/testable/generator/statement/CallSuperMethodStatementGenerator.java diff --git a/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java b/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java deleted file mode 100644 index 77330c1..0000000 --- a/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.alibaba.testable.generator; - -import com.alibaba.testable.generator.model.Statement; -import com.alibaba.testable.generator.statement.CallSuperMethodStatementGenerator; -import com.alibaba.testable.model.TestableContext; -import com.alibaba.testable.translator.EnableTestableInjectTranslator; -import com.alibaba.testable.util.ConstPool; -import com.squareup.javapoet.*; -import com.sun.tools.javac.code.Symbol; -import com.sun.tools.javac.code.Type; -import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.tree.JCTree.JCExpression; -import com.sun.tools.javac.tree.JCTree.JCMethodDecl; -import com.sun.tools.javac.tree.JCTree.JCModifiers; -import com.sun.tools.javac.tree.JCTree.JCVariableDecl; - -import javax.lang.model.element.Element; -import javax.lang.model.element.Modifier; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Generate testable class code - * - * @author flin - */ -public class TestableClassGenerator { - - private final TestableContext cx; - - public TestableClassGenerator(TestableContext cx) { - this.cx = cx; - } - - public String fetch(Symbol.ClassSymbol clazz, String packageName, String className) { - JCTree tree = cx.trees.getTree(clazz); - EnableTestableInjectTranslator translator = new EnableTestableInjectTranslator(cx); - tree.accept(translator); - - List methodSpecs = new ArrayList<>(); - for (JCMethodDecl method : translator.getMethods()) { - if (isNoncallableMethod(method)) { - continue; - } - if (isConstructorMethod(method)) { - methodSpecs.add(buildConstructorMethod(clazz, method)); - } else { - methodSpecs.add(buildMemberMethod(clazz, method)); - } - } - - TypeSpec.Builder builder = TypeSpec.classBuilder(className) - .addModifiers(Modifier.PUBLIC, Modifier.FINAL) - .superclass(clazz.asType()); - for (MethodSpec m : methodSpecs) { - builder.addMethod(m); - } - TypeSpec testableClass = builder.build(); - JavaFile javaFile = JavaFile.builder(packageName, testableClass).build(); - return javaFile.toString(); - } - - private MethodSpec buildMemberMethod(Element classElement, JCMethodDecl method) { - MethodSpec.Builder builder = MethodSpec.methodBuilder(method.name.toString()) - .addModifiers(toPublicFlags(method.getModifiers())) - .returns(TypeName.get(((Type.MethodType)method.sym.type).restype)); - for (JCVariableDecl p : method.getParameters()) { - builder.addParameter(getParameterSpec(p)); - } - if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) { - builder.addException(Exception.class); - } else { - builder.addAnnotation(Override.class); - for (JCExpression exception : method.getThrows()) { - builder.addException(TypeName.get(exception.type)); - } - } - addCallSuperStatements(builder, classElement, method); - return builder.build(); - } - - private MethodSpec buildConstructorMethod(Element classElement, JCMethodDecl method) { - MethodSpec.Builder builder = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC); - for (JCVariableDecl p : method.getParameters()) { - builder.addParameter(getParameterSpec(p)); - } - addCallSuperStatements(builder, classElement, method); - return builder.build(); - } - - private void addCallSuperStatements(MethodSpec.Builder builder, Element classElement, JCMethodDecl method) { - String className = classElement.getSimpleName().toString(); - Statement[] statements = new CallSuperMethodStatementGenerator().fetch(className, method); - for (Statement s : statements) { - builder.addStatement(s.getLine(), s.getParams()); - } - } - - private boolean isConstructorMethod(JCMethodDecl method) { - return method.name.toString().equals(ConstPool.CONSTRUCTOR_NAME); - } - - private boolean isNoncallableMethod(JCMethodDecl method) { - return method.getModifiers().getFlags().contains(Modifier.ABSTRACT); - } - - private Set toPublicFlags(JCModifiers modifiers) { - Set flags = new HashSet<>(modifiers.getFlags()); - flags.remove(Modifier.PRIVATE); - flags.remove(Modifier.PROTECTED); - flags.add(Modifier.PUBLIC); - return flags; - } - - private ParameterSpec getParameterSpec(JCVariableDecl type) { - return ParameterSpec.builder(TypeName.get(type.sym.type), type.name.toString()).build(); - } - -} diff --git a/src/main/java/com/alibaba/testable/generator/model/Statement.java b/src/main/java/com/alibaba/testable/generator/model/Statement.java deleted file mode 100644 index d353d51..0000000 --- a/src/main/java/com/alibaba/testable/generator/model/Statement.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.alibaba.testable.generator.model; - -public class Statement { - - private String line; - private Object[] params; - - public String getLine() { - return line; - } - - public void setLine(String line) { - this.line = line; - } - - public Object[] getParams() { - return params; - } - - public void setParams(Object[] params) { - this.params = params; - } - - public Statement(String line, Object[] params) { - this.line = line; - this.params = params; - } -} diff --git a/src/main/java/com/alibaba/testable/generator/statement/CallSuperMethodStatementGenerator.java b/src/main/java/com/alibaba/testable/generator/statement/CallSuperMethodStatementGenerator.java deleted file mode 100644 index 5eba964..0000000 --- a/src/main/java/com/alibaba/testable/generator/statement/CallSuperMethodStatementGenerator.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.alibaba.testable.generator.statement; - -import com.alibaba.testable.generator.model.Statement; -import com.alibaba.testable.util.ConstPool; -import com.alibaba.testable.util.StringUtil; -import com.sun.tools.javac.tree.JCTree.*; -import java.lang.reflect.Method; -import javax.lang.model.element.Modifier; -import java.util.ArrayList; -import java.util.List; - -/** - * Generate call super method statement - * - * @author flin - */ -public class CallSuperMethodStatementGenerator { - - public Statement[] fetch(String className, JCMethodDecl method) { - if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) { - return reflectCall(className, method); - } else { - return commonCall(method); - } - } - - private Statement[] commonCall(JCMethodDecl method) { - List args = new ArrayList<>(); - StringBuilder code = new StringBuilder(); - List placeholders = new ArrayList<>(); - for (JCVariableDecl p : method.params) { - args.add(p.name.toString()); - placeholders.add("$N"); - } - code.append("super"); - if (!method.name.toString().equals(ConstPool.CONSTRUCTOR_NAME)) { - code.append(".").append(method.name); - } - code.append("(").append(StringUtil.join(placeholders, ", ")).append(")"); - return new Statement[] { returnStatement(method, new Statement(code.toString(), args.toArray())) }; - } - - private Statement[] reflectCall(String className, JCMethodDecl method) { - List statements = new ArrayList<>(); - statements.add(getMethodStatement(className, method)); - statements.add(setAccessibleStatement()); - statements.add(returnStatement(method, invokeStatement(method))); - return statements.toArray(new Statement[0]); - } - - private Statement returnStatement(JCMethodDecl method, Statement statement) { - if (method.restype != null && !method.restype.toString().equals(ConstPool.TYPE_VOID)) { - statement.setLine("return " + statement.getLine()); - } - return statement; - } - - private Statement getMethodStatement(String className, JCMethodDecl method) { - List args = new ArrayList<>(); - StringBuilder code = new StringBuilder(); - code.append("$T m = "); - args.add(Method.class); - code.append(className).append(".class.getDeclaredMethod(\"").append(method.name).append("\""); - for (JCVariableDecl p : method.params) { - code.append(", $T.class"); - args.add(p.sym.type); - } - code.append(")"); - return new Statement(code.toString(), args.toArray()); - } - - private Statement setAccessibleStatement() { - return new Statement("m.setAccessible(true)", new Object[0]); - } - - private Statement invokeStatement(JCMethodDecl method) { - StringBuilder code = new StringBuilder(); - if (!method.restype.toString().equals(ConstPool.TYPE_VOID)) { - code.append("(").append(method.restype).append(")"); - } - code.append("m.invoke(this"); - for (JCVariableDecl p : method.params) { - code.append(", ").append(p.name); - } - code.append(")"); - return new Statement(code.toString(), new Object[0]); - } -} diff --git a/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java b/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java index 7212289..bd5b25f 100644 --- a/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java +++ b/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java @@ -2,9 +2,10 @@ package com.alibaba.testable.processor; import com.alibaba.testable.annotation.EnableTestableInject; import com.alibaba.testable.generator.StaticNewClassGenerator; -import com.alibaba.testable.generator.TestableClassGenerator; +import com.alibaba.testable.translator.EnableTestableInjectTranslator; 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; @@ -12,7 +13,6 @@ 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 javax.tools.FileObject; import javax.tools.JavaFileObject; @@ -71,15 +71,9 @@ public class EnableTestableInjectProcessor extends BaseProcessor { } private void processClassElement(Symbol.ClassSymbol clazz) { - String packageName = cx.elementUtils.getPackageOf(clazz).getQualifiedName().toString(); - String testableTypeName = getTestableClassName(clazz.getSimpleName()); - String fullQualityTypeName = packageName + "." + testableTypeName; - try { - writeSourceFile(fullQualityTypeName, - new TestableClassGenerator(cx).fetch(clazz, packageName, testableTypeName)); - } catch (IOException e) { - e.printStackTrace(); - } + JCTree tree = cx.trees.getTree(clazz); + EnableTestableInjectTranslator translator = new EnableTestableInjectTranslator(cx); + tree.accept(translator); } private void writeSourceFile(String fullQualityTypeName, String content) throws IOException { @@ -89,8 +83,4 @@ public class EnableTestableInjectProcessor extends BaseProcessor { writer.close(); } - private String getTestableClassName(Name className) { - return className.toString().replace(".", "_") + ConstPool.TESTABLE; - } - }