JPPF Issue Tracker
star_faded.png
Please log in to bookmark issues
bug_report_small.png
CLOSED  Bug report JPPF-470  -  JPPF serialization: serialization of single byte and char values results in ClassCastException
Posted Aug 21, 2016 - updated Aug 21, 2016
icon_info.png This issue has been closed with status "Closed" and resolution "RESOLVED".
Issue details
  • Type of issue
    Bug report
  • Status
     
    Closed
  • Assigned to
     lolo4j
  • Progress
       
  • Type of bug
    Not triaged
  • Likelihood
    Not triaged
  • Effect
    Not triaged
  • Posted by
     lolo4j
  • Owned by
    Not owned by anyone
  • Category
    Core
  • Resolution
    RESOLVED
  • Priority
    Normal
  • Reproducability
    Always
  • Severity
    Normal
  • Targetted for
    icon_milestones.png JPPF 5.2.1
Issue description
From this forum post.

When serializing a byte value using the built-in JPPFSerialization, I get the following stack trace:
java.io.IOException: java.lang.Byte cannot be cast to java.lang.Integer
  at org.jppf.serialization.JPPFObjectOutputStream.writeObjectOverride(JPPFObjectOutputStream.java:91)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:343)
  at test.org.jppf.serialization.TestJPPFSerialization.copyBySerialization(TestJPPFSerialization.java:137)
  at test.org.jppf.serialization.TestJPPFSerialization.testByte(TestJPPFSerialization.java:88)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:62)
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.JPPFObjectOutputStream.writeObjectOverride(JPPFObjectOutputStream.java:89)
  ... 12 more
For a char value we get an equivalent stack:
java.io.IOException: java.lang.Character cannot be cast to java.lang.Integer
  at org.jppf.serialization.JPPFObjectOutputStream.writeObjectOverride(JPPFObjectOutputStream.java:91)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:343)
  at test.org.jppf.serialization.TestJPPFSerialization.copyBySerialization(TestJPPFSerialization.java:150)
  at test.org.jppf.serialization.TestJPPFSerialization.testChar(TestJPPFSerialization.java:137)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:62)
Caused by: java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Integer
  at org.jppf.serialization.Serializer.writeDeclaredFields(Serializer.java:214)
  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)
  ... 12 more
Steps to reproduce this issue
The following JUnit tests reproduce the exceptions in the description:
@Test
public void testByte() throws Exception {
  Byte b1 = new Byte((byte) 127);
  Byte b2 = (Byte) copyBySerialization(b1);
  assertEquals(b1, b2);
}
 
@Test
public void testChar() throws Exception {
  Character c1 = new Character((char) 127);
  Character c2 = (Character) copyBySerialization(c1);
  assertEquals(c1, c2);
}
 
private Object copyBySerialization(final Object src) throws Exception {
  Object dest = null;
  try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
      JPPFObjectOutputStream joos = new JPPFObjectOutputStream(baos)) {
    joos.writeObject(src);
    joos.flush();
    byte[] bytes = baos.toByteArray();
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        JPPFObjectInputStream jois = new JPPFObjectInputStream(bais)) {
      dest = jois.readObject();
    }
  }
  return dest;
}

#5
Comment posted by
 lolo4j
Aug 21, 06:17
Testing to the limits, I also noticed that serialization/deserialization fails for Integer.MIN_VALUE (-2147483648) and Long.MIN_VALUE (-9223372036854775808L) but not for other values.
#6
Comment posted by
 lolo4j
Aug 21, 08:16
Fixed in: