How to implement a simple variable renamer?

213 views
Skip to first unread message

melajara

unread,
Nov 1, 2011, 7:02:24 AM11/1/11
to JavaParser
Hello,

I'm using JavaParser to implement a simple renamer for local variables
from a method. Using the Visitor pattern, I thought that overriding
the public void visit( VariableDeclarationExpr n, Object arg) would be
a good place to start. Here is a crude implementation, ignoring
scoping as it is not a concern for my purpose.

@Override
public void visit(VariableDeclarationExpr n, Object arg) {
// restrict change to first declared variable
List<VariableDeclarator> vars = n.getVars();
VariableDeclarator v1 = vars.get(0);
VariableDeclaratorId vId = v1.getId();
String name = vId.getName();
//Compute new name e.g. boolean var -> boolean bool
String newVar = tm.getVar( n.getType().toString()); //tm
is my name producer
//assign new name
vId.setName( newVar);
System.out.println( "Visited variable declaration
expression " + n.toString());
}

The VariableDeclarationExpr is indeed modified but changes are not
propagated to other nodes where the variable is used in the method.
The implication seems to be that a VariableDeclaratorId is not reused
as part of the reference stored in the AST for the variable name
whenever the variable is used in the method, but only used once, i.e.
as a distinct node storing the locus of declaration.

So, my question is how to have the change to the VariableDeclaratorId
suitably propagated?

Julio Gesser

unread,
Nov 1, 2011, 8:53:01 AM11/1/11
to javap...@googlegroups.com
Hello,

you have to add some semantics to your implementation.
The node used to represent a variable declaration is not the same for using variables. So, you will need to create a table like structure to map the variable names and its new names. in the visitor o must also visit the the nodes where the variable are used to rename them too.

--
Júlio Vilmar Gesser



--
You received this message because you are subscribed to the Google Groups "JavaParser" group.
To post to this group, send email to javap...@googlegroups.com.
To unsubscribe from this group, send email to javaparser+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javaparser?hl=en.


Ahti Legonkov

unread,
Nov 1, 2011, 8:55:18 AM11/1/11
to javap...@googlegroups.com
On 1 November 2011 13:02, melajara <mela...@gmail.com> wrote:
> Hello,
>
> I'm using JavaParser to implement a simple renamer for local variables
> from a method. Using the Visitor pattern, I thought that overriding
> the public void visit( VariableDeclarationExpr n, Object arg) would be
> a good place to start. Here is a crude implementation, ignoring
> scoping as it is not a concern for my purpose.

[snip]

> The VariableDeclarationExpr is indeed modified but changes are not
> propagated to other nodes where the variable is used in the method.
> The implication seems to be that a VariableDeclaratorId  is not reused
> as part of the reference stored in the AST for the variable name
> whenever the variable is used in the method, but only used once, i.e.
> as a distinct node storing the locus of declaration.
>
> So, my question is how to have the change to the VariableDeclaratorId
> suitably propagated?

You have to visit all the sub-nodes of the VariableDeclarationExpr,
and also its sibling-nodes, and perform renaming for each node. Better
place to start would probably be "visit(BlockStmt n, Object arg)". The
arg parameter could be used to pass the info about "from" and "to".
And in the BlockStmt you probably don't want to change the name before
the variable declaration, so you have to keep track of that too. This
means that you have to also override "visit(NameExpr n, Object arg)"
which is where you rename the name in n based on the info given in
arg. You also need to implement visit for things like MethodCallExpr
to avoid replacing name of method being called and variable used in
parameter list, also probably for QualifiedNameExpr as well. Some
others too, probably. Can't tell from top of my head at the moment.

--
lego

Mel Cooper

unread,
Nov 1, 2011, 9:50:25 AM11/1/11
to javap...@googlegroups.com
Indeed, I started to figure this out. I hoped for some free lunch here (with some structure sharing ensuring automatic propagation) but no luck ;-)

To not reinvent the wheel, are you aware of some open source tool leveraging on JavaParser to implement a Symbol table populated by the parser or when walking the AST?

I just found this https://github.com/matozoid/javaparser but it's just starting.

Anyway, thank you very much for JavaParser and your support :)

Very best regards
Mel

mathew mooty

unread,
Nov 1, 2011, 12:13:20 PM11/1/11
to javap...@googlegroups.com
If you need help with the algorithm, I would suggest looking at the Eclipse source code, which allows for variable renaming and considers scope.The Eclipse source code uses it's own AST, but you should probably be able to port that code over for your own uses. Unfortunately, I don't know exactly which class within Eclipse performs this task, so it may take you awhile to find (and may mean you don't want to look at the Eclipse code).

Thanks,
Mathew Mooty



Ahti Legonkov

unread,
Nov 1, 2011, 11:18:52 AM11/1/11
to javap...@googlegroups.com
On 1 November 2011 15:50, Mel Cooper <mela...@gmail.com> wrote:
> Indeed, I started to figure this out. I hoped for some free lunch here (with
> some structure sharing ensuring automatic propagation) but no luck ;-)
>
> To not reinvent the wheel, are you aware of some open source tool leveraging
> on JavaParser to implement a Symbol table populated by the parser or when
> walking the AST?
>

See http://code.google.com/p/java-pm/

--
lego

Reply all
Reply to author
Forward
0 new messages