Extract Parameter
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 a anonymous class based on the selected interface and uses this anonymous class as a parameter.
In the editor, position the caret within the expression that you want to introduce as a parameter.
Press Ctrl+Alt+P or from the main menu, select Refactor | Extract | Parameter. If you want to extract a functional parameter, press Ctrl+Alt+Shift+P or from the main menu, select Refactor | Extract | Functional Parameter.
Specify a name of the parameter, select the necessary options in the Extract Parameter popup. For example, if you don't want to change the existing method calls, select the Delegate via overloading method checkbox.
If you need, press Shift+Tab to change a type of the new parameter. (If, at this step, you want to return to editing the parameter name, press Tab .)
Extract parameter examples
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 |
---|---|
|
|
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 |
---|---|
|
|
Functional parameter examples
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 |
---|---|
|
|