mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
reflect call with parameter
This commit is contained in:
parent
f45c7a34bc
commit
a8a08c4fd7
@ -0,0 +1,76 @@
|
||||
package com.alibaba.testable.generator;
|
||||
|
||||
import com.alibaba.testable.util.ConstPool;
|
||||
import com.alibaba.testable.util.StringUtil;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Generate call super method statement
|
||||
*
|
||||
* @author flin
|
||||
*/
|
||||
public class CallSuperMethod {
|
||||
private final String className;
|
||||
private final JCTree.JCMethodDecl method;
|
||||
private Object[] params;
|
||||
private String statement;
|
||||
|
||||
public CallSuperMethod(String className, JCTree.JCMethodDecl method) {
|
||||
this.className = className;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public Object[] getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public String getStatement() {
|
||||
return statement;
|
||||
}
|
||||
|
||||
public CallSuperMethod invoke() {
|
||||
List<Object> args = new ArrayList<>();
|
||||
StringBuilder code = new StringBuilder();
|
||||
if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) {
|
||||
reflectCall(args, code);
|
||||
} else {
|
||||
commonCall(args, code);
|
||||
}
|
||||
statement = code.toString();
|
||||
params = args.toArray();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void commonCall(List<Object> args, StringBuilder code) {
|
||||
List<String> placeholders = new ArrayList<>();
|
||||
for (JCTree.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(")");
|
||||
}
|
||||
|
||||
private void reflectCall(List<Object> args, StringBuilder code) {
|
||||
if (!method.restype.toString().equals(ConstPool.CONSTRUCTOR_VOID)) {
|
||||
code.append("(").append(method.restype).append(")");
|
||||
}
|
||||
code.append(className).append(".class.getMethod(\"").append(method.name).append("\"");
|
||||
for (JCTree.JCVariableDecl p : method.params) {
|
||||
code.append(", $T.class");
|
||||
args.add(p.sym.type);
|
||||
}
|
||||
code.append(").invoke(this");
|
||||
for (JCTree.JCVariableDecl p : method.params) {
|
||||
code.append(", ").append(p.name);
|
||||
}
|
||||
code.append(")");
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
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 javax.lang.model.element.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author flin
|
||||
*/
|
||||
public class CallSuperMethod {
|
||||
private final String className;
|
||||
private final JCTree.JCMethodDecl method;
|
||||
private Object[] params;
|
||||
private String statement;
|
||||
|
||||
public CallSuperMethod(String className, JCTree.JCMethodDecl method) {
|
||||
this.className = className;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
|
||||
public Object[] getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public String getStatement() {
|
||||
return statement;
|
||||
}
|
||||
|
||||
public CallSuperMethod invoke() {
|
||||
List<Object> args = new ArrayList<>();
|
||||
List<String> placeholders = new ArrayList<>();
|
||||
if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) {
|
||||
statement = className + ".class.getMethod(\"" + method.name + "\").invoke(this)";
|
||||
if (!method.restype.toString().equals(ConstPool.CONSTRUCTOR_VOID)) {
|
||||
statement = "(" + method.restype + ")" + statement;
|
||||
}
|
||||
} else {
|
||||
for (JCTree.JCVariableDecl p : method.params) {
|
||||
args.add(p.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, ", ") + ")";
|
||||
}
|
||||
params = args.toArray();
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.alibaba.testable.processor;
|
||||
|
||||
import com.alibaba.testable.annotation.Testable;
|
||||
import com.alibaba.testable.generator.CallSuperMethod;
|
||||
import com.alibaba.testable.translator.TestableTreeTranslator;
|
||||
import com.alibaba.testable.util.ConstPool;
|
||||
import com.squareup.javapoet.*;
|
||||
|
Loading…
Reference in New Issue
Block a user