From 67eeb5251349658d057d76f3daf766922889f264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Fri, 15 May 2020 00:39:37 +0800 Subject: [PATCH] replace new operator with n.e.w method --- .../translator/TestableClassTranslator.java | 42 +++++++++++++++++++ .../translator/tree/TestableFieldAccess.java | 16 +++++++ .../tree/TestableMethodInvocation.java | 15 +++++++ .../com/alibaba/testable/util/ConstPool.java | 2 +- 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/alibaba/testable/translator/tree/TestableFieldAccess.java create mode 100644 src/main/java/com/alibaba/testable/translator/tree/TestableMethodInvocation.java diff --git a/src/main/java/com/alibaba/testable/translator/TestableClassTranslator.java b/src/main/java/com/alibaba/testable/translator/TestableClassTranslator.java index 2a302e6..0d27970 100644 --- a/src/main/java/com/alibaba/testable/translator/TestableClassTranslator.java +++ b/src/main/java/com/alibaba/testable/translator/TestableClassTranslator.java @@ -1,9 +1,15 @@ package com.alibaba.testable.translator; +import com.alibaba.testable.translator.tree.TestableFieldAccess; +import com.alibaba.testable.translator.tree.TestableMethodInvocation; +import com.alibaba.testable.util.ConstPool; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.TreeTranslator; import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.ListBuffer; +import com.sun.tools.javac.util.Name; import java.lang.reflect.Modifier; @@ -14,6 +20,8 @@ import java.lang.reflect.Modifier; */ public class TestableClassTranslator extends TreeTranslator { + private final TreeMaker treeMaker; + /** * Methods to inject */ @@ -23,6 +31,10 @@ public class TestableClassTranslator extends TreeTranslator { return methods; } + public TestableClassTranslator(TreeMaker treeMaker) { + this.treeMaker = treeMaker; + } + @Override public void visitClassDef(JCTree.JCClassDecl jcClassDecl) { super.visitClassDef(jcClassDecl); @@ -35,4 +47,34 @@ public class TestableClassTranslator extends TreeTranslator { methods = methods.append(jcMethodDecl); } + @Override + public void visitExec(JCTree.JCExpressionStatement jcExpressionStatement) { + if (jcExpressionStatement.expr.getClass().equals(JCTree.JCNewClass.class)) { + JCTree.JCNewClass newClassExpr = (JCTree.JCNewClass)jcExpressionStatement.expr; + Name className = ((JCTree.JCIdent)newClassExpr.clazz).name; + Name.Table nameTable = className.table; + try { + JCTree.JCExpression classType = new TestableFieldAccess(treeMaker.Ident(className), + nameTable.fromString("class"), null); + ListBuffer args = ListBuffer.of(classType); + args.addAll(newClassExpr.args); + jcExpressionStatement.expr = new TestableMethodInvocation(null, + new TestableFieldAccess(treeMaker.Ident(nameTable.fromString(ConstPool.SN_PKG_CLS)), + nameTable.fromString(ConstPool.SN_METHOD), null), args.toList()); + } catch (Exception e) { + e.printStackTrace(); + } + } + super.visitExec(jcExpressionStatement); + } + + @Override + public void visitApply(JCTree.JCMethodInvocation tree) { + super.visitApply(tree); + } + + @Override + public void visitNewClass(JCTree.JCNewClass jcNewClass) { + super.visitNewClass(jcNewClass); + } } diff --git a/src/main/java/com/alibaba/testable/translator/tree/TestableFieldAccess.java b/src/main/java/com/alibaba/testable/translator/tree/TestableFieldAccess.java new file mode 100644 index 0000000..6acc56d --- /dev/null +++ b/src/main/java/com/alibaba/testable/translator/tree/TestableFieldAccess.java @@ -0,0 +1,16 @@ +package com.alibaba.testable.translator.tree; + +import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.util.Name; + +/** + * @author flin + */ +public class TestableFieldAccess extends JCTree.JCFieldAccess { + + public TestableFieldAccess(JCExpression selected, Name name, Symbol sym) { + super(selected, name, sym); + } + +} diff --git a/src/main/java/com/alibaba/testable/translator/tree/TestableMethodInvocation.java b/src/main/java/com/alibaba/testable/translator/tree/TestableMethodInvocation.java new file mode 100644 index 0000000..fd7a452 --- /dev/null +++ b/src/main/java/com/alibaba/testable/translator/tree/TestableMethodInvocation.java @@ -0,0 +1,15 @@ +package com.alibaba.testable.translator.tree; + +import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.util.List; + +/** + * @author flin + */ +public class TestableMethodInvocation extends JCTree.JCMethodInvocation { + + public TestableMethodInvocation(List typeargs, JCExpression meth, List args) { + super(typeargs, meth, args); + } + +} diff --git a/src/main/java/com/alibaba/testable/util/ConstPool.java b/src/main/java/com/alibaba/testable/util/ConstPool.java index 69ec0a7..19c23c1 100644 --- a/src/main/java/com/alibaba/testable/util/ConstPool.java +++ b/src/main/java/com/alibaba/testable/util/ConstPool.java @@ -11,5 +11,5 @@ public final class ConstPool { public static final String SN_PKG = "n"; public static final String SN_CLS = "e"; public static final String SN_METHOD = "w"; - + public static final String SN_PKG_CLS = "n.e"; }