NetBeans 能否为 Java 类生成自动串行版本 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1373215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can NetBeans generate an automatic serial version ID for a Java class?
提问by Thomas Owens
I would like to remove some warnings for some classes by generating an automatic serial version ID. In Eclipse, this is trivial to do - the IDE can generate one automatically and add it to the class. However, I don't see this functionality in NetBeans. Is it available? If so, where is it? If not, is there a plugin that can enable it?
我想通过生成自动串行版本 ID 来删除某些类的一些警告。在 Eclipse 中,这是微不足道的 - IDE 可以自动生成一个并将其添加到类中。但是,我在 NetBeans 中没有看到此功能。是可用的么?如果有,它在哪里?如果没有,是否有可以启用它的插件?
回答by lapo
Actually my solution to that "problem" was to deactivate that warningin my project configuration (I use Eclipse but I guess for NetBeans is the same) as IMHO it's a wrong warning: not havinga serialVersion is the safest choice because the JVM calculates one that is unique on launch (something like the hash of the class) while adding it explicitly then burdens youto take care to update it if and only if you made incompatible changes to your code.
实际上,我对该“问题”的解决方案是在我的项目配置中停用该警告(我使用 Eclipse,但我猜 NetBeans 是相同的),因为恕我直言,这是一个错误的警告:没有serialVersion 是最安全的选择,因为 JVM 会计算一个这是在推出独特(类似类的哈希),同时明确将它再负担你的照顾,当且仅当你做你的代码不兼容的更改进行更新。
So, if you do not care about that, it's better to avoid that value (this way it's only compatible with version that are compatible for sure, but with some false-positives: it thinks that's not compatible, but in fact it would) instead of putting there a fixed value that you would (probably, in my case) forget to update when needed, leading to actual validity errors (false-negatives: it thinks that it is compatible but it is not).
所以,如果你不关心这个,最好避免这个值(这样它只与肯定兼容的版本兼容,但有一些误报:它认为这不兼容,但实际上它会)相反放置一个固定值,你会(可能,在我的情况下)忘记在需要时更新,导致实际有效性错误(假阴性:它认为它是兼容的,但事实并非如此)。
回答by Michael Borgwardt
This is trivial to do manually as well, unless you need to stay compatible with existing serialized instances. Just copy this line into the class:
这也很容易手动完成,除非您需要与现有的序列化实例保持兼容。只需将此行复制到类中:
private static final long serialVersionUID = 1L;
Note that there is absolutely no requirement for the serialVersionUIDto be unique across classes.
请注意,绝对没有要求serialVersionUID跨类是唯一的。
回答by Koekiebox
回答by Koekiebox
To disable these warnings in Netbeans :
要在 Netbeans 中禁用这些警告:
- Open: Tools -> Options
- Open: Editor -> Hints tab
- Select Language: Java
- Uncheck Standard Javac warnings -> Serialization
- OK
- 打开:工具 -> 选项
- 打开:编辑器 -> 提示选项卡
- 选择语言:Java
- 取消选中标准 Javac 警告 -> 序列化
- 行
I also had to clear ~/.netbeans/6.8/var/cache and restart Netbeans to clear the warnings from the Tasks list.
我还必须清除 ~/.netbeans/6.8/var/cache 并重新启动 Netbeans 以清除任务列表中的警告。
回答by Leonardo Saracini
(For user that where ask for same question now)
I 'm using nb7.4 and found this plugin from nb6.5 to nb7.4
plugin
I tested it in nb7.4 and work well.
For the meaning of servial version id look at
serialVersionUID
if you add it explicitly you can make all change to your class without change uid until you haven't your class serialized and store somewhere:
if you create class A serialize it
end then store it on database
end then change class A making no more compatible with the old class A,
when someone try to load the serialized object from the last class A to the new class A he can have problem.
(对于现在问同样问题的用户)
我正在使用 nb7.4 并发现这个插件从 nb6.5 到 nb7.4
插件
我在 nb7.4 中对其进行了测试并且运行良好。对于服务版本 id 的含义,请查看
serialVersionUID
如果您明确添加它,您可以对您的类进行所有更改而无需更改 uid,直到您的类尚未序列化并存储在某处:如果您创建类 A 序列化它
结束然后将其存储在数据库
端然后更改类 A 使其不再与旧类 A 兼容,
当有人尝试将序列化对象从最后一个类 A 加载到新类 A 时,他可能会遇到问题。
When you change class A and make it incompatible with last class A you have to change uid so if someone try to load it will get InvalidClassExceptions.
当您更改 A 类并使其与上一个 A 类不兼容时,您必须更改 uid,因此如果有人尝试加载它,则会出现 InvalidClassExceptions。

