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
. If you want to extract a functional parameter, press Ctrl+Alt+Shift+P or from the main menu, select .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 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 |
---|---|
public class HelloWorldPrinter {
public static void print() {
System.out.println(generateText());
}
private static String generateText() {
return "Hello, World!".toUpperCase();
}
}
|
public class HelloWorldPrinter {
public static void print() {
System.out.println(generateText("Hello, World!"));
}
private static String generateText(String text) {
return text.toUpperCase();
}
}
|
Before | After |
---|---|
object HelloWorldPrinter {
fun print() {
println(generateText())
}
private fun generateText(): String {
return "Hello, World!".toUpperCase()
}
}
|
object HelloWorldPrinter {
fun print() {
println(generateText("Hello, World!"))
}
private fun generateText(text: String): String {
return text.toUpperCase()
}
}
|
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 |
---|---|
public class HelloWorldPrinter {
public static void print() {
System.out.println(generateText());
}
private static String generateText() {
return "Hello, World!".toUpperCase();
}
}
|
public class HelloWorldPrinter {
public static void print() {
System.out.println(generateText());
}
private static String generateText() {
return generateText("Hello, World!");
}
private static String generateText(String text) {
return text.toUpperCase();
}
}
|
Before | After |
---|---|
object HelloWorldPrinter {
fun print() {
println(generateText())
}
private fun generateText(): String {
return "Hello, World!".toUpperCase()
}
}
|
// if you selected the "Introduce default value" option
object HelloWorldPrinter {
fun print() {
println(generateText())
}
private fun generateText(text: String = "Hello, World!"): String {
return text.toUpperCase()
}
}
|
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()
.
Before | After |
---|---|
@FunctionalInterface
public interface Person {
public void sayHello (String s);
}
public class Hello {
private void printHello () {
String s="Hello";
System.out.println(s);
}
private void printText () {
printHello();
}
}
|
@FunctionalInterface
public interface Person {
public void sayHello (String s);
}
public class Hello {
private void printHello(Person person) {
String s = "Hello";
person.sayHello(s);
}
private void printText () {
printHello(new Person() {
public void sayHello(String s) {
System.out.println(s);
}
});
}
}
|
Special notes for the Extract Parameter refactoring
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 box 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.
Side effects
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:
The code after refactoring applied to the field 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:
Same code after refactoring looks as follows:
The new variable list
was created and all parameters used for the method call are provided using this variable.
Extract parameter dialog (Java)
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. |
Extract parameter dialog (Groovy)
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. |