Search code examples
javanetbeansrefactoringrenameautomated-refactoring

Which refactoring tools can rename a Java class along with all its similarly-named instances?


Suppose I have a class called Orange in my NetBeans Java project, which I'd like to rename to Apple. The NetBeans rename refactoring can handle this without difficulty. However, my project contains many instances of the Orange class with the name orange, resulting in a lot of confusing refactored code along the lines of:

final Apple orange = new Apple();

I'd like all these instances -- and preferably also prefixed ones like juicyOrange and defaultOrange -- to be renamed to apple, juicyApple, etc. as part of the same refactoring. The refactoring should of course not affect variables of different classes (e.g. Colour orange should remain as-is), or orange occurrences where there's already an apple variable in context. It seems that IntelliJ IDEA offers this functionality, but I can't find it in NetBeans 8.2.

Is this functionality available or planned either in NetBeans itself or in any NetBeans plug-in? Failing that, is there any stand-alone Java refactoring tool which offers it?


Solution

  • It seems that IntelliJ IDEA offers this functionality, but I can't find it in NetBeans 8.2.

    The refactoring functionality you describe in Intellij IDEA does not exist in NetBeans 8.2, nor in NetBeans 9.0.

    Is this functionality available or planned either in NetBeans itself or in any NetBeans plug-in?

    I don't know of any plans to add this functionality, nor do I know of any NetBeans plugin that does what you want. If you would like this functionality to be added to Apache NetBeans 9.0 consider raising an enhancement request through a bug report.

    That said, the functionality provided by Edit > Replace in Projects... might be worth looking at as a cruder alternative:

    ReplaceInProjects

    • You can provide a regular expression for the text to search for.
    • The files to be considered for modification can be specified in several ways:

      • The current project.
      • Selected projects.
      • All open projects.
      • Open files.
      • Selected files.
    • You can additionally provide a pattern for the files to be modified, allowing for complex selection. For example:

      • Modify all files containing "Widget" in their name with file extension "jsf" in projects A, B and C.
      • Modify only open files with the extension "properties".

    Of course this functionality is not sophisticated, and it will blindly do whatever you tell it. It cannot handle the Colour orange scenario you described (unless you ensure that the relevant files are excluded based on your file selection criteria).

    Failing that, is there any stand-alone Java refactoring tool which offers it?

    Recommending software is specifically off topic here, though that has been asked in some closed questions here. (See What topics can I ask about here?)