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:
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
回答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.
回答4:
To disable these warnings in Netbeans :
- Open: Tools -> Options
- Open: Editor -> Hints tab
- Select Language: Java
- Uncheck Standard Javac warnings -> Serialization
- 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.