15

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

Touko
  • 11,359
  • 16
  • 75
  • 105
  • Why would you want to do this? The compiler auto-generates the same `serialVersionUID` as Eclipse if you don't specify one. The whole point with specifying a `serialVersionUID` is that it *shouldn't* change unless you make breaking changes (change class hierarchy or remove fields). – gustafc Sep 23 '09 at 08:07

3 Answers3

4

There is no standard functionality in Eclipse to do this.

Jesper
  • 202,709
  • 46
  • 318
  • 350
2

You could update the serial version at build time with the Ant SerialVer task.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Jason Gritman
  • 5,251
  • 4
  • 30
  • 38
0

the id won't be automatically updated as I change the file unless I do the generation again.

AFAIK default serialVersionUID is just this -- an id generated based on current "shape" of a class. So if you want your id generated when you change anything -- just leave it out.

However, if you want to change serialVersionUID only sometimes -- just change it. Random modification to the id will do.

Random number is only little better than consecutive numbers (if you can assume no one else makes up a class with the same qualified name in your environment), so you may even start from serialVersionUID = 1 and increment this number at will.

Piotr Findeisen
  • 19,480
  • 2
  • 52
  • 82
  • "Just leaving it out", however, is dependent on the compiler. If serialization is actually being used, other Java versions/compilers are in use, etc. it's not necessarily a good idea to rely on something you don't have control over. – Dave Newton Jan 02 '13 at 23:24