如果将对象移动到另一个包或重命名,我该如何反序列化该对象? [英] How can I deserialize the object, if it was moved to another package or renamed?

查看:94
本文介绍了如果将对象移动到另一个包或重命名,我该如何反序列化该对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情况:

有一个序列化文件,由旧版本的应用程序创建。不幸的是,已经为序列化的类改变了包。现在我需要将此文件中的信息加载到同一个类中,但位于不同的包中。此类已定义 serialVersionUID 且未更改(即兼容)。

There is a serialization file, created by the older version of the application. Unfortunately, the package has changed for the class, that has been serialized. And now I need to load the information from this file into the same class, but located in different package. This class has serialVersionUID defined and has not changed (i.e. is compatible).

问题:是否可以加载这个文件中的新类实例使用任何技巧(除了将类复制到旧包中然后使用反序列化包装器逻辑)?可以使用 readResolve() 从移动/重命名课程中恢复?如果没有,请解释原因。

Question: Is it possible to load the new class instances from this file using any tricks (except trivial copying the class into old package and then using the deserialization wrapper logic)? It is possible to use readResolve() to recover from moving/renaming the class? If not, please, explain why.

推荐答案


问题:是否可以加载$ b来自这个文件的$ b新类实例
使用任何技巧(除了琐碎的
将类复制到旧包中,然后用
然后使用反序列化包装器
逻辑)?

Question: Is it possible to load the new class instances from this file using any tricks (except trivial copying the class into old package and then using the deserialization wrapper logic)?

我不认为你可以使用任何其他技巧,不涉及至少部分重新实现序列化协议。

I don't think there are any other "tricks" you could use that don't involve at least a partial reimplementation of the serialization protocol.


可以使用readResolve()来
从移动/重命名
类中恢复?如果没有,请解释原因。

It is possible to use readResolve() to recover from moving/renaming the class? If not, please, explain why.

不,因为反序列化机制会在尝试定位的阶段更早失败正在被反序列化的类 - 它无法知道不同包中的类是否有一个 readResolve()它应该使用的方法。

No, because the deserialization mechanism will fail much earlier, at the stage where it tries to locate the class that's being deserialized - it has no way of knowing that a class in a different package has a readResolve() method it's supposed to use.

这篇关于如果将对象移动到另一个包或重命名,我该如何反序列化该对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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