IntelliJ IDEA 14.0.0 Web Help

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.

Example

Before After
Replace Constructor With Builder 1
Replace constructor with builder 2



Replace constructor with builder 3

To replace a constructor with a builder class

  1. Place the caret at the constructor invocation to be replaced.
  2. Open Replace Constructor with Builder dialog box by choosing Refactor | Replace Constructor with Builder 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.
  3. 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.
  4. 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.
  5. To review the intended changes and make final corrections prior to the refactoring, click Preview. To apply the changes immediately, click Refactor.

See Also

Last modified: 12 November 2014
comments powered by Disqus