Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse auto-generation of serialVersionUID with each change

Eclipse nicely generates the serialVersionUID for me. But this seems to be passive code generation as the id won't be automatically updated as I change the file unless I do the generation again.

Is there some way to have the serialVersionUID being generated every time I change the contents? The "Save Actions" don't seem to include such an option - has somebody found some way to do this?

It would be nice that it could be combined with the IDE save actions or something similar so that I could revert the change if I were doing that doesn't affect the serialization.

Best regards, Touko

EDIT: @gustafc: There are two main points for this:

  • If I have understood correctly, different compilers may end up with different values for serialVersionUID
    • From Serializable API :However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization
  • I'm using kind of command-pattern like objects doing things at server. So, even though the object content isn't changed, it would be nice to catch the cases when the class content is different at client and server.
    • But on thinking another time, this wuoldn't probably work with auto-generated value since only the content changes would change that? So, actually I'd like to have an auto-incremented serialVersionUID

Does this sound sensible?

Summa summarum, after more thinking, an auto-incremented serialVersionUID incremented at each change would be even better...

like image 250
Touko Avatar asked Sep 23 '09 07:09

Touko


People also ask

How is serialVersionUID calculated?

It is calculated based on the structure of your class - fields, methods, etc. It is specified in the Object Serialization Specification - see this section for the exact format. The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.

What should I set serialVersionUID to?

You should usually have the serialVersionUID as a private static final field in your class; you can choose any value. If you don't specify one, a value is generated automatically (this approach can lead to problems as it is compiler (object structure) dependent.

Where can I find serialVersionUID of a class?

for Android developers, you can also enable serialization inspection from Settings -> Editor -> Inspections -> enable "Serializable class without 'serialVersionUID' check" first, then ALT+ ENTER, the studio will create serialVersionUID for you.


1 Answers

There is no standard functionality in Eclipse to do this.

like image 182
Jesper Avatar answered Oct 06 '22 09:10

Jesper