Extract Parameter Object
The Extract Parameter Object refactoring allows you to select a set of parameters to a method, and either create a wrapper class for those parameters, or use an existing compatible wrapper class. All calls to the method selected will have their parameters appropriately wrapped, and all usages of the wrapped parameter will be replaced by the appropriate calls on the newly created parameter class.
Extracting a parameter object is useful if the number of parameters passed to a method has grown too large, or if the parameters have become complex enough to deserve first-class handling as their own class. Also, it is common to wrap primitive parameters as parameter objects, thus allowing interface and implementation to be decoupled as needed.
To extract a parameter object
- Select the desired method. To do that, either open the class in question for editing, and position the caret at the method, click such method in the Structure view, or select it in the UML class diagram.
- Choose Refactor | Extract | Parameter Object on the main menu or on the context menu of the selection.
- In the Extract Parameter Object dialog box:
- In the Parameter Class section, specify whether you want to create a new class, or use an existing one to wrap the parameters.
- In the Parameters to extract list, check the parameters you want include in the new class.
- Click Preview to make IntelliJ IDEA search for usages of the selected fields or methods,
and display the refactoring preview results in the Find tool window.
In the preview, you can include usages into refactoring or skip them. Click Do Refactor to apply
refactoring to the selected usages.
If you do not want to view usages, click Refactor. In this case, the usages will be changed immediately.
The Refactoring preview window may appear anyway, if the files to be affected are read-only.