Listeners and Adapters

Posted on by basleijdekkers

Occasionally implementing a Listener leads to an explosion of ugly empty boiler-plate methods. In such cases it is usually better to extend a matching adapter class and only override the needed methods.

The next IntelliJ IDEA Maia EAP build will make switching between implementing a Listener or extending an Adapter easier — with newly added Listener implementation could be replaced with Adapter extension inspection (also announced on Twitter).

This new inspection quickly highlights any implementations of Listeners which contain empty methods and offers to replace them with an extension of the relevant Adapter class.

A quick Alt+Enter, Enter replaces the verbose listener code by a more compact use of an Adapter. If you ever need to return to an implementation of a Listener, there is a new complementary intention to make this journey back quick and painless — Replace Adapter Extension with Listener implementation.

Comments below can no longer be edited.

5 Responses to Listeners and Adapters

  1. Vaclav Slovacek says:

    September 3, 2009

    Maybe the new closure folding could be used here as well to hide unused methods?

  2. Michael says:

    September 3, 2009

    This is nice. I have needed this on many occasions! There have been times I have implemented the listener then decided I just need the adapter because I only really need 1 or 2 methods implemented. Tedious. Awesome new addition!

  3. Gleb Mazovetskiy says:

    September 4, 2009

    If I have an XListener and an XAdapter will this inspection work?
    Basically, is this a naming convention based feature?

  4. Esko Luontola says:

    September 5, 2009

    Will this work with any interfaces, such as those in the current project and its libraries, or only those that are part of the JDK? In theory it should be possible to detect adapter classes – they have an empty implementation of all (or most) methods of an interface.

  5. Bas Leijdekkers says:

    September 7, 2009

    This inspection and intention will work with any interface whose name ends with Listener which has an abstract subclass that implements this interface whose name ends with Adapter. Thus this will work for your Adapters and Listeners too.

Subscribe

Subscribe for updates