Extracting Method in Groovy
This section discusses Extract Method refactoring in Groovy.
This refactoring lets you perform the following actions:
- Extract method for a variable.
- Extract method for a list of variables.
- Extract method for one or several statements.
In this section:
| || |
| || |
| || |
To extract a method in Groovy
- In the editor, select a block of code to be transformed into a method or a function.
- On the main menu or on the context menu of the selection, choose or press Ctrl+Alt+M.
- In the Extract Method dialog box that opens, specify the name of the new method.
- To return the value of a data type explicitly, select the Specify return type explicitly checkbox.
- To return a keyword, select the Use explicit return statement checkbox.
- In the Parameters area, do the following:
- Specify the variables to be passed as method parameters, by selecting/clearing the corresponding checkboxes; if a parameter is disabled, a local variable of the corresponding type, with the initial value ... will be created in the extracted method, so that you will have to enter the initializer with an appropriate value manually.
- Rename the desired parameters, by double-clicking the corresponding parameter lines and entering new names.
- Check the result in the Signature Preview pane and click OK to create the method. The selected code fragment will be replaced with a method call. Additionally, IntelliJ IDEA will propose to replace any similar code fragments found within the current class.
Last modified: 4 September 2018