The extract method feature can create a method to replace all occurrences of a given code fragment.
To create a new method from an expression:
- In the editor, select the the code fragement which you would like to replace with a method.
- Right-click and select Refactor | Extract Method -or- click Alt+Shift+M.
The Extract Method dialog is launched.
- Enter the name of the new method in the Method name field.
- Select the Access modifier for your method.
- If multiple occurrences of the code fragement appear in your code, mark the 'replace all occurrences of statements with method' checkbox for all occurrences to be replaced with the new method.
- Mark the Generate method comments checkbox for comments to be created for your method.
- Click OK to apply your changes or click Preview if you want to see a preview of the changes that this refactoring will create.
- If you clicked preview a preview window will open with a changes tree showing all the changes which will be made to reflect the extracting of the method.
- The changes will be listed according to the context within which they appear. You can expand the nodes to see all changes within particular files, classes or functions.
- Use the Next / Previous Change arrows to scroll through all possible changes.
Unmarking the checkboxes next to the changes will cause those changes not to take effect. - Click OK to apply the changes.
The method will be extracted and the relevant changes made to the code.