The Change Signature refactoring combines several different modifications that can be applied to a method signature or a class signature.
For a class, this refactoring can turn a class into a generic and manipulate its type parameters. It automatically corrects all calls, implementations and overridings of the class.
For a method, this refactoring can change the method name, add, remove, reorder, and rename parameters and exceptions, and propagate new parameters and exceptions through the hierarchy of calls.
Select method or a class for which you want to change signature.
On the context menu, select(Ctrl+F6).
In the dialog that opens depending on what you're changing signature for, specify the appropriate options and click Refactor. If you want to see a preview of your potential changes, click Preview.
Change Class Signature example
Let's add type parameters
Param1, Param2 to a class
String and Integer are default values that were specified in the Change Class Signature dialog for
You can also add a bounded value for a parameter to put some restrictions on what gets passed to the type parameter. For example, add
Param3 with default value
List and bounded value
Change Method Signature example
You can add parameters to a method and specify different options in the Change Method Signature dialog to have different results.
You can also add exceptions to a method and they will be propagated in a call hierarchy.
Let's add a parameter with a name
double, and a default value
IntelliJ IDEA adds parameters to the method, updates method calls and specifies the default parameter value.
Use any variable
Let's update all the method calls and look for a variable of the appropriate type near the method call and pass it to the method. To achieve that check the Use Any Var option.
As a result, IntelliJ IDEA finds the variable d which has the same type as the new parameter and uses it in the method call.
Let's ask IntelliJ IDEA to keep the method calls unchanged but create a new overloading method which will call the method with the new signature. To achieve that use the Delegate via overloading method option.
Note that the new overloading method has the old signature. However, it calls the method with the new signature.
0.0 was specified as the default parameter value when performing the refactoring.
Let's propagate Alt+G the new parameter to the method call through the calling method
As a result, the new parameter
price propagates to the method call through the calling method and changes the method call accordingly.
Let's add a method
read that throws an exception
IOException to a class
Then let's create a new class
ExtendsMyClass that will extend
MyClass and override the original
Now if we go back to
MyClass and decide to add another exception, for example,
TimeOutException through this refactoring, the method
read will be updated the sub class as well.