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.

  • 1
    Why doesn't a string find and replace work? – Lawrence Aiello Mar 16 '16 at 17:28
  • 1
    String find and replace doesn't work because the word "beauty" can appear in all sorts of places that are not method calls. Suppose it is in the comments (and not in reference to the method) and suppose it is in strings displayed to the end user. I just want to replace the method calls. A regex replace will not work either for the same reason. I cannot anticipate all strings that might appear in comments, code, or strings in order to form a regex that will just match the calls. – Jason Mar 16 '16 at 17:31
  • 1
    I'd create a class with the same name, create the beauty method and then rename/refactor it to makeBeautiful. Your IDE can do this for you. – tkausl Mar 16 '16 at 17:34
  • 2
    I'm not sure about your Java IDE, but in Visual Studio (.Net) they have the ability to find-all-references of a method, and can rename just calls to that method. And as @tkausl said, we usually just create a temp copy of an object if it has already been renamed by an outside party, do the rename from that temp value, then delete it afterwards. – Rachel Mar 16 '16 at 17:38
  • 1
    @jason building a class around another class can be as simple as just inheriting from your external-library class without declaring anything extra. It will work exactly the way as if you would call it directly. It's just, when something in the library changes you don't have to deal with it in a 1000 places in your code but can modify your inherited class instead. – Pieter B Mar 17 '16 at 14:50

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.

  • The important, and relevant to me, part of this answer is "If you cannot get to the source of the class". The suggestion does require some manual work, but I consider it minimal and it's a very nice idea. – Jason Mar 17 '16 at 14:48

For occasional renaming, the best would be to use a common IDE. Many IDE do already syntax analysis and have therefore some build-in refactoring tools. If you have a bigger refactoring issue (a lot of classes and methods get renamed) an automated tool could help.

Here some hints:

  • Eclipse element renaming. The renaming function of refactoring has two different behaviors. If the class you are referring to is source-included in the project, the method in the class is updated as are all its references. If the class is only included as part of a jar library, you can't rename a valid method (error message)... unless the method doesn't exist anymore in the library. I did the test, replacing the jar with an carefully updated jar prepared outside of eclipse. As soon as eclipse noted that the method didin't exist anymore, it allowed me to rename. This feature is powerful enough to also rename the method in derived classes. Unfortunately, I had to do it file by file.
  • Netbeans element renaming (to be verified)
  • walkmod with a refactoring plugin for refactoring using rules at a larger scale. Note that one of the use case described in the documentation of the plugin is exactly the renaming of classes or methods in case of a changed library.
  • The rename refactoring does not apply here. I already tried it with IntelliJ. The problem is, I don't have the source code and I don't want to rename the method. I want to rename my calls to the method... this is a subtle distinction that doesn't seem to be covered by standard refactoring. – Jason Mar 17 '16 at 1:21
  • 1
    @Jason the fact that IntelliJ has limitations doesn't mean that all other tools are constraint as well. The refactoring plugin above has as explicit use case the renaming of library classes and methods exactly as you've described. – Christophe Mar 17 '16 at 6:34
  • will you edit your answer to reflect this? I cannot change my down vote unless the answer is edited. Also, since JetBrains doesn't support this and you have it listed, could you explicitly mention which tools you know support this feature? – Jason Mar 17 '16 at 14:43
  • @Jason it did some additional verifications. Please check the updated answer – Christophe Mar 17 '16 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.