使用XMLEncoder的Java Bean持久性

时间:2013-04-08 20:30:19

标签: java persistence javabeans

我编写了一个包含HashMultiMap的bean类(来自Guava库)。我想使用JRE的XMLEncoder对bean进行XML编码。使用自定义PersistenceDelegate我已经成功地将bean写入文件。但是,当我尝试反序列化XML时,我得到了异常:

java.lang.NoSuchMethodException: <unbound>=HashMultimap.put("pz1", "pz2")

我做错了什么?

// create the bean
SomeBean sb = new SomeBean();

// add some data
HashMultimap<String, String> 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<?> type, Object oldInstance, Object newInstance,
                              Encoder out)
    {
        super.initialize(type, oldInstance, newInstance, out);

        com.google.common.collect.HashMultimap<String, String> m =
            (com.google.common.collect.HashMultimap) oldInstance;

        for (Map.Entry<String, String> entry : m.entries())
        {
            out.writeStatement(new Statement(oldInstance, "put", 
            new Object[] { entry.getKey(), entry.getValue() }));
        }

    }
}

public class SomeBean
{
    private HashMultimap<String, String> stateMap;

    public HashMultimap<String, String> getStateMap()
    {
        return stateMap;
    }

    public void setStateMap(HashMultimap<String, String> stateMap)
    {
        this.stateMap = stateMap;
    }
}

1 个答案:

答案 0 :(得分:5)

我还没有解决方案。但这里至少可以澄清问题。似乎Java 7 build 15及更高版本中的某些更改已经破坏了Statement声明所需的方法。如果将ExceptionListener添加到XmlEncoder中,它可以让您更好地了解它是如何失败的:

        encoder.setExceptionListener(new ExceptionListener() {
        @Override
        public void exceptionThrown(Exception e) {
            System.out.println("got exception. e=" + e);
            e.printStackTrace();
        }
    });

然后你会看到一个完整的堆栈跟踪:

java.lang.Exception: Encoder: discarding statement HashMultimap.put(Object, Object);
at java.beans.Encoder.writeStatement(Encoder.java:306)
at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:400)
at test2.XmlEncoderTest$CustomPersistenceDelegate.initialize(XmlEncoderTest.java:83)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:118)
at java.beans.Encoder.writeObject(Encoder.java:74)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:327)
at java.beans.Encoder.writeExpression(Encoder.java:330)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:454)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:115)
at java.beans.Encoder.writeObject(Encoder.java:74)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:327)
at java.beans.Encoder.writeExpression(Encoder.java:330)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:454)
at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:194)
at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:253)
at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:400)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:118)
at java.beans.Encoder.writeObject(Encoder.java:74)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:327)
at java.beans.Encoder.writeExpression(Encoder.java:330)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:454)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:115)
at java.beans.Encoder.writeObject(Encoder.java:74)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:327)
at java.beans.Encoder.writeObject1(Encoder.java:258)
at java.beans.Encoder.cloneStatement(Encoder.java:271)
at java.beans.Encoder.writeStatement(Encoder.java:301)
at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:400)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:330)

    ...

Caused by: java.lang.NoSuchMethodException: HashMultimap.put(Object, Object);
    at java.beans.Statement.invokeInternal(Statement.java:313)
    at java.beans.Statement.access$000(Statement.java:58)
    at java.beans.Statement$2.run(Statement.java:185)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.beans.Statement.invoke(Statement.java:182)
    at java.beans.Statement.execute(Statement.java:173)
    at java.beans.Encoder.writeStatement(Encoder.java:304)
    ... 51 more

由部分引起的表明找不到 put 方法。它看起来像是因为它不能正确匹配方法签名。它在java bean MethodFinder中失败了,但由于源代码不包含在JDK中,我无法很好地跟踪它。

如果我能找到确切的原因,我会更新。只是想在此期间为您提供更多信息。

<强>更新

我认为这是后续版本中的一个错误。这是一个单元测试,可以更直接地暴露bug(或意外行为)。下面的失败正是您的代码中发生的事情:

@Test
public void testMethodFinder() throws Exception {

    Method m0 = MethodFinder.findMethod(this.getClass(), "setUp", new Class<?>[0]);
    assertNotNull(m0);

    // this is okay, because method is declared in the type referenced
    Method m = MethodFinder.findMethod(Multimap.class, "put", new Class<?>[] { Object.class, Object.class });
    assertNotNull(m);

    try {
        // this fails, apparently because method is not declared in this subclass (is inherited from parent class)
        Method m2 = MethodFinder.findMethod(HashMultimap.class, "put", new Class<?>[] { Object.class, Object.class });
        assertNotNull(m2);
    } catch (Exception e) {
        System.out.println("got exception. e=" + e);
    }
}