I've got a Serializable
class that has been in use in production since about 2004. However, the guy who wrote it forgot to generate a serialVersionUID
.
The whole thing sort of works (there is a big comment warning to not touch anything in the class), but while working in a related different class I've had a InvalidCastException
, so I want to add serialVersionUID
to be sure that the class works properly.
The question is, is it possible to add such a field, without existing serialized instances breaking? There are a lot of them, and there is no easy way to modify them.
My idea came from seeing the exception message, which is something along the lines of:
java.io.InvalidClassException: the.problematic.Class; local class incompatible: stream classdesc serialVersionUID = -8802277085918151566, local class serialVersionUID = -3137213695071887162"
I've thought that I could use the implicit serial (stream classdesc in the trace) as the serial, and it should retain backwards compatibility. Is this correct? I'm (more or less) sure that all the serialized instances that we've got across the system are of the same version.
Yes, specifying the exact same value for serialVersionUID
as the computed on is should prevent backwards compatibility (Eclipse for example has an option to do that, called "Add generated serial version ID").