Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

POF deserialisation - Map values

BretCalveyOct 25 2013 — edited Oct 27 2013

Hi,

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;

  @SuppressWarnings("unchecked")

  @Override

  public void readExternal(PofReader reader) throws IOException {

  data = reader.readMap(1, new HashMap<Object, Set<Object>>());

  }

  @Override

  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>();

  setOfStuff.add("[STUFF]");

  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"

  xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"

  xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config coherence-pof-config.xsd">

  <user-type-list>

  <include>coherence-pof-config.xml</include>

  <user-type>

  <type-id>1001</type-id>

  <class-name>test.PofBug</class-name>

  </user-type>

  </user-type-list>

</pof-config>

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"?

Thanks

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 24 2013
Added on Oct 25 2013
2 comments
762 views