Replace Constructor with Builder
The Replace Constructor with Builder refactoring helps hide a constructor, replacing its usages with the references to a newly generated builder class, or to an existing builder class.
To replace a constructor with a builder class
- Place the caret at the constructor invocation to be replaced.
- Open Replace Constructor with Builder dialog box by choosing on the main menu, or on the context menu of the selection.
The dialog box shows the list of constructor parameters to be passed to the builder, and the suggested name of a new builder.
- In the Parameters to pass to the builder list, optionally change the suggested setter names, and specify whether you want to initialize the generated fields in the builder.
If you specify an initial value that matches the parameter value in the constructor invocation, you can skip setter method for such parameter by selecting the Optional Setter check box.
- Specify which builder are you going to use. If you select the Create new radio button, IntelliJ IDEA will generate a new builder, with the specified name, located in the specified package.
If you select the Use existing radio button, find the desired builder class in your project.
- To review the intended changes and make final corrections prior to the refactoring, click Preview. To apply the changes immediately, click Refactor.
Last modified: 29 November 2017