commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r987336 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/event/EventListenerSupport.java test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
Date Thu, 19 Aug 2010 22:27:05 GMT
Author: mbenson
Date: Thu Aug 19 22:27:05 2010
New Revision: 987336

URL: http://svn.apache.org/viewvc?rev=987336&view=rev
Log:
allow subclass custom invocation handling

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java?rev=987336&r1=987335&r2=987336&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
Thu Aug 19 22:27:05 2010
@@ -219,17 +219,6 @@ public class EventListenerSupport<L> imp
     }
 
     /**
-     * Create the proxy object.
-     * @param listenerInterface
-     * @param classLoader
-     */
-    private void createProxy(Class<L> listenerInterface, ClassLoader classLoader) {
-        proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader, 
-                new Class[]{listenerInterface},
-                new ProxyInvocationHandler()));
-    }
-
-    /**
      * Serialize.
      * @param objectOutputStream
      * @throws IOException
@@ -279,16 +268,35 @@ public class EventListenerSupport<L> imp
      * @param classLoader
      */
     private void initializeTransientFields(Class<L> listenerInterface, ClassLoader
classLoader) {
-        createProxy(listenerInterface, classLoader);
         @SuppressWarnings("unchecked")
         L[] array = (L[]) Array.newInstance(listenerInterface, 0);
         this.prototypeArray = array;
+        createProxy(listenerInterface, classLoader);
+    }
+
+    /**
+     * Create the proxy object.
+     * @param listenerInterface
+     * @param classLoader
+     */
+    private void createProxy(Class<L> listenerInterface, ClassLoader classLoader) {
+        proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader,
+                new Class[] { listenerInterface }, createInvocationHandler()));
+    }
+
+    /**
+     * Create the {@link InvocationHandler} responsible for broadcasting calls
+     * to the managed listeners.  Subclasses can override to provide custom behavior.
+     * @return ProxyInvocationHandler
+     */
+    protected InvocationHandler createInvocationHandler() {
+        return new ProxyInvocationHandler();
     }
 
     /**
      * An invocation handler used to dispatch the event(s) to all the listeners.
      */
-    private class ProxyInvocationHandler implements InvocationHandler
+    protected class ProxyInvocationHandler implements InvocationHandler
     {
         /** Serialization version */
         private static final long serialVersionUID = 1L;

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java?rev=987336&r1=987335&r2=987336&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
Thu Aug 19 22:27:05 2010
@@ -27,6 +27,7 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -180,6 +181,39 @@ public class EventListenerSupportTest ex
         assertEquals(0, deserializedListenerSupport.getListeners().length);
     }
 
+    public void testSubclassInvocationHandling() {
+
+        @SuppressWarnings("serial")
+        EventListenerSupport<ActionListener> eventListenerSupport = new EventListenerSupport<ActionListener>(
+                ActionListener.class) {
+            protected java.lang.reflect.InvocationHandler createInvocationHandler() {
+                return new ProxyInvocationHandler() {
+                    /**
+                     * {@inheritDoc}
+                     */
+                    @Override
+                    public Object invoke(Object proxy, Method method, Object[] args)
+                            throws Throwable {
+                        return "actionPerformed".equals(method.getName())
+                                && "ignore".equals(((ActionEvent) args[0]).getActionCommand())
? null
+                                : super.invoke(proxy, method, args);
+                    }
+                };
+            };
+        };
+
+        ActionListener listener = EasyMock.createNiceMock(ActionListener.class);
+        eventListenerSupport.addListener(listener);
+        Object source = new Object();
+        ActionEvent ignore = new ActionEvent(source, 0, "ignore");
+        ActionEvent respond = new ActionEvent(source, 1, "respond");
+        listener.actionPerformed(respond);
+        EasyMock.replay(listener);
+        eventListenerSupport.fire().actionPerformed(ignore);
+        eventListenerSupport.fire().actionPerformed(respond);
+        EasyMock.verify(listener);
+    }
+
     private void addDeregisterListener(final EventListenerSupport<ActionListener> listenerSupport)
     {
         listenerSupport.addListener(new ActionListener()



Mime
View raw message