Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4082708
Votes 1
Synopsis java.awt.event.ContainerEvent has wrong constructor
Category java:classes_awt
Reported Against 1.3 , 1.1.4 , 1.1.8_003
Release Fixed 1.5(tiger)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 4875299
Submit Date 30-SEP-1997
Description





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)
-----------

======================================================================
Work Around
N/A
Evaluation




Mohamed Sulthan ( xxxxx@xxxxx )		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  xxxxx@xxxxx  -- 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.
 xxxxx@xxxxx  2002-07-23




It is almost impossible to mark old ctor as deprecated.  
So, we should just add check in getContainer() method.

 xxxxx@xxxxx  2003-04-22

======================================================================
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang