Can NetBeans generate an automatic serial version

2019-06-17 06:05发布

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?

5条回答
三岁会撩人
2楼-- · 2019-06-17 06:34

To disable these warnings in Netbeans :

  1. Open: Tools -> Options
  2. Open: Editor -> Hints tab
  3. Select Language: Java
  4. Uncheck Standard Javac warnings -> Serialization
  5. OK

I also had to clear ~/.netbeans/6.8/var/cache and restart Netbeans to clear the warnings from the Tasks list.

查看更多
聊天终结者
3楼-- · 2019-06-17 06:37

(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.

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.

查看更多
甜甜的少女心
4楼-- · 2019-06-17 06:45

It seems like out of the box NetBeans doesn't support the feature, but plug-ins do.

Please have a look at

link 1, link 2 and link 3

查看更多
贼婆χ
5楼-- · 2019-06-17 06:48

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.

查看更多
Lonely孤独者°
6楼-- · 2019-06-17 06:54

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.

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).

查看更多
登录 后发表回答