简体   繁体   English

NetBeans是否可以为Java类生成自动串行版本ID?

[英]Can NetBeans generate an automatic serial version ID for a Java class?

I would like to remove some warnings for some classes by generating an automatic serial version ID. 我想通过生成自动序列版本ID来删除某些类的一些警告。 In Eclipse, this is trivial to do - the IDE can generate one automatically and add it to the class. 在Eclipse中,这很简单 - IDE可以自动生成一个并将其添加到类中。 However, I don't see this functionality in NetBeans. 但是,我在NetBeans中看不到此功能。 Is it available? 是可用的么? If so, where is it? 如果是的话,它在哪里? If not, is there a plugin that can enable it? 如果没有,是否有可以启用它的插件?

Actually my solution to that "problem" was to deactivate that warning in my project configuration (I use Eclipse but I guess for NetBeans is the same) as IMHO it's a wrong warning: not having a 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 you to 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). 所以,如果你不关心这个,这是更好地避免价值(这种方式是只对版本是肯定的兼容兼容,但也有一些误报:它认为这是不兼容的,但实际上它会),而不是把那里的固定值(可能,在我的情况下)忘记在需要时更新,导致实际的有效性错误(假阴性:它认为它是兼容的,但事实并非如此)。

It seems like out of the box NetBeans doesn't support the feature, but plug-ins do. 似乎开箱即用NetBeans不支持该功能,但插件可以。

Please have a look at 请看看

link 1 , link 2 and link 3 链接1链接2链接3

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 serialVersionUID to be unique across classes. 请注意,绝对不要求serialVersionUID在类之间是唯一的。

To disable these warnings in Netbeans : 要在Netbeans中禁用这些警告:

  1. Open: Tools -> Options 打开:工具 - >选项
  2. Open: Editor -> Hints tab 打开:编辑器 - >提示选项卡
  3. Select Language: Java 选择语言:Java
  4. Uncheck Standard Javac warnings -> Serialization 取消选中标准Javac警告 - >序列化
  5. OK

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以清除Tasks列表中的警告。

(For user that where ask for same question now) (对于现在要求相同问题的用户)
I 'm using nb7.4 and found this plugin from nb6.5 to nb7.4 我正在使用nb7.4并发现这个插件从nb6.5到nb7.4
plugin 插入
I tested it in nb7.4 and work well. 我在nb7.4中进行了测试并且运行良好。 For the meaning of servial version id look at 有关servial版本id的含义请查看
serialVersionUID 的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 如果你明确地添加它,你可以在不更改uid的情况下对你的类进行所有更改,直到你没有将类序列化并存储在某处:如果你创建了类A序列化它
end then store it on database 然后将其存储在数据库中
end then change class A making no more compatible with the old class A, 然后改变A级,使其与旧的A级不相容,
when someone try to load the serialized object from the last class A to the new class A he can have problem. 当有人试图将序列化对象从最后一个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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM