XStream won't call readObject()

安稳与你 提交于 2019-12-22 18:44:11

问题


I have code that is modeled as such:

class A {
    private transient Foo foo = new Foo();
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        foo = new Foo();
    }
}

class B extends A {}

I added readObject() to A so that during deserialization, the transient foo will be initialized. However, I stuck breakpoints in my code and could see that XStream is not calling readObject() at all. I also tried sticking readObject() in class B that calls an initFoo() function in A, and that didn't work either.

The FAQ on the website does not seem to imply any other boilerplate is necessary for this to work. Why is it not being called?


回答1:


It seems that the XStream documentation was incorrect, or at least misleading. It says in http://x-stream.github.io/faq.html#Serialization_initialize_transient,

Use the latter [readObject] in class hierarchies, readResolve is not called for base classes.

However, replacing readObject() with readResolve() in the code I listed in my question, it seems to be called and foo is being initialized properly. This is confusing though because according to documentation of both XStream and java.io.Serializable, readObject() seems to be what I need.

The breakpoint inside the method still is not being hit however. It may have to do with the way these Serializable methods are being called.



来源:https://stackoverflow.com/questions/15752945/xstream-wont-call-readobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!