JPPF, java, parallel computing, distributed computing, grid computing, parallel, distributed, cluster, grid, cloud, open source, android, .net
JPPF

The open source
grid computing
solution

 Home   About   Features   Download   Documentation   On Github   Forums 
January 19, 2021, 11:01:05 PM *
Welcome,
Please login or register.

Login with username, password and session length
Advanced search  
News: New users, please read this message. Thank you!
  Home Help Search Login Register  
Pages: [1]   Go Down

Author Topic: ClassCastException when serializing  (Read 1112 times)

magneticflux-

  • JPPF Padawan
  • *
  • Posts: 7
ClassCastException when serializing
« on: August 21, 2016, 01:24:54 AM »

Hello! I'm encountering a ClassCastException during serialization using the JPPF built-in serialization. The specific error is here:
Code: [Select]
Caused by: java.lang.ClassCastException: java.lang.Byte cannot be cast to java.lang.Integer
at org.jppf.serialization.Serializer.writeDeclaredFields(Serializer.java:208)
at org.jppf.serialization.Serializer.writeFields(Serializer.java:192)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:146)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:122)
at org.jppf.serialization.Serializer.writeDeclaredFields(Serializer.java:219)
at org.jppf.serialization.Serializer.writeFields(Serializer.java:192)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:146)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:122)
at org.jppf.serialization.Serializer.writeDeclaredFields(Serializer.java:219)
at org.jppf.serialization.Serializer.writeFields(Serializer.java:192)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:146)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:122)
at org.jppf.serialization.Serializer.writeDeclaredFields(Serializer.java:219)
at org.jppf.serialization.Serializer.writeFields(Serializer.java:192)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:146)
at org.jppf.serialization.Serializer.writeObject(Serializer.java:122)
at org.jppf.serialization.JPPFObjectOutputStream.writeObjectOverride(JPPFObjectOutputStream.java:89)
... 127 more
I tracked it down to this piece of code in org.jppf.serialization.Serializer that appears to be the culprit, and also appears to be a mistype:
Code: [Select]
void writeDeclaredFields(final Object obj, final ClassDescriptor cd) throws Exception {
    for (FieldDescriptor fd: cd.fields) {
      //if (traceEnabled) try { log.trace("writing field '" + fd.name + "' of object " + obj); } catch(Exception e) {}
      Object val = fd.field.get(obj);
      if (fd.type.primitive) {
        switch(fd.type.signature.charAt(0)) {
          case 'B': out.write((Integer) val); break;
          case 'S': out.writeShort((Short) val); break;
          case 'I': writeInt((Integer) val); break;
          case 'J': writeLong((Long) val); break;
          case 'F': writeFloat((Float) val); break;
          case 'D': writeDouble((Double) val); break;
          case 'C': out.writeChar((Integer) val); break;
          case 'Z': out.writeBoolean((Boolean) val); break;
        }
      }
      else if (fd.type.enumType) writeObject(val == null ? null : ((Enum) val).name());
      else writeObject(val);
    }
  }
Notice the pattern inside the switch statement, and how Byte does not follow that pattern. I suggest changing the Byte line to
Code: [Select]
          case 'B': out.writeByte((Byte) val); break;
There also may be problems with the Character line, but I have not encountered any yet.

Versions:
JPPF 5.2
Java 8, Update 101
Windows 10
Logged

lolo

  • Administrator
  • JPPF Council Member
  • *****
  • Posts: 2262
    • JPPF Web site
Re: ClassCastException when serializing
« Reply #1 on: August 21, 2016, 05:40:20 AM »

Hello,

Thank you very much for catching this problem and for the detailed anlaysis.
This is indeed a JPPF bug, which I registered as JPPF-470 JPPF serialization: serialization of single byte and char values results in ClassCastException.

I am currently working on a fix (and proper unit tests) and will deliver it as part of the upcoming v5.2.1 maintenance release. This should happen very soon (in the next few days) since I also wanted to deliver an important enhancement (JPPF-467), so I'm not going to wait until the currently planned release date of September 15th.

Sincerely,
-Laurent
Logged

magneticflux-

  • JPPF Padawan
  • *
  • Posts: 7
Re: ClassCastException when serializing
« Reply #2 on: August 21, 2016, 05:48:10 AM »

Thank you! I'm extremely grateful for the timely reply, and I'm glad I could help. I look forward to the next release!
Logged
Pages: [1]   Go Up
 
JPPF Powered by SMF 2.0 RC5 | SMF © 2006–2011, Simple Machines LLC Get JPPF at SourceForge.net. Fast, secure and Free Open Source software downloads