XStream不会调用readObject() [英] XStream won't call readObject()

查看:144
本文介绍了XStream不会调用readObject()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样建模的:

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 {}

我将readObject()添加到A,以便在反序列化期间,初始化瞬态 foo 。但是,我在代码中遇到了断点,并且可以看到XStream根本没有调用readObject()。我也尝试在类B中使用readObject()来调用A中的initFoo()函数,但这也不起作用。

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?

推荐答案

似乎XStream文档不正确,或者至少具有误导性。它在 http://x-stream.github.io/faq.html#Serialization_initialize_transient

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,


在类层次结构中使用后者[readObject],不为基类调用readResolve。

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

然而,用替换 readObject() readResolve()在我在我的问题中列出的代码中,似乎被调用并且foo正在被正确初始化。这很令人困惑,因为根据XStream和java.io.Serializable的文档,readObject()似乎是我需要的。

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.

方法内的断点仍然是然而,没有受到打击。它可能与调用这些Serializable方法的方式有关。

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.

这篇关于XStream不会调用readObject()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆