Reputation: 480
I came across a task to internationalise the project which including the code (now 80% of the variable names are in Italian, which need to be changed to English). The translation part is fine, but how to find a way to refactor the code is driving me nuts. Almost every IDE in the market has such refactoring function, but none of them work automatically or programmably.
My question is, do they just use the regex to scan through all the files in the project and then refactor them or follow some sophisticated rules?
Upvotes: 4
Views: 494
Reputation: 95430
OP appears to want to do mass renaming.
While not the tool you might expect, you can use our Java obfuscator to achieve this. This solution does mass renaming ignoring scopes, but I suspect that works fine for this.
Many source obfuscators take identifiers and scramble them in a consistent fashion. This means somewhere in the obfuscator, there is map from legacy identifiers to obfuscated identifiers, e.g., "foo" -> "bar" meaning "replace 'foo' by 'bar'".
Our obfuscator reads such a map before it starts (and that's the key to a solution). This map was presumably generated by a previous obfuscation step; reading and reusing such a map ensures that any incremental/additional obfuscation is done in the same way as as the original.
If what you do is run our obfuscator on a set of source files, you get a complete map of every identifier and what it was mapped to (randomly), in a text file looking like (as above):
foo -> i3718234
fas -> h823478
...
If you then run the obfuscator again and feed it the same souces and this map file, it will (by design) map the identifiers the same way, producing the same result as the first time.
To do OPs task, one could run our obfuscator on his code to get:
italianID1 -> i23432
italianID2 -> g232343
...
Using a text editor, he could modify this file to:
italianID1 -> englishID1
italianID2 -> englishID2
...
Names he doesnt want touched are edited (obviously) to this form:
foo -> foo
bar -> bar
(A shorthand for this, is to simply leave off the "-> id" part. "foo" by itself is interpreted as "foo -> foo").
Now running the obfuscator will rename all the italian IDs to englishIDs.
Check out my bio for a link. Its a commercial tool, but its probably cheaper than a day of OP's labor and I think it solves his problem, waaaaay easier than building his own refactoring tool.
A downside: I think he'll be forced to reformat his code. That's OK, the obfuscator includes a reformatter.
Upvotes: 3
Reputation: 65889
I have used a combination of JavaParser and another tool I forget the name of which allowed me to trace usages of all variables in classes from the compiled .class/.jar file.
The JavaParser is certainly a tool that could do the job. I will take a look tomorrow what the other componnet was. Could have been Class Dependency Analyser.
Upvotes: 3