[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.jdt] Re: How to remove parameter using ASTParser

Fayezin Islam wrote:
Hello,

Suppose, I have a method invocation like -
class1.method1(par1, par2, par3);

I want to change it to:
class1.method1(par2);

How can I do this using the Eclipse ASTParser?

The parameters to be removed are decided by Static Analysis (Program Slicing).

Please note that I should be able to change invocations like these ase well -

class1.method1(new Object(), par2, par3);

Previously, I have analyzed code using the ASTParser but did not transformed/change code as of yet.

Therefore, a reply with an example will be very appreciated...

Thanks,

Fayezin


I don't have an example that does exactly what you're looking for, but the basic steps would be:

- Create a visitor class (extends ASTVisitor); call it something meaningful (like ArgumentRemovalVisitor), as if you work with the AST a lot you'll probably end up creating lots of these.
- Implement a method to visit MethodInvocations:
public boolean visit(MethodInvocation node) { ... }
- Within the visit method:
- check node.getName().getIdentifier() to find the methods you want to alter
- node.arguments() will then be the live list of argument nodes, from which you can delete any arguments that aren't wanted.


- to invoke your visitor, do something like:
	ArgumentRemovalVisitor arv = new ArgumentRemovalVisitor();
	myCompilationUnit.accept(arv);

The compilation unit in this case is a CompilationUnit, as returned by ASTParser.createAST(), not an ICompilationUnit.