In Java, you can use the Change Method Signature refactoring to:
- Change the method name, return type and visibility scope.
- Add new parameters and remove the existing ones. Note that you can also add a parameter using a dedicated Extract Parameter refactoring.
- Reorder parameters.
- Change parameter names and types.
- Add and remove exceptions.
- Propagate new parameters and exceptions through the method call hierarchy.
On this page:
The following table shows 4 different ways of performing the same Change Method Signature refactoring.
In all the cases, a new parameter price of the type double is added to the method myMethod().
The examples show how the method call, the calling method (myMethodCall()) and other code fragments may be affected depending on the refactoring settings.
- In the editor, place the cursor within the name of the method whose signature you want to change.
Do one of the following:
- Press Ctrl+F6Ctrl+F6Ctrl+6Ctrl+F6Ctrl+F6Ctrl+F6Ctrl+F6Ctrl+F6Shift+Alt+CCommand F6Command F6Alt Command C.
- Choose Refactor | Change Signature in the main menu.
- Select Refactor | Change Signature from the context menu.
If you refactor a method that overrides another method, IntelliJ IDEA suggests either to modify the method from the base class, or to modify only the selected method.
In the Change Signature dialog, make the necessary changes to the method
signature and specify which other, related changes are required.
- Change the method visibility scope (access level modifier) by selecting the necessary option under Visibility.
- Change the method return type by editing the contents of the Return type field.
Code completion is available in this field, and also in other fields used for specifying the types.
- Change the method name. To do that, edit the text in the Name field.
Manage the method parameters using the controls on the Parameters tab:
To add a new parameter, click (Alt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+Insert or Ctrl+NAlt+InsertCommand N or Control EnterControl N or Control EnterCommand N or Control Enter) and
specify the properties of the new parameter in the corresponding fields.
If necessary, select the Use Any Var option.
When adding parameters, you may want to propagate these parameters to the methods that call the current method.
- To remove a parameter, select this parameter in the table and click (Alt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteAlt+DeleteCommand DeleteCommand DeleteCommand Delete).
- To reorder the parameters, use (Alt+UpAlt+UpAlt+UpAlt+UpAlt+Up or Ctrl+Alt+AAlt+UpAlt+UpCtrl+Alt+UpCtrl+Shift+UpControl UpControl UpControl Alt Up) and (Alt+DownAlt+DownAlt+DownAlt+DownAlt+Down or Ctrl+Alt+EAlt+DownAlt+DownCtrl+Alt+DownCtrl+Shift+DownControl DownControl DownControl Alt Down).
- To change the name, type, or the default value for a parameter, click this parameter in the table and make the necessary edits in the corresponding fields.
- To add a new parameter, click (Alt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+Insert or Ctrl+NAlt+InsertCommand N or Control EnterControl N or Control EnterCommand N or Control Enter) and specify the properties of the new parameter in the corresponding fields. If necessary, select the Use Any Var option.
Propagate new method parameters (if any) along the hierarchy of the methods that call the current
(There may be the methods that call the method whose signature you are changing. These methods, in their turn, may be called by other methods, and so on. You can propagate the changes you are making to the parameters of the current method through the hierarchy of the calling methods and also specify which calling methods should be affected and which shouldn't.)
To propagate the new parameters:
- Click (Alt+G).
In the left-hand pane of the Select Methods to Propagate New Parameters
dialog, expand the necessary nodes and select the check boxes next to the methods you want
the new parameters to be propagated to.
To help you select the necessary methods, the code for the calling method and the method being called is shown in the right-hand part of the dialog (in the Caller Method and Callee Method panes respectively).
As you switch between the methods in the left-hand pane, the code in the right-hand part changes accordingly.
- Click OK.
- Manage the method exceptions using the list of exception types and the buttons on the Exceptions tab. The procedures are similar to those used for managing the method parameters.
- Propagate new exceptions (if any) along the hierarchy of the methods that call the current method. To initiate this procedure, use (Alt+X). In all other respects, the procedure is similar to that used for propagating new method parameters.
- Specify how the method calls should be handled. To do that, select one of the following Method calls options:
To perform the refactoring right away, click Refactor.
To see the expected changes and make the necessary adjustments prior to actually performing the refactoring, click Preview.