[英]AST eclipse, adding arguments to a MethodInvocation
我试图将参数添加到MethodInvocation的参数列表中,它似乎不起作用,我可以删除对象,但看不到要添加它们。 我的最终目标是采用2个MethodInvocation,它们使用不同的参数调用相同的方法,并将其转换为1个具有ConditionalExpression作为参数的MethodInvocation。 例:
if (A){
System.out.println("hi");
} else {
System.out.println("hey");
}
将转换为:
System.out.println((A ? "hi" : "hey"));
因此,如果有人知道如何将参数列表转换为1个可以在ConditionalExpression中放置的大表达式,我也将不胜感激。
谢谢!
编辑:抱歉忘了提及它是ecplise的代码格式化插件
EDIT2:我尝试运行的代码:
final ExpressionStatement thenStmnt=(ExpressionStatement)((Block)node.getThenStatement()).statements().get(0),
elseStmnt=(ExpressionStatement)((Block)node.getElseStatement()).statements().get(0);
MethodInvocation thenMethod=(MethodInvocation)thenStmnt.getExpression(),
elseMethod=(MethodInvocation)elseStmnt.getExpression();
final MethodInvocation method=ast.newMethodInvocation();
method.setName(ast.newSimpleName("add"));
method.arguments().add(0, elseMethod.arguments().get(0));
ast是给定的lea AST,node是给定的leaIfIfStatement。
解决了,问题出在这里:
method.arguments().add(0, elseMethod.arguments().get(0));
如果要获取或复制原始代码中已经包含的内容(意味着AST中已经存在),则必须使用r.createCopyTarget,如下所示:
method.arguments().add(0, r.createCopyTarget(elseMethod.arguments().get(0)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.