Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Bean Persistence with XMLEncoder
    primarykey
    data
    text
    <p>I've written a bean class containing a HashMultiMap (from the Guava library). I would like to XML encode the bean using the JRE's XMLEncoder. Using a custom PersistenceDelegate I've successfully written the bean to file. However, when I attempt to deserialize the XML I get the exception: </p> <p><code>java.lang.NoSuchMethodException: &lt;unbound&gt;=HashMultimap.put("pz1", "pz2")</code> </p> <p>What am I doing wrong? </p> <pre><code>// create the bean SomeBean sb = new SomeBean(); // add some data HashMultimap&lt;String, String&gt; stateMap = HashMultimap.create(); stateMap.put("pz1", "pz2"); stateMap.put("pz3", "pz4"); sb.setStateMap(stateMap); // encode as xml FileOutputStream os = new FileOutputStream("myXMLFile.xml"); XMLEncoder encoder = new XMLEncoder(os); encoder.setPersistenceDelegate(HashMultimap.class, new CustomPersistenceDelegate()); encoder.writeObject(sb); // decode the xml FileInputStream is = new FileInputStream("myXMLFile.xml"); XMLDecoder decoder = new XMLDecoder(is); Object deSerializedObject = decoder.readObject(); class CustomPersistenceDelegate extends DefaultPersistenceDelegate { protected Expression instantiate(Object oldInstance, Encoder out) { return new Expression(oldInstance, oldInstance.getClass(), "create", null); } protected void initialize(Class&lt;?&gt; type, Object oldInstance, Object newInstance, Encoder out) { super.initialize(type, oldInstance, newInstance, out); com.google.common.collect.HashMultimap&lt;String, String&gt; m = (com.google.common.collect.HashMultimap) oldInstance; for (Map.Entry&lt;String, String&gt; entry : m.entries()) { out.writeStatement(new Statement(oldInstance, "put", new Object[] { entry.getKey(), entry.getValue() })); } } } public class SomeBean { private HashMultimap&lt;String, String&gt; stateMap; public HashMultimap&lt;String, String&gt; getStateMap() { return stateMap; } public void setStateMap(HashMultimap&lt;String, String&gt; stateMap) { this.stateMap = stateMap; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload