Tips & Tricks

IntelliJ IDEA 8 Refactorings: Remove Middleman

IntelliJ IDEA 8 can help you maintain and improve the source code in a wide variety of ways. For example, to make it clearer, simpler and less knotty, you can use its rich set of refactorings. In this article we’re going to explain the Remove Middleman refactoring, which is helpful when you have classes doing too much simple delegation. Remove Middleman refactoring replaces all calls to delegating methods with the equivalent direct calls.

Class Foo before refactoring
Class Bar
Class Client before refactoring

Here, bar field in the Foo class is nothing more than a simple delegation, so we can safely remove it. Right-click this field and select Refactor | Remove Middleman. IntelliJ IDEA suggests to inline methods which simply forward the calls to this field and remove their declarations. That’s OK for this case, so we click Refactor and enjoy the result!

CLass Foo after refactoring
Class Bar
Class Client after refactoring

If you need the delegating method declarations for backward compatibility, or any other reason, you keep them – IntelliJ IDEA gives you this option.

image description