mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-25 11:51:15 +08:00
call super method
This commit is contained in:
parent
23487b26c9
commit
6a58e4f301
@ -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<String> 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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
23
src/main/java/com/alibaba/testable/util/StringUtil.java
Normal file
23
src/main/java/com/alibaba/testable/util/StringUtil.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.alibaba.testable.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class StringUtil {
|
||||
|
||||
static public String join(List<String> 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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user