Hi folks. I have the following problem. I made some changes to a class and I have serialised objects created before the changes. I hoped that the changes I made would still be backwards compatable and that I would still be able to deserialise legacy objects.
I made the following changes...
added
private transient int _hashcode = 0;
and
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
_hashcode = calcHashCode();
}
The changes were motivated by some profiling using JIP which indicated that the hashCode method of my class was being called very repetitively outside of my control, and instead I decided to cache the hashcode instead of recalculating it.
Unfortunately however, now I get a
java.io.StreamCorruptedException
when I try to deserialise a Hashtable containing old objects of this class. Now, I know I can hack a fix into the code; however, I would love if someone could point out my stupidity and tell me why the class is no longer backwards compatable. Is the transient element an issue, or am I doing something stupid with the readObject method? The calcHashCode() method simply reinitialises the _hashcode field by calculating it's values from other fields.
Thanks.
B.T.W. I use
public static final long serialVersionUID = 1l;