java - Is there a way to automatically re-generate serialVersionId when class/code signature changes? -
we have whole bunch of serialized classes want database bits invalidated whenever "signature" ie: field structure , serialization code class changes,
is there utility can generate "hash" class file optimally detect when serialization structure java.serializable changes class?
there's no way "optimally detect when serialization structure changes" 1 rather important reason:
serialization breaks encapsulation.
when implement serializable, private , package-private fields , members of class become part of class's exported api. moment class published wild, serialized form (which contains of implementation details) part of contract. changing serialized form have 1 of 2 consequences:
backward compatibility. because
serializablebreaks encapsulation, serialized form becomes part of exported api. when implementation detail changes, @ developer's discretion customizedreadobject(),writeobject()methods can designed continue support original serialized form (even if change result of new implementation). desireable if api far flung , changing serialized form break many clients of api. in case, though serialized form change new implementation, serialversionuid need remain same continue support original serialized form.forced upgrade. if implementation of class changes , impossible or infeasible support original serialized form, changing serialversionuid cause clients of api break, thereby forcing clients upgraded use new serialized form. may desireable in circumstances (but force clients upgrade code).
it worth mentioning if not explicitly declare static final serialversionuid in serializable class, java environment automatically compute 1 applying complex procedure code (that takes account fields , method signatures).
in short, serialversionuid should track serialized form used rather actual class implementation. if want serialversionuid change automatically whenever class implementation changes, can omit explicit declaration of serialversionuid (but may have other negative consequences). decision change serialversionuid needs made explicitly depending on how want api behave when implementation detail changes.
Comments
Post a Comment