每次更改时 Eclipse 自动生成serialVersionUID

发布于 2024-08-05 07:56:06 字数 841 浏览 8 评论 0原文

Eclipse 很好地为我生成了serialVersionUID。但这似乎是被动代码生成,因为当我更改文件时,id 不会自动更新,除非我再次生成。

有什么方法可以在每次更改内容时生成serialVersionUID? “保存操作”似乎不包含这样的选项 - 有人找到某种方法来做到这一点吗?

如果它可以与 IDE 保存操作或类似的操作结合起来,那就太好了,这样我就可以在不影响序列化的情况下恢复更改。

此致, Touko

编辑:@gustafc:这有两个要点:

  • 如果我理解正确,不同的编译器可能会得到不同的serialVersionUID值
    • 来自可序列化API但是,强烈建议所有可序列化类显式声明serialVersionUID值,因为默认的serialVersionUID计算对类细节高度敏感,这些细节可能因编译器实现而异,因此可能会在反序列化期间导致意外的InvalidClassExceptions
  • 我正在使用类似于在服务器上执行操作的对象的命令模式。因此,即使对象内容没有更改,当客户端和服务器上的类内容不同时,最好能够捕获这些情况。
    • 但再想一想,这可能不适用于自动生成的值,因为只有内容更改才会改变它?所以,实际上我想要一个自动递增的serialVersionUID

这听起来合理吗?

Summa summarum,经过更多思考,每次更改时自动递增的serialVersionUID会更好......

Eclipse nicely generates the serialVersionUID for me. But this seems to be passive code generation as the id won't be automatically updated as I change the file unless I do the generation again.

Is there some way to have the serialVersionUID being generated every time I change the contents? The "Save Actions" don't seem to include such an option - has somebody found some way to do this?

It would be nice that it could be combined with the IDE save actions or something similar so that I could revert the change if I were doing that doesn't affect the serialization.

Best regards,
Touko

EDIT: @gustafc: There are two main points for this:

  • If I have understood correctly, different compilers may end up with different values for serialVersionUID
    • From Serializable API :However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization
  • I'm using kind of command-pattern like objects doing things at server. So, even though the object content isn't changed, it would be nice to catch the cases when the class content is different at client and server.
    • But on thinking another time, this wuoldn't probably work with auto-generated value since only the content changes would change that? So, actually I'd like to have an auto-incremented serialVersionUID

Does this sound sensible?

Summa summarum, after more thinking, an auto-incremented serialVersionUID incremented at each change would be even better...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转身以后 2024-08-12 07:56:06

Eclipse 中没有标准功能来执行此操作。

There is no standard functionality in Eclipse to do this.

情栀口红 2024-08-12 07:56:06

您可以使用 Ant SerialVer 任务 在构建时更新串行版本。

You could update the serial version at build time with the Ant SerialVer task.

怎会甘心 2024-08-12 07:56:06

当我更改文件时,id 不会自动更新,除非我再次生成。

AFAIK 默认的 serialVersionUID 就是这样——一个基于类的当前“形状”生成的 id。因此,如果您希望在更改任何内容时生成您的 id,只需将其省略即可。

但是,如果您只想有时更改 serialVersionUID - 只需更改它即可。随机修改id即可。

随机数仅比连续数字好一点(如果您可以假设在您的环境中没有其他人组成具有相同限定名称的类),因此您甚至可以从 serialVersionUID = 1 开始并递增此值数量随意。

the id won't be automatically updated as I change the file unless I do the generation again.

AFAIK default serialVersionUID is just this -- an id generated based on current "shape" of a class. So if you want your id generated when you change anything -- just leave it out.

However, if you want to change serialVersionUID only sometimes -- just change it. Random modification to the id will do.

Random number is only little better than consecutive numbers (if you can assume no one else makes up a class with the same qualified name in your environment), so you may even start from serialVersionUID = 1 and increment this number at will.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文