From 066107372b72b14ced880d26821ca8ec953fbf79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Wed, 13 May 2020 22:32:36 +0800 Subject: [PATCH] generate static new method --- ...tor.java => CallSuperMethodStatement.java} | 2 +- .../generator/TestableClassGenerator.java | 31 ++++++++++++++++--- .../com/alibaba/testable/util/ConstPool.java | 3 +- 3 files changed, 29 insertions(+), 7 deletions(-) rename src/main/java/com/alibaba/testable/generator/{CallSuperMethodStatementGenerator.java => CallSuperMethodStatement.java} (98%) diff --git a/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatementGenerator.java b/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatement.java similarity index 98% rename from src/main/java/com/alibaba/testable/generator/CallSuperMethodStatementGenerator.java rename to src/main/java/com/alibaba/testable/generator/CallSuperMethodStatement.java index e52bbe6..50823c9 100644 --- a/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatementGenerator.java +++ b/src/main/java/com/alibaba/testable/generator/CallSuperMethodStatement.java @@ -14,7 +14,7 @@ import java.util.List; * * @author flin */ -public class CallSuperMethodStatementGenerator { +public class CallSuperMethodStatement { public Statement[] fetch(String className, JCTree.JCMethodDecl method) { if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) { diff --git a/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java b/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java index 3963f42..26d246d 100644 --- a/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java +++ b/src/main/java/com/alibaba/testable/generator/TestableClassGenerator.java @@ -34,6 +34,7 @@ public class TestableClassGenerator { tree.accept(translator); List methodSpecs = new ArrayList<>(); + methodSpecs.add(buildStaticNewMethod(clazz)); for (JCTree.JCMethodDecl method : translator.getMethods()) { if (isNoncallableMethod(method)) { continue; @@ -56,6 +57,28 @@ public class TestableClassGenerator { return javaFile.toString(); } + private MethodSpec buildStaticNewMethod(Element clazz) { + TypeVariableName typeVariable = TypeVariableName.get("T"); + MethodSpec.Builder builder = MethodSpec.methodBuilder("New") + .addModifiers(Modifier.PUBLIC).addModifiers(Modifier.STATIC) + .addException(Exception.class) + .addTypeVariable(typeVariable) + .varargs(true) + .addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), typeVariable), "type") + .addParameter(ArrayTypeName.of(Object.class), "args") + .returns(typeVariable); + addStaticNewMethodStatement(builder); + return builder.build(); + } + + private void addStaticNewMethodStatement(MethodSpec.Builder builder) { + builder.addStatement("$T<$T> pts = new $T<>()", List.class, Class.class, ArrayList.class) + .beginControlFlow("for (Object o : args)") + .addStatement("pts.add(o.getClass())") + .endControlFlow() + .addStatement("return type.getConstructor(pts.toArray(new Class[0])).newInstance(args)"); + } + private MethodSpec buildMemberMethod(Element classElement, JCTree.JCMethodDecl method) { MethodSpec.Builder builder = MethodSpec.methodBuilder(method.name.toString()) .addModifiers(toPublicFlags(method.getModifiers())) @@ -71,7 +94,7 @@ public class TestableClassGenerator { builder.addException(TypeName.get(exception.type)); } } - addStatements(builder, classElement, method); + addCallSuperStatements(builder, classElement, method); return builder.build(); } @@ -81,13 +104,13 @@ public class TestableClassGenerator { for (JCTree.JCVariableDecl p : method.getParameters()) { builder.addParameter(getParameterSpec(p)); } - addStatements(builder, classElement, method); + addCallSuperStatements(builder, classElement, method); return builder.build(); } - private void addStatements(MethodSpec.Builder builder, Element classElement, JCTree.JCMethodDecl method) { + private void addCallSuperStatements(MethodSpec.Builder builder, Element classElement, JCTree.JCMethodDecl method) { String className = classElement.getSimpleName().toString(); - Statement[] statements = new CallSuperMethodStatementGenerator().fetch(className, method); + Statement[] statements = new CallSuperMethodStatement().fetch(className, method); for (Statement s : statements) { builder.addStatement(s.getLine(), s.getParams()); } diff --git a/src/main/java/com/alibaba/testable/util/ConstPool.java b/src/main/java/com/alibaba/testable/util/ConstPool.java index bd3f350..a3d05cb 100644 --- a/src/main/java/com/alibaba/testable/util/ConstPool.java +++ b/src/main/java/com/alibaba/testable/util/ConstPool.java @@ -1,7 +1,5 @@ package com.alibaba.testable.util; -import com.sun.tools.javac.tree.JCTree; - /** * @author flin */ @@ -10,4 +8,5 @@ public final class ConstPool { public static final String CONSTRUCTOR_NAME = ""; public static final String TYPE_VOID = "void"; public static final String TESTABLE = "Testable"; + }