Extract parameter
Refactor | Extract | Parameter
CtrlAlt0P
Refactor | Extract | Functional Parameter
CtrlAltShift0P
The Extract Parameter refactoring lets you extract a new parameter to a method.
You can also use the Extract Functional Parameter refactoring. In this case, IntelliJ IDEA analyzes the selected code, finds out what the method signature would be for the extracted fragment, finds all functional interfaces with this method signature and wraps the code fragment with an anonymous class based on the selected interface and uses this anonymous class as a parameter.
In the editor, place the caret within the expression that you want to introduce as a parameter and press CtrlAlt0P or go to Refactor | Extract | Parameter in the main menu.
Alternatively, select a code fragment. On the toolbar that appears, click Extract and select Parameter.
If you want to extract a functional parameter, press CtrlAltShift0P or go to Refactor | Extract | Functional Parameter in the main menu.
Specify a name of the parameter, select the necessary options in the Extract Parameter popup. When you invoke this refactoring, IntelliJ IDEA also displays that you can use to configure more options.
For example, if you don't want to change the existing method calls, select the Delegate via overloading method checkbox.
If you need, press ShiftTab to change a type of the new parameter. (If, at this step, you want to return to editing the parameter name, press Tab.)
When extracting a new parameter you can either introduce a parameter into the existing method or use the Delegate via overloading method option to save the original method and define a new method with your parameter.
Let's replace the string value "Hello, World!"
in the method generateText ()
with the new parameter text
. The value "Hello, World!"
is passed to the method in the updated method call generateText("Hello, World!")
.
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
Now let's use Delegate via overloading method. In this case, a new overloading method is created and the new parameter is extracted in the definition of this method (the second of the generateText()
methods). The signature of the existing generateText()
method is not changed. However, the method itself has been modified. Now, it calls the new generateText()
method and passes the value "Hello, World!"
to it in this call. Note that the existing call of generateText()
(in the method print()
) is not changed.
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
Let's perform the refactoring on System.out.println(s);
within Hello.printHello()
.
IntelliJ IDEA finds all functional interfaces with the appropriate method signature (String) -> void
and suggests that you select one of them. (In this example, the interface Person
is selected.)
As a result, the selected fragment System.out.println(s);
is wrapped with an anonymous class based on the interface Person
. This anonymous class is passed as a parameter to the call to printHello()
within printText()
.
Person
is added as a parameter to printHello()
and the initially selected code fragment is replaced with the call to the method of the interface sayHello()
.
tip
Only the interfaces marked with
@FunctionalInterface
or ones belonging to well-known libraries such as Guava, Apache Collections, etc. are suggested.
Before | After |
---|---|
|
|
If you want a field to be provided as a new parameter in the method declaration, in a method call this field will be presented as a field of a class instance.
If a class member is inaccessible (for instance, in the example above the field is private), it will be inserted in a method call but will be highlighted as an error making this file uncompilable.
If you use for the Extract Parameter refactoring a field with a getter, you will be prompted with an extended dialog. The dialog has an option group Replace fields used in expressions with their getters:
Do not replace: None of the fields will be replaced with calls to the getter.
Replace fields inaccessible in usage context: Only the fields that cannot be directly accessed from the usage context will be replaced with calls to the getter.
Replace all fields: All fields will be replaced with calls to the getter.
Applying the refactoring on a local variable will call the Extract Parameter dialog with additional checkboxes:
Replace all occurrences (<number_of_occurrences> occurrences): If enabled, all occurrences of the selected variable will be replaced with a parameter and the Delete variable definition checkbox is enabled. Otherwise, only the selected variable usage will be replaced with a parameter.
Delete variable definition: If enabled, the variable definition will be deleted.
Use variable initializer to initialize parameter: If enabled, the variable initializer will be used to initialize the parameter in the method call.
Using the Extract Parameter refactoring can have unexpected side effects if applied on class instances or expressions which are actual method parameters. For instance, in case of such code:
class AClass {
int field;
int method() {
return field;
}
}
class Usage {
void method(List list) {
int sum = 0;
for(Iterator it = list.iterator(); it.hasNext(); ) {
sum += ((AClass) it.next()).method();
}
}
}
The code after refactoring applied to the field field
:
class AClass {
int field;
int method(int newfield) {
return newfield;
}
}
class Usage {
void method(List list) {
int sum = 0;
for(Iterator it = list.iterator; it.hasNext(); ) {
sum += ((AClass) it.next()).method(((AClass) it.next()).field);
}
}
}
The iterator value is increased twice which is, actually, not the behavior you would expect.
However, IntelliJ IDEA can use a temporary variable successfully and resolve such cases as increment/decrement/assignment operations and the new keyword usage. For instance:
public int myMethod(List list) {
return list.size();
}
public void anotherMethod() {
myMethod(new ArrayList());
}
Same code after refactoring looks as follows:
public int myMethod(List list, int newPar) {
return list.size();
}
public void anotherMethod() {
final ArrayList list = new ArrayList();
myMethod(list, list.size());
}
The new variable list
was created and all parameters used for the method call are provided using this variable.
Use this dialog to specify the settings for extracting a parameter.
Item | Description |
---|---|
Parameter of type | Specify the type of the new parameter. Usually, you don't need to change the type suggested by IntelliJ IDEA. |
Name | Specify the name for the new parameter. |
Replace all occurrences | Select this option to replace all the occurrences of the selected expression within the method. |
Declare final | Select this option to declare the parameter |
Delegate via overloading method | Select this option to keep the existing method calls unchanged. As a result, a new overloading method will be created and the new parameter will be added to the definition of this method. The method with the old signature will be kept. The call to the new method will be added to the old method. The necessary value (or expression) will be passed to the new method in this call. |
The following options are available if the expression contains a direct call to a class field that has a getter. | |
Do not replace | Select this option to use a direct call to the field regardless of the scope context. |
Replace fields inaccessible in usage context | Select this option to use a call to the getter only where a field is directly inaccessible. |
Replace all fields | Select this option to use a call to the getter. |
The following options are available when the refactored expression contains local variables. | |
Delete variable definition | Select this option to remove the definition of a local variable. |
Use variable initializer to initialize parameter | Select this option to use the default value of the selected variable in the corrected method calls.
|
Use this dialog to specify the options and settings related to the Extract Parameter refactoring in Groovy.
Item | Description |
---|---|
Type | Specify the type of the new parameter. Usually, you don't need to change the type suggested by IntelliJ IDEA. |
Name | Specify the name for the new parameter. |
Declare final | Select this option to declare the parameter |
Delegate via overloading method | Select this option to keep the existing method calls unchanged. As a result, a new overloading method will be created and the new parameter will be added to the definition of this method. The method with the old signature will be kept. The call to the new method will be added to the old method. The necessary value (or expression) will be passed to the new method in this call. |
Remove parameter <name> no longer used | Select this checkbox to remove a parameter. |
Use explicit return statement | This checkbox is active if the method returns a value. You can omit |
Parameters | In this area, select parameters to be passed to the new method/function.
|
Move Up/Down | Use these buttons to change the order of the parameters. |
Signature preview | In this read-only field, view the declaration of the new method/function. |
Thanks for your feedback!