Remote EJB Marshalling failing with ClassCastException: Cannot cast java.util.TreeMap to ... , when using Collections.emptySortedMap() in JBoss EAP 7

Solution Verified - Updated

Environment

Red Hat JBoss Enterprise Application Platform (EAP) 7.3

Issue

  • Remote EJB Marshalling failing with ClassCastException: Cannot cast java.util.TreeMap to ... , when using Collections.emptySortedMap() in JBoss EAP 7

The class being serialized is :

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;

public class Example implements Serializable {
  private static final long serialVersionUID = 3L;  
  private SortedMap<String, String> map = Collections.emptySortedMap();
  private Date mop;
}

The exception is:

     [java] java.lang.ClassCastException: Cannot cast java.util.TreeMap to java.util.Date
     [java] 	at java.lang.Class.cast(Class.java:3369)
     [java] 	at org.jboss.marshalling.reflect.SerializableField.setObject(SerializableField.java:342)
     [java] 	at org.jboss.marshalling.river.RiverUnmarshaller.readFields(RiverUnmarshaller.java:1864)
     [java] 	at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1778)
     [java] 	at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1406)
     [java] 	at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:283)
     [java] 	at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:216)
     [java] 	at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)
     [java] 	at WriteReadExample.read(WriteReadExample.java:83)
     [java] 	at WriteReadExample.test(WriteReadExample.java:29)
     [java] 	at WriteReadExample.main(WriteReadExample.java:20)
     [java] Caused by: an exception which occurred:
     [java] 	in field Example.mop
     [java] 	in object Example@458c1321
     [java] 	in object of type Example

Resolution

Apply the latest cumulative patch / update that resolves This content is not included.JBEAP-20245

Workaround:

Change from :

  private SortedMap<String, String> map = Collections.emptySortedMap();
  private Date mop; 

to:

  private SortedMap<String, String> map = new TreeMap<String,String>();
  private Date mop; 

Root Cause

This content is not included.JBEAP-20245 - Cannot cast java.util.TreeMap to java.util.Date , if Collections.emptySortedMap(); and Date field name sorts after Map

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.