replace new operator with n.e.w method

This commit is contained in:
金戟 2020-05-15 00:39:37 +08:00
parent df55e05a57
commit 67eeb52513
4 changed files with 74 additions and 1 deletions

View File

@ -1,9 +1,15 @@
package com.alibaba.testable.translator; 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;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; 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.tree.TreeTranslator;
import com.sun.tools.javac.util.List; 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; import java.lang.reflect.Modifier;
@ -14,6 +20,8 @@ import java.lang.reflect.Modifier;
*/ */
public class TestableClassTranslator extends TreeTranslator { public class TestableClassTranslator extends TreeTranslator {
private final TreeMaker treeMaker;
/** /**
* Methods to inject * Methods to inject
*/ */
@ -23,6 +31,10 @@ public class TestableClassTranslator extends TreeTranslator {
return methods; return methods;
} }
public TestableClassTranslator(TreeMaker treeMaker) {
this.treeMaker = treeMaker;
}
@Override @Override
public void visitClassDef(JCTree.JCClassDecl jcClassDecl) { public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
super.visitClassDef(jcClassDecl); super.visitClassDef(jcClassDecl);
@ -35,4 +47,34 @@ public class TestableClassTranslator extends TreeTranslator {
methods = methods.append(jcMethodDecl); 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<JCTree.JCExpression> 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);
}
} }

View File

@ -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);
}
}

View File

@ -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<JCExpression> typeargs, JCExpression meth, List<JCExpression> args) {
super(typeargs, meth, args);
}
}

View File

@ -11,5 +11,5 @@ public final class ConstPool {
public static final String SN_PKG = "n"; public static final String SN_PKG = "n";
public static final String SN_CLS = "e"; public static final String SN_CLS = "e";
public static final String SN_METHOD = "w"; public static final String SN_METHOD = "w";
public static final String SN_PKG_CLS = "n.e";
} }