From a8a08c4fd71ffebf0edae1cfa07d7e778f4eb1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Tue, 12 May 2020 22:37:27 +0800 Subject: [PATCH] reflect call with parameter --- .../testable/generator/CallSuperMethod.java | 76 +++++++++++++++++++ .../testable/processor/CallSuperMethod.java | 56 -------------- .../testable/processor/TestableProcessor.java | 1 + 3 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/alibaba/testable/generator/CallSuperMethod.java delete mode 100644 src/main/java/com/alibaba/testable/processor/CallSuperMethod.java diff --git a/src/main/java/com/alibaba/testable/generator/CallSuperMethod.java b/src/main/java/com/alibaba/testable/generator/CallSuperMethod.java new file mode 100644 index 0000000..8c72fd4 --- /dev/null +++ b/src/main/java/com/alibaba/testable/generator/CallSuperMethod.java @@ -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 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 args, StringBuilder code) { + List 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 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(")"); + } +} diff --git a/src/main/java/com/alibaba/testable/processor/CallSuperMethod.java b/src/main/java/com/alibaba/testable/processor/CallSuperMethod.java deleted file mode 100644 index 1dd7ed3..0000000 --- a/src/main/java/com/alibaba/testable/processor/CallSuperMethod.java +++ /dev/null @@ -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 args = new ArrayList<>(); - List 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; - } -} diff --git a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java b/src/main/java/com/alibaba/testable/processor/TestableProcessor.java index 6bee204..b21f64b 100644 --- a/src/main/java/com/alibaba/testable/processor/TestableProcessor.java +++ b/src/main/java/com/alibaba/testable/processor/TestableProcessor.java @@ -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.*;