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

社会主义新天地 提交于 2020-01-14 08:14:10

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/1373215/can-netbeans-generate-an-automatic-serial-version-id-for-a-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!