Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 57664 invoked from network); 19 Aug 2010 22:28:42 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 19 Aug 2010 22:28:42 -0000 Received: (qmail 22157 invoked by uid 500); 19 Aug 2010 22:28:42 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 22084 invoked by uid 500); 19 Aug 2010 22:28:41 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 22077 invoked by uid 99); 19 Aug 2010 22:28:41 -0000 Received: from Unknown (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Aug 2010 22:28:41 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Aug 2010 22:28:23 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E37D12388A3B; Thu, 19 Aug 2010 22:27:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: mbenson@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100819222705.E37D12388A3B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 imp } /** - * Create the proxy object. - * @param listenerInterface - * @param classLoader - */ - private void createProxy(Class 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 imp * @param classLoader */ private void initializeTransientFields(Class 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 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 eventListenerSupport = new EventListenerSupport( + 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 listenerSupport) { listenerSupport.addListener(new ActionListener()