This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 33a58e56b Fix Object method handling in EventListenerSupport proxies
33a58e56b is described below

commit 33a58e56b81a1b78fcf735b2b779cb943970bab5
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 18 10:36:36 2026 -0500

    Fix Object method handling in EventListenerSupport proxies
    
    Handle equals, hashCode, and toString locally using proxy identity,
    preventing null-unboxing failures and unintended listener invocations.
    
    Share the implementation with EventUtils through a package-private
    helper.
    Update Javadoc and add regression tests for identity semantics,
    collection
    operations, and forwarding proxy registration and removal.
    
    Full Maven validation passes.
    
    Assisted-By: OpenAI Codex
---
 src/changes/changes.xml                            |  1 +
 .../commons/lang3/event/EventListenerSupport.java  | 38 +++++++-----
 .../org/apache/commons/lang3/event/EventUtils.java | 38 ++++++++----
 .../lang3/event/EventListenerSupportTest.java      | 67 ++++++++++++++++++++++
 4 files changed, 119 insertions(+), 25 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 532387408..ff31ba3e6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -102,6 +102,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Gary 
Gregory">Correct Strings Javadoc for case sensitivity, null ordering, prefix 
and suffix matching, and search bounds.</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Preserve interrupt status in BackgroundInitializer.isInitialized 
(#1879).</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Restrict MethodUtils.invokeStaticMethod to static overloads; both 
static invocation variants now throw NoSuchMethodException instead of 
NullPointerException when only an instance method matches (#1881).</action>
+    <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Handle Object methods locally in EventListenerSupport proxies, 
preventing NullPointerException from equals and hashCode and returning an 
identity string from toString.</action>
     <!-- ADD -->
     <!-- UPDATE -->
   </release>
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 be3ffbb1a..62052ccff 100644
--- a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
+++ b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
@@ -105,22 +105,31 @@ protected void handle(final Throwable t) throws 
IllegalAccessException, IllegalA
         }
 
         /**
-         * Propagates the method call to all registered listeners in place of 
the proxy listener object.
+         * Handles a method call on the proxy listener object.
+         *
+         * <p>
+         * Propagates listener method calls to all registered listeners in the 
order added to the underlying {@link List}.
+         * </p>
+         *
          * <p>
-         * Calls listeners in the order added to the underlying {@link List}.
+         * Calls to {@link Object#equals(Object)}, {@link Object#hashCode()}, 
and {@link Object#toString()} use the proxy's identity and are not propagated to
+         * listeners.
          * </p>
          *
-         * @param unusedProxy The proxy object representing a listener on 
which the invocation was called; not used
-         * @param method The listener method that will be called on all of the 
listeners.
-         * @param args event arguments to propagate to the listeners.
-         * @return The result of the method call
+         * @param proxy The proxy object representing a listener on which the 
invocation was called.
+         * @param method The method invoked on the proxy.
+         * @param args The method arguments, or {@code null} if the method has 
no arguments.
+         * @return The identity-based result for an Object method, or {@code 
null} for a listener method.
          * @throws InvocationTargetException Thrown if an error occurs.
          * @throws IllegalArgumentException Thrown if an error occurs.
          * @throws IllegalAccessException Thrown if an error occurs.
          */
         @Override
-        public Object invoke(final Object unusedProxy, final Method method, 
final Object[] args)
+        public Object invoke(final Object proxy, final Method method, final 
Object[] args)
                 throws IllegalAccessException, IllegalArgumentException, 
InvocationTargetException {
+            if (method.getDeclaringClass() == Object.class) {
+                return EventUtils.invokeObjectMethod(proxy, method, args);
+            }
             for (final L listener : listeners) {
                 try {
                     method.invoke(listener, args);
@@ -161,7 +170,7 @@ public static <T> EventListenerSupport<T> create(final 
Class<T> listenerInterfac
     private List<L> listeners = new CopyOnWriteArrayList<>();
 
     /**
-     * The proxy representing the collection of listeners. Calls to this proxy 
object will be sent to all registered listeners.
+     * The proxy representing the collection of listeners. Listener method 
calls to this proxy object are sent to all registered listeners.
      */
     private transient L proxy;
 
@@ -268,12 +277,15 @@ private void createProxy(final Class<L> 
listenerInterface, final ClassLoader cla
     }
 
     /**
-     * Returns a proxy object which can be used to call listener methods on all
-     * of the registered event listeners. All calls made to this proxy will be
-     * forwarded to all registered listeners.
+     * Gets a proxy object which can be used to call listener methods on all 
registered event listeners.
+     *
+     * <p>
+     * By default, listener method calls are forwarded to all registered 
listeners. Calls to {@link Object#equals(Object)}, {@link Object#hashCode()}, 
and
+     * {@link Object#toString()} use the proxy's identity and are not 
forwarded to listeners. Subclasses can customize this behavior by overriding
+     * {@link #createInvocationHandler()}.
+     * </p>
      *
-     * @return A proxy object which can be used to call listener methods on all
-     * of the registered event listeners
+     * @return A proxy object which can be used to call listener methods on 
all registered event listeners.
      */
     public L fire() {
         return proxy;
diff --git a/src/main/java/org/apache/commons/lang3/event/EventUtils.java 
b/src/main/java/org/apache/commons/lang3/event/EventUtils.java
index 5f2abbdb1..50807766b 100644
--- a/src/main/java/org/apache/commons/lang3/event/EventUtils.java
+++ b/src/main/java/org/apache/commons/lang3/event/EventUtils.java
@@ -76,18 +76,7 @@ private boolean hasMatchingParametersMethod(final Method 
method) {
         @Override
         public Object invoke(final Object proxy, final Method method, final 
Object[] parameters) throws Throwable {
             if (method.getDeclaringClass() == Object.class) {
-                // Handle Object methods locally instead of dispatching them 
to the bound target,
-                // mirroring java.beans.EventHandler: routine host actions 
(hash-based collections,
-                // logging, equality checks during listener de-registration) 
must not invoke the
-                // target method and must not return null into an unboxing 
context.
-                switch (method.getName()) {
-                case "hashCode":
-                    return Integer.valueOf(System.identityHashCode(proxy));
-                case "equals":
-                    return Boolean.valueOf(proxy == parameters[0]);
-                default: // toString
-                    return ObjectUtils.identityToString(proxy);
-                }
+                return invokeObjectMethod(proxy, method, parameters);
             }
             if (eventTypes.isEmpty() || eventTypes.contains(method.getName())) 
{
                 if (hasMatchingParametersMethod(method)) {
@@ -137,6 +126,31 @@ public static <L> void bindEventsToMethod(final Object 
target, final String meth
         addEventListener(eventSource, listenerType, listener);
     }
 
+    /**
+     * Handles an Object method locally using the proxy's identity.
+     *
+     * <p>
+     * The caller must first check that the method is declared by {@link 
Object}. JDK proxies dispatch only {@link Object#equals(Object)},
+     * {@link Object#hashCode()}, and {@link Object#toString()} with that 
declaring class. These calls must not invoke listeners or return {@code null} 
into a
+     * primitive unboxing context.
+     * </p>
+     *
+     * @param proxy The proxy instance.
+     * @param method The Object method invoked on the proxy.
+     * @param args The method arguments, or {@code null} if the method has no 
arguments.
+     * @return The identity-based result of the Object method.
+     */
+    static Object invokeObjectMethod(final Object proxy, final Method method, 
final Object[] args) {
+        switch (method.getName()) {
+        case "hashCode":
+            return Integer.valueOf(System.identityHashCode(proxy));
+        case "equals":
+            return Boolean.valueOf(proxy == args[0]);
+        default: // toString
+            return ObjectUtils.identityToString(proxy);
+        }
+    }
+
     /**
      * Make private in 4.0.
      *
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 79f6fc00e..731140ed5 100644
--- a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
+++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
@@ -20,9 +20,11 @@
 import static 
org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
 import static 
org.apache.commons.lang3.LangAssertions.assertNullPointerException;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyVetoException;
@@ -38,15 +40,20 @@
 import java.lang.reflect.UndeclaredThrowableException;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Function;
 
 import org.apache.commons.lang3.AbstractLangTest;
+import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.commons.lang3.function.FailableConsumer;
 import org.easymock.EasyMock;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 /**
  * Tests {@link EventListenerSupport}.
@@ -142,6 +149,66 @@ void testGetListeners() {
         assertSame(empty, listenerSupport.getListeners());
     }
 
+    @ParameterizedTest
+    @ValueSource(booleans = { false, true })
+    void testProxyObjectMethods(final boolean addListener) {
+        final EventListenerSupport<VetoableChangeListener> listenerSupport = 
EventListenerSupport.create(VetoableChangeListener.class);
+        if (addListener) {
+            listenerSupport.addListener(new VetoableChangeListener() {
+                @Override
+                public boolean equals(final Object other) {
+                    throw new AssertionError("Must not dispatch equals to a 
listener");
+                }
+
+                @Override
+                public int hashCode() {
+                    throw new AssertionError("Must not dispatch hashCode to a 
listener");
+                }
+
+                @Override
+                public String toString() {
+                    throw new AssertionError("Must not dispatch toString to a 
listener");
+                }
+
+                @Override
+                public void vetoableChange(final PropertyChangeEvent event) {
+                }
+            });
+        }
+        final VetoableChangeListener proxy = listenerSupport.fire();
+        final VetoableChangeListener otherProxy = 
EventListenerSupport.create(VetoableChangeListener.class).fire();
+        assertEquals(System.identityHashCode(proxy), proxy.hashCode());
+        assertTrue(proxy.equals(proxy));
+        assertFalse(proxy.equals(null));
+        assertFalse(proxy.equals(otherProxy));
+        assertFalse(proxy.equals(new Object()));
+        assertEquals(ObjectUtils.identityToString(proxy), proxy.toString());
+        final Set<VetoableChangeListener> set = new HashSet<>();
+        assertTrue(set.add(proxy));
+        assertFalse(set.add(proxy));
+        assertTrue(set.remove(proxy));
+        final List<VetoableChangeListener> list = new ArrayList<>();
+        list.add(proxy);
+        assertTrue(list.remove(proxy));
+        assertTrue(list.isEmpty());
+    }
+
+    @Test
+    void testProxyRegisteredAsListener() throws PropertyVetoException {
+        final EventListenerSupport<VetoableChangeListener> source = 
EventListenerSupport.create(VetoableChangeListener.class);
+        final EventListenerSupport<VetoableChangeListener> target = 
EventListenerSupport.create(VetoableChangeListener.class);
+        final AtomicInteger calls = new AtomicInteger();
+        target.addListener(event -> calls.incrementAndGet());
+        final VetoableChangeListener proxy = target.fire();
+        source.addListener(proxy, false);
+        source.addListener(proxy, false);
+        assertEquals(1, source.getListenerCount());
+        source.fire().vetoableChange(new PropertyChangeEvent(this, "value", 1, 
2));
+        assertEquals(1, calls.get());
+        source.removeListener(proxy);
+        assertEquals(0, source.getListenerCount());
+    }
+
     @Test
     void testRemoveListenerDuringEvent() throws PropertyVetoException {
         final EventListenerSupport<VetoableChangeListener> listenerSupport = 
EventListenerSupport.create(VetoableChangeListener.class);

Reply via email to