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

Hello Mike,

I have tried to do as you said, but I was not successful as I could not 
resolveBindings in the parser.

I am setting the source code on the parse as a char array. I have read the 
JDT API and it seems that I also have to specify IJavaProject and set a name 
for the compilation unit, which is not possible (at least I could not do 
it).

Please advice how can I get this done.

Thanks,

Fayezin

"Mike Yawn" <myawn@xxxxxxxx> wrote in message 
news:46C071B6.4090104@xxxxxxxxxxx
> Fayezin Islam wrote:
>> 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.
>>>
>>>
>>>
>>
>
> I've only been using the AST for about six months myself, so there's still 
> a lot I'm figuring out.   I think you can do what you want this way (but I 
> haven't tried it):
>
> > Class2 c = new Class2();
> > c.method1(10, "Hello");
>
> In the method invocation (2nd line), the 'c' is the expression.  So you 
> can retrieve that by doing node.getExpression() within the 
> MethodInvocation visitor.
>
> One you have an expression, you should be able to call 
> resolveTypeBinding() on it to get the type (Class2) represented by the 
> expression 'c'.
>
> ITypeBinding itb = node.getExpression().resolveTypeBinding();
> You probably want to then call getQualifedName on 'itb' to find the types 
> you intend to operate on.
>
> In order for resolveBinding to work, you have to specify that on your 
> parser when it is created (parser.setResolveBindings(true) ).
>
> Hope that helps,
> Mike