United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4082708 : java.awt.event.ContainerEvent has wrong constructor

Details
Type:
Bug
Submit Date:
1997-09-30
Status:
Resolved
Updated Date:
2003-05-25
Project Name:
JDK
Resolved Date:
2003-05-25
Component:
client-libs
OS:
solaris_2.5,generic
Sub-Component:
java.awt
CPU:
sparc,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.1.4,1.1.8_003,1.3.0
Fixed Versions:
5.0 (tiger)

Related Reports

Sub Tasks

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



Hardware and Software, Engineered to Work Together