JDK-4270563 : java.awt.Component.getListeners works wrong with PropertyChangeListener
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5
  • CPU: sparc
  • Submitted: 1999-09-10
  • Updated: 1999-09-13
  • Resolved: 1999-09-13
Related Reports
Duplicate :  
Description

Name: dsC58869			Date: 09/10/99



The java.awt.Component.getListeners(Class listenerType) method always 
returns null if listenerType is an java.beans.PropertyChangeListener.
Java Doc says:
    /**
     * Return an array of all the listeners that were added to the Component
     * with addXXXListener(), where XXX is the name of the 
     * <code>listenerType</code>
     * argument.  For example, to get all of the MouseListeners for the
     * given Component <code>c</code>, one would write:
     * <pre>
     * MouseListener[] mls = (MouseListener[])
     *         (c.getListeners(MouseListener.class))
     * </pre>
     * If no such listener list exists, then an empty array is returned.
     * 
     * @param    listenerType   Type of listeners requested
     * @returns an array of all listeners added to this Component using 
     * addXXXListener, or an empty array if no such listeners have been 
     * added to this Component.
     * @since 1.3
     */

PropertyChangeListener extends java.util.EventListener and can be added
to Component by addPropertyChangeListener(PropertyChangeListener listener) and 
addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
mehods, so getListener should process it correctly.

Here is a minimized test:
--- ComponentTest.java ---
import java.awt.*;
import java.beans.*;
import java.util.*;
public class ComponentTest {
    public static void main (String[] args) {
        Canvas c = new Canvas();
        PropertyChangeListener pcl = new MyPropertyChangeListener();
        c.addPropertyChangeListener(pcl);
        EventListener[] l = c.getListeners(PropertyChangeListener.class);
        System.out.println(l.length);
        if (l.length == 1)
            System.out.println("OKAY");
        else
            System.out.println("FAILED");
    }
}

--- Output ---
%java -version
java version "1.3beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
Java(TM) HotSpot Client VM 
(build 1.3beta-O-release, 1.3beta-O-release, interpreted mode)
%java ComponentTest
0
FAILED

======================================================================

Comments
EVALUATION Commit to fix in Kestrel. eric.hawkes@eng 1999-09-10 This will require some design work as PropertyChangeListeners don't follow the basic event listener pattern. I've removed the "commit to fix". Here's the "punt" paragraph from the original getListeners() proposal: This proposal conciously ignores the problem created by addListenerMethods that take extra arguments, e.g. the overloading of addPropertyChangeListener that allows one to specify a single property name. Although one could accomodate listener lists like this by having getEventListeners() return an object that contained both the listener and the add listener arguments, we were uncomfortable with supporting this special case now. Support could easily be added in a future release. The only two listener lists we're aware of that take an extra parameter in addXXXListener are Component.propertyChangeListener and the static list Toolkit.AWTEventListener. We could provide explicit support for these two, e.g. by adding the following to java.util: public class EventListenerProxy implements EventListener { private final EventListener listener; private final Object[] parameters; public EventListenerProxy(EventListener listener, Object[] parameters) { Object[] v = new Object[parameters.length]; System.arraycopy(parameters, 0, v, 0, parameters.length); this.listener = listener; this.parameters = v; } public getListener() { return listener; } public getParameters() { return listener; } } The definition of getListeners() would be extended to mean that we'd return the real listener, or an EventListenerProxy for cases where there was an addXXXListener parameter. hans.muller@Eng 1999-09-13
13-09-1999