java.lang.ClassCastException: cannot assign instance of org.springframework.integration.message.MessageHeadersTests$Foo to field org.springframework.integration.message.MessageHeadersTests$Foo.foo of type java.lang.String in instance of org.springframework.integration.message.MessageHeadersTests$Foo

Spring JIRA | Artem Bilan | 2 months ago
  1. 0

    Although my test-case isn't related to the {{MessageHeaders}} it demonstrates the problem: {code:java} @Test public void serializeWithNonSerializableHeader2() throws Exception { serializeAndDeserialize(new Foo("foo")); } private static Object serializeAndDeserialize(Object object) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(object); out.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); Object result = in.readObject(); in.close(); return result; } private static class Foo implements Serializable { private final String foo; private Foo(String foo) { this.foo = foo; } private void writeObject(ObjectOutputStream out) throws IOException { if ("foo".equals(this.foo)) { out.writeObject(new Foo(this.foo.toUpperCase())); } else { out.defaultWriteObject(); } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); } } {code} Where the result is like: {noformat} java.lang.ClassCastException: cannot assign instance of org.springframework.integration.message.MessageHeadersTests$Foo to field org.springframework.integration.message.MessageHeadersTests$Foo.foo of type java.lang.String in instance of org.springframework.integration.message.MessageHeadersTests$Foo at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2133) at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1305) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2006) at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:501) at org.springframework.integration.message.MessageHeadersTests$Foo.readObject(MessageHeadersTests.java:185) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) {noformat} Similar code we have in the {{MessageHeaders}}: {code:java} private void writeObject(ObjectOutputStream out) throws IOException { ... if (keysToIgnore.isEmpty()) { out.defaultWriteObject(); } else { ... out.writeObject(new MessageHeaders(this, keysToIgnore)); } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); } {code} We don't catch the {{ClassCastException}} problem in the {{MessageHeaders}} because the {{headers}} field is a {{Map<>}}, therefore it can be assigned to the {{MessageHeaders}}. But at the same time {{MessageHeaders}} is immutable, therefore having a subclass and use its {{getRawHeaders()}} for modification makes us surprised that we deal with {{MessageHeaders}} instead of {{HashMap}} after deserialization. Not sure how to fix properly, but seems for me making {{headers}} as {{transient}} and use an appropriate {{writeObject(headersToSerialize)}} and, therefore, {{this.headers = readObject()}} would make it possible and meet Java serialization standards. OTOH {{transient}} may make other serialization systems, like Kryo, powerless...

    Spring JIRA | 2 months ago | Artem Bilan
    java.lang.ClassCastException: cannot assign instance of org.springframework.integration.message.MessageHeadersTests$Foo to field org.springframework.integration.message.MessageHeadersTests$Foo.foo of type java.lang.String in instance of org.springframework.integration.message.MessageHeadersTests$Foo
  2. 0

    Spark 1.6.0 executor dies because of ClassCastException and causes timeout

    Stack Overflow | 11 months ago | Alexandru Rosianu
    java.io.IOException: java.lang.ClassCastException: cannot assign instance of scala.collection.immutable.HashMap$SerializationProxy to field org.apache.spark.executor.TaskMetrics._accumulatorUpdates of type scala.collection.immutable.Map in instance of org.apache.spark.executor.TaskMetrics
  3. 0

    spark sql dataframe join with renaming in a loop

    Stack Overflow | 10 months ago | user2038119
    java.io.IOException: java.lang.ClassCastException: cannot assign instance of scala.collection.immutable.HashMap$SerializationProxy to field org.apache.spark.executor.TaskMetrics._accumulatorUpdates of type scala.collection.immutable.Map in instance of org.apache.spark.executor.TaskMetrics
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    [jira] [Commented] (SPARK-12675) Executor dies because of ClassCastException and causes timeout

    spark-issues | 8 months ago | Matt Butler (JIRA)
    java.io.IOException: java.lang.ClassCastException: cannot assign instance of scala.collection.immutable.HashMap$SerializationProxy to field org.apache.spark.executor.TaskMetrics._accumulatorUpdates of type scala.collection.immutable.Map in instance of org.apache.spark.executor.TaskMetrics
  6. 0

    [jira] [Commented] (SPARK-12675) Executor dies because of ClassCastException and causes timeout

    spark-issues | 8 months ago | Matt Butler (JIRA)
    java.io.IOException: java.lang.ClassCastException: cannot assign instance of scala.collection.immutable.HashMap$SerializationProxy to field org.apache.spark.executor.TaskMetrics._accumulatorUpdates of type scala.collection.immutable.Map in instance of org.apache.spark.executor.TaskMetrics

    5 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.ClassCastException

      cannot assign instance of org.springframework.integration.message.MessageHeadersTests$Foo to field org.springframework.integration.message.MessageHeadersTests$Foo.foo of type java.lang.String in instance of org.springframework.integration.message.MessageHeadersTests$Foo

      at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues()
    2. Java RT
      ObjectInputStream.defaultReadObject
      1. java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2133)
      2. java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1305)
      3. java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2006)
      4. java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:501)
      4 frames
    3. Spring Integration Core
      MessageHeadersTests$Foo.readObject
      1. org.springframework.integration.message.MessageHeadersTests$Foo.readObject(MessageHeadersTests.java:185)
      1 frame
    4. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      4. java.lang.reflect.Method.invoke(Method.java:497)
      4 frames