IntelliJ IDEA 2024.3 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.

  1. Place the caret at the constructor invocation in the editor and press Alt+Enter.

  2. From the list of available context actions, select Replace constructor with builder.

    Replace constructor with builder
  3. In the dialog that opens, if you need, change the suggested setter names. 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 the setter method for such a parameter by selecting the Optional Setter checkbox. You can also specify whether you want to create a new builder or use the existing one.

  4. Preview and apply the changes.

Example

Before

After

// UIConstraint.java public class UIConstraint { private int gridX; private int gridY; private int gridWidth; private int gridHeight; private double weightX; private double weightY; UIConstraint(int gridX, int gridY, int gridWidth, int gridHeight, double weightX, double weightY) { this.gridX = gridX; this.gridY = gridY; this.gridWidth = gridWidth; this.gridHeight = gridHeight; this.weightX = weightX; this.weightY = weightY; } }
// UIConstraint.java (unchanged) public class UIConstraint { private int gridX; private int gridY; private int gridWidth; private int gridHeight; private double weightX; private double weightY; UIConstraint(int gridX, int gridY, int gridWidth, int gridHeight, double weightX, double weightY) { this.gridX = gridX; this.gridY = gridY; this.gridWidth = gridWidth; this.gridHeight = gridHeight; this.weightX = weightX; this.weightY = weightY; } } // UIConstraintBuilder.java public class UIConstraintBuilder { private int gridX = 0; private int gridY = 0; private int gridWidth = 1; private int gridHeight = 1; private double weightX = 0.0; private double weightY = 0.0; public UIConstraintBuilder setGridX(int gridX) { this.gridX = gridX; return this; } public UIConstraintBuilder setGridY(int gridY) { this.gridY = gridY; return this; } public UIConstraintBuilder setGridWidth(int gridWidth) { this.gridWidth = gridWidth; return this; } public UIConstraintBuilder setGridHeight(int gridHeight) { this.gridHeight = gridHeight; return this; } public UIConstraintBuilder setWeightX(double weightX) { this.weightX = weightX; return this; } public UIConstraintBuilder setWeightY(double weightY) { this.weightY = weightY; return this; } public UIConstraint createUIConstraint() { return new UIConstraint(gridX, gridY, gridWidth, gridHeight, weightX, weightY); } } // Test.java public class Test { public void main(String[] args) { // Default values are used for omitted fields final UIConstraint uiConstraint = new UIConstraintBuilder() .setGridWidth(2) .setWeightY(1.0) .createUIConstraint(); } }

Replace constructor with builder dialog

Use this dialog to specify options for the Replace constructor with builder refactoring.

Item

Description

Parameters to pass to the builder

Parameter

This column shows the list of parameters detected in the constructor, which will be replaced with the builder fields.

Field name

This editable column shows the list of suggested field names in the builder.

Setter name

This editable column shows the list of suggested setter names in the builder.

Default value

Use this editable column to initialize the fields with the default values.

Optional setter

If the specified default value of a field matches the parameter value in the constructor invocation, then selecting this checkbox results in omitting the setter method for this field in the builder invocation. If this checkbox is not selected, the corresponding setter method will be shown anyway.

Builder name and location

Create new

Click this option to generate a new builder class, with the specified name and destination package.

Builder class name

This editable field shows the suggested name of the new builder class to be generated. You can accept the default suggestion or type a new one.

Package for new builder

Type the name of the destination package, or click the browse button and locate it in the Choose Destination Package dialog. If the necessary package doesn't exist, click to create a new one.

Use existing

Click this option to specify an existing builder class.

Builder class name

Type here the fully qualified name of the builder class that already exists in your project, or click the browse button and find it either by name or in the project tree view.

Last modified: 11 October 2024