Add, remove and reorder a method’s arguments instantly across multiple classes in Eclipse

When refactoring, one thing that sometimes changes is a method’s signature (ie. its return type, number of arguments, argument types and order).

For some it may not happen that often, but the job becomes difficult when you have a dozen or more classes that call that method. All of them need to be changed to match the new signature and this can take a long time to change.

Fortunately, Eclipse has a refactoring called Change Method Signature that makes this easy, allowing a signature change in almost no time across any number of classes. This can be a huge timesaver when you need it and save you hours of frustration trying to fix red crosses in Eclipse.

How to change a method signature

The refactoring is easy to invoke. The fastest way to do this is to position the cursor on the method’s declaration or any of it’s calls and then press Alt+Shift+C (or alternatively select Refactor > Change Method Signature from the application menu). This can only be invoked on methods for which you own the code.

In the next video I show an example of adding a new String argument called message to the method process as the second argument. Notice how Eclipse automatically shifts all other arguments up by one. Then we’ll remove the int argument. The two affected classes are below each other so you can see the magic in action.

Notes:

  • To reorder the arguments, just press the Up/Down buttons.
  • You can specify a default value for the new argument. This is the default value that Eclipse will give all method calls. Normally null or another sensible default will do.
  • You can preview the changes before you accept them. Eclipse shows you all affected classes and allows you to compare the current and soon-to-be code.
  • Eclipse has Content Assist inside the refactoring dialog, meaning that you can press Ctrl+Space in the Type column to see a list of object types that match the name you’ve typed.
  • You can change the return type and even the name of the method. If you just want to rename a method, rather use the Rename refactoring (Alt+Shift+R).

Related Tips

Support Eclipse On E

3 thoughts on “Add, remove and reorder a method’s arguments instantly across multiple classes in Eclipse