IntelliJ IDEA 2024.3 Help

Remove middleman

The Remove Middleman refactoring lets you replace all calls to delegating methods in a class with the equivalent calls directly to the field delegated to. Additionally, you can automatically remove the classes delegating methods, which will now be unused.

This refactoring is useful if you have a class that simply forwards many of its method calls to objects of other classes, and you wish to simplify your design.

Install the Additional Java Refactorings plugin

This functionality relies on the Additional Java Refactorings plugin, which you need to install and enable.

  1. Press Ctrl+Alt+S to open settings and then select Plugins.

  2. Open the Marketplace tab, find the Additional Java Refactorings plugin, and click Install (restart the IDE if prompted).

Run the Remove Middleman refactoring

  1. Open the class in question in the editor, and place the caret at the name of the delegating field.

  2. On the main or context menu, select Refactor | Remove Middleman.

  3. In the dialog that opens, select methods that you want to inline.

    Remove middleman refactoring
  4. Preview and apply your changes

Example

Before

After

// File Foo.java public class Foo { Bar bar; public Foo getImpValue() { return bar.getImpValue(); } } // File Bar.java public class Bar { private Foo impValue1; public Bar(Foo impValue) { impValue1 = impValue; } public Foo getImpValue() { return impValue1; } } // File Client.java public class Client { Foo a; Foo impValue = a.getImpValue(); }
// File Foo.java public class Foo { Bar bar; public Bar getbar() { return bar; } } // File Bar.java public class Bar { private Foo impValue1; public Bar(Foo impValue) { impValue1 = impValue; } public Foo getImpValue(){ return impValue1; } } // File Client.java public class Client { Foo a; Foo impValue = a.getbar().getImpValue(); }
Last modified: 22 July 2024