The Java language has greatly benefited from adding enumerations to it; but, unfortunately, they do not work well when sending serialized objects between systems with different levels of code.
Example: suppose you have two systems A and B. Both of them start at the same code levels, but at some point they begin to see code updates at different points in time. Now suppose that there is some
public enum Whatever { FIRST; }
And there are other objects that contain references to the constants of this enumeration. These objects are serialized and sent from A to B or vice versa. Now consider that B has a newer version of Whatever
public enum Whatever { FIRST; SECOND }
Then:
class SomethingElse implements Serializable { ... private final Whatever theWhatever; SomethingElse(Whatever theWhatever) { this.theWhatever = theWhatever; ..
gets an instance ...
SomethingElse somethin = new SomethingElse(Whatever.SECOND)
and then serialized and sent to A (for example, as a result of some RMI call). This is bad, because now there will be an error during deserialization on A: A knows any enum class, but in a version that does not have SECOND.
We found this a difficult way; and now I really want to use enumerations for situations that are actually "perfect for enumerations"; simply because I know that I cannot easily extend an existing enumeration later.
Now I'm wondering: are there any (good) strategies to avoid such enumeration compatibility issues? Or do I really need to go back to the "pre-enum" time; and not use enumerations, but should rely on a solution where I use simple strings everywhere?
Update: note that using serialversionuid does not help at all. This thing only helps you make incompatible changes “more obvious”. But the fact is, I don't care why deserialization fails - because I have to avoid it. And I am also not able to change the way we serialize our objects. We do RMI; and we serialize for binary; I have no way to change this.
java enums serialization
Ghostcat
source share