Search code examples
javaeclipseeclipse-pluginabstract-syntax-treeeclipse-jdt

AST eclipse, method call with arguments


I want to add below code using ASTRewrite in java file. where name is a variable

printName(name);

I am trying below code for this

 MethodInvocation newInvocation = ast.newMethodInvocation();
 SimpleName methodAdd = ast.newSimpleName("printName");
 newInvocation.setName(methodAdd);
 Statement newStatement = ast.newExpressionStatement(newInvocation);
 listRewrite = rewriter.getListRewrite(block,Block.STATEMENTS_PROPERTY);
 listRewrite.insertFirst(newStatement, null);

output of this code is

 printName();

Can anyone help, how to add argument in method call.


Solution

  • Just add a Name to the arguments List of the MethodInvocation.

    newInvocation.arguments().add(ast.newSimpleName("name"));