Q:

changing javascript variable and function name from java using rhino or closure compiler

I want to be able to change the variables and functions name in a javascript file based on certain conditions and then output the modified javascript. This is similar to what a javascript minifier does.
Here is an example:
I have this javascript code
var something=5;
something++;
function doSomething(n){
   alert(n);
}
doSomething(something):

Now I want to replace the something and doSomething with somethingElse and doSomethingElse and return the following javascript code:
var somethingElse=5;
somethingElse++;
function doSomethingElse(n){
   alert(n);
}
doSomethingElse(somethingElse):

I think this can be done in rhino or google closure compile(which is based on rhino), but don't know how. I'm also open to other suggestions.
Thanks

A:

Rhino is just a parser, so it wouldn't really help except in the simplest of cases.
Closure-compiler technically can do this, but doesn't officially support directed renaming.
The following technique is used for incremental compilation. Using map files, the compiler tries (but does not guarantee) to honor previous renaming for variables and properties. You might make this work using the following steps, but there are no guarantees that it will work in all cases:
Step 1
Create a variable map file using the following syntax (one entry per line)
varname:renamedname

Step 2
Use the variable renaming as part of the compilation command
java -jar compiler.jar --js inputfile.js \
  --variable_map_input_file varrenaming.txt

Note that all normal renaming rules still apply. If a variable wasn't renamed previously, this won't cause it to be renamed now.

