[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.tools.jdt] Re: How to remove parameter using ASTParser
|
Thanks for your help Mike.
I have done as per your suggestions and it works. But there is a small
problem.
In the ASTVisitor, I am changing arguments of method invocations that match
the method name. But what if another class has the same method name which I
dont want to change?
For example, I want to change all invocation of Class1.method1(int i, String
s) only.
But when I run my program, it also changes the invocations of -
Class2 c = new Class2();
c.method1(10, "Hello");
So, I thought it would be better to check both method name of the invocation
and class name of the object instance making the call.
Please help me find out how can I achieve this.
Looking forward to your help.
Thanks,
Fayezin Islam
"Mike Yawn" <myawn@xxxxxxxx> wrote in message
news:46B8910D.8080108@xxxxxxxxxxx
> 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.
>
>
>