IntelliJ IDEA 8 Refactorings: Remove Middleman
December 3rd, 2008 by Maria KhalusovaIntelliJ 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.


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!


If you need the delegating method declarations for backward compatibility, or any other reason, you keep them – IntelliJ IDEA gives you this option.
December 16th, 2008 at 7:19 pm
I may have missed something here, or maybe it’s not a good example, but doesn’t this break the encapsulation of bar in class Foo, and prevent a change of implementation of getImpValue() in Foo - the very things forwarding methods are encouraged for?
Why would I want to do that?