Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java source refactoring of 7000 references

Tags:

I need to change the signature of a method used all over the codebase.

Specifically, the method void log(String) will take two additional arguments (Class c, String methodName), which need to be provided by the caller, depending on the method where it is called. I can't simply pass null or similar.

To give an idea of the scope, Eclipse found 7000 references to that method, so if I change it the whole project will go down. It will take weeks for me to fix it manually.

As far as I can tell Eclipse's refactoring plugin of Eclipse is not up to the task, but I really want to automate it.
So, how can I get the job done?

like image 670
Hayati Guvence Avatar asked Oct 11 '10 12:10

Hayati Guvence


2 Answers

Great, I can copy a previous answer of mine and I just need to edit a tiny little bit:


I think what you need to do is use a source code parser like javaparser to do this.

For every java source file, parse it to a CompilationUnit, create a Visitor, probably using ModifierVisitor as base class, and override (at least) visit(MethodCallExpr, arg). Then write the changed CompilationUnit to a new File and do a diff afterwards.

I would advise against changing the original source file, but creating a shadow file tree may me a good idea (e.g. old file: src/main/java/com/mycompany/MyClass.java, new file src/main/refactored/com/mycompany/MyClass.java, that way you can diff the entire directories).

like image 165
Sean Patrick Floyd Avatar answered Oct 13 '22 18:10

Sean Patrick Floyd


Eclipse is able to do that using Refactor -> Change Method signature and provide default values for the new parameters.

For the class parameter the defaultValue should be this.getClass() but you are right in your comment I don't know how to do for the method name parameter.

like image 31
Manuel Selva Avatar answered Oct 13 '22 20:10

Manuel Selva