IntelliJ IDEA 2017.3 Help

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.


public class A { private void drawEdge(Graphics g, float edgeWidth, int x1, int x2, int y1, int y2) { final Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(edgeWidth)); g.drawLine(x1, y1, x2, y2); } }
public class A { private void drawEdge(Edge edge, Graphics g) { final Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(edge.getEdgeWidth())); g.drawLine(edge.getX1(), edge.getY1(), edge.getX2(), edge.getY2()); } } public class Edge { private final float edgeWidth; private final int x1; ... public Edge(float edgeWidth, int x1, int x2, int y1, int y2) { this.edgeWidth = edgeWidth; this.x1 = x1; ... } public float getEdgeWidth() { return edgeWidth; } public int getX1() { return x1; } ... }

To extract a parameter object

  1. 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.
  2. Choose Refactor | Extract | Parameter Object on the main menu or on the context menu of the selection.
  3. 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.

Last modified: 6 March 2018

See Also