This page exists so that we can discuss the on-going @ListenerList implementation.

Basic Usage

This example shows the most basic usage of the @ListenerList annotation. The easiest way to use this annotation is to annotate a field of type List and give the List a generic type. In this example we use a List of type MyListener. MyListener is a one method interface that takes a MyEvent as a parameter. The following code is some sample source code showing the simplest scenario.

interface MyListener {
    void eventOccurred(MyEvent event)
}
class MyEvent {    def source
    String message

    MyEvent(def source, String message) {
        this.source = source
        this.message = message
    }
}
class MyBeanClass {
    @ListenerList
    List<MyListener> listeners
}

Gets turned into:

ListenerLists for classes and wide interfaces

The ListenerList generates a fireX method for every public method on the target. For instance, this class: 

interface TestTwoMethodListener {
  void eventOccurred1(TestEvent event)
  void eventOccurred2(TestEvent event)
}


class TestClass {
     @ListenerList
     List<TestTwoMethodListener> listeners
}

Has these two methods generated:

    public void fireEventOccurred1(TestEvent event) {
        if ( listeners != null) {
            java.util.ArrayList<E extends java.lang.Object> __list = new java.util.ArrayList<E extends java.lang.Object>(listeners)
            for (java.lang.Object listener : __list ) {
                listener.eventOccurred1(event)
            }
        }
    }

    public void fireEventOccurred2(TestEvent event) {
        if ( listeners != null) {
            java.util.ArrayList<E extends java.lang.Object> __list = new java.util.ArrayList<E extends java.lang.Object>(listeners)
            for (java.lang.Object listener : __list ) {
                listener.eventOccurred2(event)
            }
        }
    }

Rejected Alternatives