What are the characteristics of EnumSets regarding class versioning?

javadoc as well as question both underline

Enum sets are represented internally as bit vectors.

Now I'm wondering - what is the behavior when sending (standard Java serialization) EnumSet objects on wiring to another JVM, which may have a different version of the Enum base class?

In other words: when I posted some Set<MyEnum>, it is possible that an exception is thrown during de-serialization to another JVM (in cases where my JVM uses some MyEnum.NEW_GUYthat the other JVM doesn’t know). In this situation, deserialization attempts to create an enum constant that does not exist in the class on another JVM.

But assuming that EnumSet does not carry constant instances of enum (but only a bit with some bits true, some false) - what happens when a serialized EnumSet turns on MyEnum.NEW_GUY?

I tried to define a specification that tells me what should happen (as opposed to assuming that this is an implementation detail), but so far no luck.

+4
source share
1 answer

For implementation only (using Oracle Java 8), the answer is this: it's not just a “bit”.

Meaning: You can quickly write a test that serializes an EnumSet instance into a file, then change the enum class and run code that will read the set from the file.

Turns off:

  • If the file contains an enum constant that no longer exists, is java.io.InvalidObjectException: enum constant B does not exist in class com.ibm.hwmca.z.managed.TestEnumthrown
  • , - ​​, , - .

: , EnumSet "" , . A) "" enum B) enum, . @Seelenvirtuose , EnumSet , , enum ( , ).

, .

+3

Source: https://habr.com/ru/post/1693414/


All Articles