1 Reply Latest reply: Jun 12, 2010 12:10 PM by Kayaman RSS

    ClassCastException: SimpleTimeZone to ZoneInfo in readObject

    843790
      Hello

      I have a java application running on weblogic.
      Java version - 1.6.0_17-b04
      And a client application written in Java as well (same version).
      There are objects with GregorianCalendar which are transfered from server to client via RMI.
      I occasionally get the following error:

      Caused by: java.lang.ClassCastException: java.util.SimpleTimeZone cannot be cast to sun.util.calendar.ZoneInfo
      at java.util.Calendar$1.run(Unknown Source)
      at java.util.Calendar$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.util.Calendar.readObject(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
      at java.io.ObjectInputStream.readSerialData(Unknown Source)
      at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
      at java.io.ObjectInputStream.readSerialData(Unknown Source)
      at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
      at java.io.ObjectInputStream.readSerialData(Unknown Source)
      at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
      at java.io.ObjectInputStream.readSerialData(Unknown Source)
      at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
      at java.io.ObjectInputStream.readSerialData(Unknown Source)
      at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
      at java.io.ObjectInputStream.readSerialData(Unknown Source)
      at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.readObject(Unknown Source)
      at java.util.ArrayList.readObject(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)

      I see that the readObject of Calendar code is:
      ZoneInfo zi = null;
           try {
               zi = AccessController.doPrivileged(
                     new PrivilegedExceptionAction<ZoneInfo>() {
                     public ZoneInfo run() throws Exception {
                         return (ZoneInfo) input.readObject();
                     }
                    },
                    CalendarAccessControlContext.INSTANCE);
           } catch (PrivilegedActionException pae) {
               Exception e = pae.getException();
               if (!(e instanceof OptionalDataException)) {
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else if (e instanceof IOException) {
                    throw (IOException) e;
                } else if (e instanceof ClassNotFoundException) {
                    throw (ClassNotFoundException) e;
                }
                throw new RuntimeException(e);
               }
           }
           if (zi != null) {
               zone = zi;
           }
      
           // If the deserialized object has a SimpleTimeZone, try to
           // replace it with a ZoneInfo equivalent (as of 1.4) in order
           // to be compatible with the SimpleTimeZone-based
           // implementation as much as possible.
           if (zone instanceof SimpleTimeZone) {
               String id = zone.getID();
               TimeZone tz = TimeZone.getTimeZone(id);
               if (tz != null && tz.hasSameRules(zone) && tz.getID().equals(id)) {
                zone = tz;
               }
           }
      There is casting to ZoneInfo no matter what's the class is actually instance of.
      In my case, the calendar sometimes contains SimpleTimeZone and not ZoneInfo which causes for the ClassCastException.
      I also find that there are occasions where the zone variable holds SimpleTimeZone (gets to the line "if (zone instanceof SimpleTimeZone)" and equals true) but doesn't fail in "return (ZoneInfo) input.readObject();".

      Any idea?