Home » Language IDEs » Java Development Tools (JDT) » doing recursive AST rewrites
doing recursive AST rewrites [message #182558] |
Thu, 14 October 2004 14:09  |
Eclipse User |
|
|
|
Originally posted by: emerson.cse.ogi.edu
Howdy,
I've been working with the ASTRewrite in a few contexts, but have always
had this minor problem where I can't do two rewrites that occur, nested,
in the same expression. For example, I have the following expression:
var1 = var2
and I would like to see it changed to:
setVar1(getVar2());
So essentially I traverse the tree, replacing Assignments with
MethodReferences and some SimpleNames with MethodReferences. Since the
Assignment is expressed first, the I first add the change:
var1 = var2 --> setVar1(var2)
then, when I come upon the SimpleName var2, I add the change:
var2 --> getVar2()
So far, so good. But when I actually do the rewrite, I only get:
setVar1(var2)
I imagine the problem here is that once the 1st rule is executed, the
2nd rule is no longer valid or applicable because var2 no longer exists
in the AST (just a semantically identical SimpleName).
To get around this, I can traverse the AST twice; once to make the
"getter" replacements, and then once more to make the "setter"
replacements. Sad, that.
Alternatively, I might be able to reorder the AST changes such that
SimpleNames are replaced before Assignments. Anyone know if this would
work, or has a better idea?
Thanks!
-e
|
|
|
Re: doing recursive AST rewrites [message #182686 is a reply to message #182558] |
Fri, 15 October 2004 10:52   |
Eclipse User |
|
|
|
Are you diectly modifying the AST or are you using the describing
ASTRewrite?
If it's the first, please file a bug against jdt.core.
If it's the describing Rewrite:
When you did
var1 = var2 --> setVar1(var2)
what did you fill in as 'var2'? The correct thing is insert there a
'move target' of 'var2'.
Emerson Murphy-Hill wrote:
> Howdy,
>
> I've been working with the ASTRewrite in a few contexts, but have always
> had this minor problem where I can't do two rewrites that occur, nested,
> in the same expression. For example, I have the following expression:
>
> var1 = var2
>
> and I would like to see it changed to:
>
> setVar1(getVar2());
>
> So essentially I traverse the tree, replacing Assignments with
> MethodReferences and some SimpleNames with MethodReferences. Since the
> Assignment is expressed first, the I first add the change:
>
> var1 = var2 --> setVar1(var2)
>
> then, when I come upon the SimpleName var2, I add the change:
>
> var2 --> getVar2()
>
> So far, so good. But when I actually do the rewrite, I only get:
>
> setVar1(var2)
>
> I imagine the problem here is that once the 1st rule is executed, the
> 2nd rule is no longer valid or applicable because var2 no longer exists
> in the AST (just a semantically identical SimpleName).
>
> To get around this, I can traverse the AST twice; once to make the
> "getter" replacements, and then once more to make the "setter"
> replacements. Sad, that.
>
> Alternatively, I might be able to reorder the AST changes such that
> SimpleNames are replaced before Assignments. Anyone know if this would
> work, or has a better idea?
>
> Thanks!
>
> -e
|
|
|
Re: doing recursive AST rewrites [message #182875 is a reply to message #182686] |
Mon, 18 October 2004 11:43  |
Eclipse User |
|
|
|
Originally posted by: emerson.cse.ogi.edu
> Are you diectly modifying the AST or are you using the describing
> ASTRewrite?
The latter.
> When you did
> var1 = var2 --> setVar1(var2)
> what did you fill in as 'var2'? The correct thing is insert there a
> 'move target' of 'var2'.
Yes, I believe I'm doing that. For the replacement of the assignment:
protected void replaceFieldAssignmentWithSetter(Assignment node) {
SimpleName lhsName = getLeftHandSideName(node);
int flags = getFlags(lhsName.getIdentifier());
MethodInvocation setter = getSetter(lhsName);
setter.arguments().add(
getRewrite().createMoveTarget(node.getRightHandSide()));
getRewrite().replace(node,setter,getTextEditGroup());
}
And for the target:
protected void replaceFieldAccessorWithMethodCall(SimpleName node){
MethodInvocation getter = getGetter(node);
getRewrite().replace(node,getter,getTextEditGroup());
}
Any other suggestions?
Thanks!
e
> Emerson Murphy-Hill wrote:
>
>> Howdy,
>>
>> I've been working with the ASTRewrite in a few contexts, but have
>> always had this minor problem where I can't do two rewrites that
>> occur, nested, in the same expression. For example, I have the
>> following expression:
>>
>> var1 = var2
>>
>> and I would like to see it changed to:
>>
>> setVar1(getVar2());
>>
>> So essentially I traverse the tree, replacing Assignments with
>> MethodReferences and some SimpleNames with MethodReferences. Since
>> the Assignment is expressed first, the I first add the change:
>>
>> var1 = var2 --> setVar1(var2)
>>
>> then, when I come upon the SimpleName var2, I add the change:
>>
>> var2 --> getVar2()
>>
>> So far, so good. But when I actually do the rewrite, I only get:
>>
>> setVar1(var2)
>>
>> I imagine the problem here is that once the 1st rule is executed, the
>> 2nd rule is no longer valid or applicable because var2 no longer
>> exists in the AST (just a semantically identical SimpleName).
>>
>> To get around this, I can traverse the AST twice; once to make the
>> "getter" replacements, and then once more to make the "setter"
>> replacements. Sad, that.
>>
>> Alternatively, I might be able to reorder the AST changes such that
>> SimpleNames are replaced before Assignments. Anyone know if this
>> would work, or has a better idea?
>>
>> Thanks!
>>
>> -e
|
|
|
Goto Forum:
Current Time: Wed May 07 22:14:52 EDT 2025
Powered by FUDForum. Page generated in 0.04239 seconds
|