diff --git a/src/main/java/com/alibaba/testable/processor/CallSuperMethod.java b/src/main/java/com/alibaba/testable/processor/CallSuperMethod.java new file mode 100644 index 0000000..ec229a5 --- /dev/null +++ b/src/main/java/com/alibaba/testable/processor/CallSuperMethod.java @@ -0,0 +1,42 @@ +package com.alibaba.testable.processor; + +import com.alibaba.testable.util.ConstPool; +import com.alibaba.testable.util.StringUtil; +import com.sun.tools.javac.tree.JCTree; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author flin + */ +public class CallSuperMethod { + private final JCTree.JCMethodDecl method; + private Object[] params; + private String statement; + + public CallSuperMethod(JCTree.JCMethodDecl method) {this.method = method;} + + public Object[] getParams() { + return params; + } + + public String getStatement() { + return statement; + } + + public CallSuperMethod invoke() { + params = new String[method.params.length()]; + List placeholders = new ArrayList<>(); + for (int i = 0; i < method.params.length(); i++) { + params[i] = (method.params.get(i).name.toString()); + placeholders.add("$N"); + } + String call = "super"; + if (!method.name.toString().equals(ConstPool.CONSTRUCTOR_NAME)) { + call += ("." + method.name.toString()); + } + statement = call + "(" + StringUtil.join(placeholders, ", ") + ")"; + return this; + } +} diff --git a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java b/src/main/java/com/alibaba/testable/processor/TestableProcessor.java index 165550e..82bb1cc 100644 --- a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java +++ b/src/main/java/com/alibaba/testable/processor/TestableProcessor.java @@ -35,7 +35,6 @@ public class TestableProcessor extends BaseProcessor { String packageName = elementUtils.getPackageOf(classElement).getQualifiedName().toString(); String testableTypeName = classElement.getSimpleName().toString().replace(".", "_") + "Testable"; String fullQualityTypeName = packageName + "." + testableTypeName; - info(">>> generating: " + fullQualityTypeName); try { JavaFileObject jfo = filter.createSourceFile(fullQualityTypeName); Writer writer = jfo.openWriter(); @@ -60,18 +59,22 @@ public class TestableProcessor extends BaseProcessor { MethodSpec.Builder builder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC); for (JCTree.JCVariableDecl p : method.getParameters()) { - builder.addParameter(getParameterSpec(p.type)); + builder.addParameter(getParameterSpec(p)); } + CallSuperMethod callSuperMethod = new CallSuperMethod(method).invoke(); + builder.addStatement(callSuperMethod.getStatement(), callSuperMethod.getParams()); methodSpecs.add(builder.build()); } else { MethodSpec.Builder builder = MethodSpec.methodBuilder(method.name.toString()) .addModifiers(method.getModifiers().getFlags()) .addModifiers(Modifier.PUBLIC) - .returns(method.restype.getClass()); + .returns(TypeName.get(((Type.MethodType)method.sym.type).restype)); for (JCTree.JCVariableDecl p : method.getParameters()) { - builder.addParameter(getParameterSpec(p.type)); + builder.addParameter(getParameterSpec(p)); } - builder.addStatement("$T.out.println($S)", System.class, "Hello, Testable !"); + CallSuperMethod callSuperMethod = new CallSuperMethod(method).invoke(); + String statement = method.restype == null ? callSuperMethod.getStatement() : "return " + callSuperMethod.getStatement(); + builder.addStatement(statement, callSuperMethod.getParams()); methodSpecs.add(builder.build()); } } @@ -87,10 +90,8 @@ public class TestableProcessor extends BaseProcessor { return javaFile.toString(); } - private ParameterSpec getParameterSpec(Type type) { - return ParameterSpec.builder(String.class, "placeholder") - .addModifiers(Modifier.PUBLIC) - .build(); + private ParameterSpec getParameterSpec(JCTree.JCVariableDecl type) { + return ParameterSpec.builder(TypeName.get(type.sym.type), type.name.toString()).build(); } } diff --git a/src/main/java/com/alibaba/testable/util/StringUtil.java b/src/main/java/com/alibaba/testable/util/StringUtil.java new file mode 100644 index 0000000..7e85382 --- /dev/null +++ b/src/main/java/com/alibaba/testable/util/StringUtil.java @@ -0,0 +1,23 @@ +package com.alibaba.testable.util; + +import java.util.List; + +public class StringUtil { + + static public String join(List list, String conjunction) + { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (String item : list) + { + if (first) { + first = false; + } else { + sb.append(conjunction); + } + sb.append(item); + } + return sb.toString(); + } + +}