2 Replies Latest reply: Oct 27, 2013 5:15 PM by Leo_TA RSS

    POF deserialisation - Map values




      I can sort of understand why this happens, but it has caught me out a few times.


      I have an object that I want to pass to an Invocation Service (POF enabled) and this object has a "map" field...


      private Map<String, Set<Object>> data;


      When this is deserialised, the values in the map are not Sets, but instead com.tangosol.util.ImmutableArrayList$ListView


      I assume this is a limitation of POF because it has no idea what sort of collection it needs to create for map values


      The code below will produce a ClassCastException...


      package test;
      import java.io.IOException;
      import java.util.HashMap;
      import java.util.HashSet;
      import java.util.Map;
      import java.util.Set;
      import com.tangosol.io.Serializer;
      import com.tangosol.io.pof.ConfigurablePofContext;
      import com.tangosol.io.pof.PofReader;
      import com.tangosol.io.pof.PofWriter;
      import com.tangosol.io.pof.PortableObject;
      import com.tangosol.util.Binary;
      import com.tangosol.util.ExternalizableHelper;
      public class PofBug implements PortableObject {
        private Map<String, Set<Object>> data;
        public void readExternal(PofReader reader) throws IOException {
        data = reader.readMap(1, new HashMap<Object, Set<Object>>());
        public void writeExternal(PofWriter writer) throws IOException {
        writer.writeMap(1, data);
        public static void main(String[] args) {
        PofBug bug = new PofBug();
        Map<String, Set<Object>> testData = new HashMap<String, Set<Object>>();
        Set<Object> setOfStuff = new HashSet<Object>();
        testData.put("[KEY]", setOfStuff);
        bug.data = testData;
        Serializer serializer = new ConfigurablePofContext("/test/test-pof-config.xml");
        Binary bin = ExternalizableHelper.toBinary(bug, serializer);
        PofBug deserialised = (PofBug) ExternalizableHelper.fromBinary(bin, serializer);
        // BANG!!!
        Set<Object> mySet = deserialised.data.get("[KEY]");
        System.out.println("IT WORKED!!!! " + mySet);


      The POF config is...


      <pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config coherence-pof-config.xsd">


      I assume that the thing to do will be to manually write each entry (or wrap my Set<Object> values into another type)


      Is this a known "feature"/"bug"?