Listeners and Adapters

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.

This entry was posted in New Features and tagged , . Bookmark the permalink.

5 Responses to Listeners and Adapters

  1. Vaclav Slovacek says:

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

  2. Michael says:

    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. If I have an XListener and an XAdapter will this inspection work?
    Basically, is this a naming convention based feature?

  4. Esko Luontola says:

    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:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">