JDK-4082708 : java.awt.event.ContainerEvent has wrong constructor
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.4,1.1.8_003,1.3.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,solaris_2.5
  • CPU: generic,sparc
  • Submitted: 1997-09-30
  • Updated: 2017-05-16
  • Resolved: 2003-05-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Description

Name: mgC56079			Date: 09/30/97



The ContainerEvent has the constructor:
    public ContainerEvent(Component source, int id, Component child) 
should instead have:
    public ContainerEvent(Container source, int id, Component child) 

The getContainer() method will fail if the source is not a container
(comment in this method says that cast is always OK. It isn't.)

---- The ContainerEvent's source ----
    /**
     * Constructs a ContainerEvent object.
     * 
     * @param source the Component object (container) that originated the event
     * @param id     an integer indicating the type of event
     * @param child  the component that was added or removed
     */
    public ContainerEvent(Component source, int id, Component child) {
        super(source, id);
        this.child = child;
    }

    /**
     * Returns the originator of the event.
     *
     * @return the Container object that originated the event
     */
    public Container getContainer() {
        return (Container)source; // cast should always be OK, type was checked in constructor
    }

---- Here is the test (CE.java) ----
import java.awt.Button;
import java.awt.Container;
import java.awt.event.ContainerEvent;

public class CE {
public static void main(String[] args) {
  Button b=new Button("label");
  ContainerEvent ce=new ContainerEvent(b,ContainerEvent.COMPONENT_ADDED,b);
  Container c=ce.getContainer();
  System.exit(0); //will not exit under 1.2 without this line
}
}
---- output from the test ----
% java CE
java.lang.ClassCastException: java.awt.Button
	at java.awt.event.ContainerEvent.getContainer(ContainerEvent.java:86)
	at CE.main(CE.java:9)
-----------

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

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b08
2004-08-24

EVALUATION Name: ksT78225 Date: 03/29/99 Mohamed Sulthan (###@###.###) March 30th 1999 Bug is reproducible in both Win32 and Solaris ( Tested with JDK 1.2fcs-I ). Result: --------- java.lang.ClassCastException: java.awt.Button at java.awt.event.ContainerEvent.getContainer(ContainerEvent.java:102) at CE.main(CE.java:14) Exception in thread "main" Process completed with exit code 1 ------------------------------------------------- I commented the following line from the submitter code, // System.exit(0); the program is exits with above Exception. ====================================================================== 5/21/2000 kevin.ryan@eng -- still true as of 1.1.8_003 and 1.3.0-C. The ideal fix would be to add a more appropriate constructor (which takes a Container instead of a Component) and to document the current constructor as being kept around only for backward compatibility. This would have to be done in 1.5. ###@###.### 2002-07-23 Name: osR10079 Date: 04/22/2003 It is almost impossible to mark old ctor as deprecated. So, we should just add check in getContainer() method. ###@###.### 2003-04-22 ======================================================================
2003-04-22

SUGGESTED FIX Name: osR10079 Date: 04/22/2003 ------- ContainerEvent.java ------- *** /tmp/dc_ai2h Mon Mar 24 09:33:56 2003 --- ContainerEvent.java Mon Mar 24 09:24:22 2003 *************** *** 93,102 **** /** * Returns the originator of the event. * ! * @return the Container object that originated the event */ public Container getContainer() { ! return (Container)source; // cast should always be OK, type was checked in constructor } /** --- 93,104 ---- /** * Returns the originator of the event. * ! * @return the <code>Container</code> object that originated ! * the event, or <code>null</code> if the object is not a ! * <code>Container</code>. */ public Container getContainer() { ! return (source instanceof Container) ? (Container)source : null; } /** ###@###.### 2003-04-22 ======================================================================
2003-04-22