Touko
Touko

Reputation: 11779

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:

Does this sound sensible?

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

Upvotes: 15

Views: 9911

Answers (3)

Jason Gritman
Jason Gritman

Reputation: 5301

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

Upvotes: 2

Piotr Findeisen
Piotr Findeisen

Reputation: 20710

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.

Upvotes: 0

Jesper
Jesper

Reputation: 206786

There is no standard functionality in Eclipse to do this.

Upvotes: 4

Related Questions