Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4249035

Serializing EventListenerList causes ClassNotFound exceptions

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      03
    • CPU:
      x86, sparc
    • OS:
      solaris_2.6, windows_nt

      Backports

        Description



        Name: sg39081 Date: 06/23/99


        The EventListenerList.readMethod() throws ClassNotFoundExceptions
        when an EventListenerList object is serialized, under the following
        circumstances:

        a.) listeners added to the EventListenerList object aren't anonymous.

        b.) listeners added to the EventListenerList object don't
            come as part of the API, such as ActionListener.

        An example which demonstrates the problem is below;
        the stack trace, after running it, is as follows:


        java.lang.ClassNotFoundException: MyListener
                at java.lang.Class.forName0(Native Method)
                at java.lang.Class.forName(Class.java, Compiled Code)
                at javax.swing.event.EventListenerList.readObject(EventListenerList.java, Compiled Code)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.io.ObjectInputStream.invokeObjectReader(ObjectInputStream.java, Compiled Code)
                at java.io.ObjectInputStream.inputObject(ObjectInputStream.java, Compiled Code)
                at java.io.ObjectInputStream.readObject(ObjectInputStream.java, Compiled Code)
                at java.io.ObjectInputStream.readObject(ObjectInputStream.java, Compiled Code)
                at A.copyObject(A.java, Compiled Code)
                at A.<init>(A.java, Compiled Code)
                at A.main(A.java, Compiled Code)

        //File -- A.java
        //
        import java.io.*;
        import java.util.EventListener;
        import javax.swing.event.EventListenerList;

        interface MyListener extends EventListener{}

        public class A implements MyListener, EventListener, Serializable
        {
           public A()
           {
              EventListenerList listenerList = new EventListenerList();

              try
              {
                 //Using base EventListener for the the class type parameter
                 //and a non anonymous class for the listener
                 listenerList.add(EventListener.class, this);

                 //This will serialize without a problem.
                 copyObject(listenerList);



                 //Using sub interface of EventListener for the the class type parameter
                 //and a anonymous class for the listener
                 listenerList.add(MyListener.class, new MyListener(){});

                 //This will serialize without a problem.
                 copyObject(listenerList);



                 //Using sub interface of EventListener for the the class type parameter
                 //and a non anonymous class for the listener
                 listenerList.add(MyListener.class, this);

                 //The call to copyObject() below will cause a ClassNotFoundException exception.
                 copyObject(listenerList);
              }
              catch(Exception ex)
              {
                 ex.printStackTrace();
              }
           }

           /**
            * Makes a copy of an object, using serialization.
            *
            * @param o source object
            * @return copy of of the source object.
            */
           synchronized static public Object copyObject(final Object o)
           throws ClassNotFoundException, IOException, StreamCorruptedException
           {
              final ByteArrayOutputStream baos = new ByteArrayOutputStream();
              final ObjectOutput os = new ObjectOutputStream(baos);

              os.writeObject(o);
              os.flush();
              os.close();
              baos.close();

              final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
              final ObjectInput is = new ObjectInputStream(bais);

              return is.readObject();
           }

           public static void main(String args[] )
           {
              A a = new A();
           }
        }
        //
        //File -- A.java

        (Review ID: 55295)
        ======================================================================

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                amfowler Anne Fowler (Inactive)
                Reporter:
                sgoodsunw Sheri Good (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: