diff --git a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java index 69da3d6bc..31fccf6c3 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java +++ b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.event; +import java.lang.reflect.Array; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; @@ -68,6 +69,11 @@ public class EventListenerSupport */ private final L proxy; + /** + * Empty typed array for #getListeners(). + */ + private final L[] prototypeArray; + /** * Creates an EventListenerSupport object which supports the specified * listener type. @@ -128,6 +134,9 @@ public class EventListenerSupport proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader, new Class[]{listenerInterface}, new ProxyInvocationHandler())); + @SuppressWarnings("unchecked") + final L[] prototypeArray = (L[]) Array.newInstance(listenerInterface, 0); + this.prototypeArray = prototypeArray; } /** @@ -185,6 +194,16 @@ public class EventListenerSupport listeners.remove(listener); } + /** + * Get an array containing the currently registered listeners. + * Modification to this array's elements will have no effect on the + * {@link EventListenerSupport} instance. + * @return L[] + */ + public L[] getListeners() { + return listeners.toArray(prototypeArray); + } + /** * An invocation handler used to dispatch the event(s) to all the listeners. */ diff --git a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java index 735a6d70f..cb2624563 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java @@ -24,6 +24,8 @@ import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; +import org.easymock.EasyMock; + /** * @since 3.0 * @version $Id$ @@ -111,6 +113,28 @@ public class EventListenerSupportTest extends TestCase assertEquals(listenerSupport.getListenerCount(), 0); } + public void testGetListeners() { + final EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class); + + ActionListener[] listeners = listenerSupport.getListeners(); + assertEquals(0, listeners.length); + ActionListener[] empty = listeners; + //for fun, show that the same empty instance is used + assertSame(empty, listenerSupport.getListeners()); + + ActionListener listener1 = EasyMock.createNiceMock(ActionListener.class); + listenerSupport.addListener(listener1); + assertEquals(1, listenerSupport.getListeners().length); + assertEquals(ActionListener.class, listenerSupport.getListeners().getClass().getComponentType()); + ActionListener listener2 = EasyMock.createNiceMock(ActionListener.class); + listenerSupport.addListener(listener2); + assertEquals(2, listenerSupport.getListeners().length); + listenerSupport.removeListener(listener1); + assertEquals(1, listenerSupport.getListeners().length); + listenerSupport.removeListener(listener2); + assertSame(empty, listenerSupport.getListeners()); + } + private void addDeregisterListener(final EventListenerSupport listenerSupport) { listenerSupport.addListener(new ActionListener()