Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is are the characteristics of EnumSets regarding class versioning?

Tags:

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.

like image 314
GhostCat Avatar asked Feb 09 '18 12:02

GhostCat


1 Answers

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:

  • if the file contains an enum constant that doesn't exist any more, a java.io.InvalidObjectException: enum constant B does not exist in class com.ibm.hwmca.z.managed.TestEnum is thrown
  • if all enum constants exist, but their order is changed - the set that gets read back is still correct.

In 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.

like image 52
GhostCat Avatar answered Sep 22 '22 13:09

GhostCat