The javadoc as well as this question both emphasize on
Enum sets are represented internally as bit vectors.
Now I am wondering - what is the behaviour when sending (standard Java serialized) EnumSet objects over the wire to some other JVM that might have a different version of the underlying Enum class?
In other words: when I sent some Set<MyEnum>
it is very well possible that an exception is thrown during de-serialization on the other JVM (in cases where my JVM is using some MyEnum.NEW_GUY
that the other JVM doesn't know about). In that situation, the deserialization tries to instantiate an enum constant that doesn't exist in the class on the other JVM.
But assuming that EnumSet doesn't transport enum constant instances (but just a bit set with some bits true, some false) - what happens when the serialized EnumSet included MyEnum.NEW_GUY
?
I tried to identify a specification that tells me what should happen ( versus assuming that this is an implementation detail ), but no luck so far.
Regarding implementation only (using Oracle Java 8), the answer is: it is not "just" a bitset.
Meaning: one can quickly write a test that serializes an EnumSet instance into a file, then change the enum class, and run code that reads back the set from the file.
Turns out:
java.io.InvalidObjectException: enum constant B does not exist in class com.ibm.hwmca.z.managed.TestEnum
is thrownIn other words: that test suggests is that a serialized EnumSet does contain "more" information than just a plain bitset. The implementation is able to A) detect "missing" enum constants and B) deal with changes to the enum class that only affect the ordering of entries. Matching up with the comments by @Seelenvirtuose that the serialized EnumSet isn't a bit vector, but an array containing actual enum constants (thus being open for the exact same version conflict).
Still searching if there is a specification for the observed behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With