Java Refactoring – Refactoring Code Dependent on Renamed Library Method

javarefactoring

Say third party library Beautify renames one of their methods from beauty to makeBeautiful because they want the method to be a verb and by some lack of initial planning didn't do this from the start.

Now, I use this beauty method a lot… how do I rename all occurrences of it to makeBeautiful without doing a plain string search and replace? Is there a name for this type of refactoring? Do any tools support it?

I'm particularly interested in Java solutions, but if I can at least get a name for the refactoring type that would be great.

Best Answer

Many modern IDEs offer refactoring options, you can highlight the method you want to refactor, run the rename-refactoring and you're pretty much set. If you cannot get to the source of the class, I'd suggest you mimicking it, by creating a temporary dummy of the class whose API you want to alter (same name, same package) and refactor that one. Then deleting this dummy and importing your new version of the library.

Renaming aside, you should probably opt for a global refactoring of your code anyway by creating an adapter, wrapping the 3rd party library and then using your adapter throughout your project. That way you will depend on your own API and if the 3rd party class' API changes in the future, all you need to do is to change the call in one place. One of the great benefits of the adapter pattern.

Related Topic