# HG changeset patch # User darcy # Date 1574209618 28800 # Node ID c4be5e03aff739c07dbb1b3def9118409e465a23 # Parent bd436284147d301cec2c714377f9eb7a04e9d182 8234381: API docs should mention special handling of enums in serialization Reviewed-by: rriggs diff -r bd436284147d -r c4be5e03aff7 src/java.base/share/classes/java/io/Serializable.java --- a/src/java.base/share/classes/java/io/Serializable.java Wed Nov 20 08:12:14 2019 +0800 +++ b/src/java.base/share/classes/java/io/Serializable.java Tue Nov 19 16:26:58 2019 -0800 @@ -134,6 +134,11 @@ * This readResolve method follows the same invocation rules and * accessibility rules as writeReplace.

* + * Enum types are all serializable and receive treatment defined by + * the Java Object Serialization Specification during + * serialization and deserialization. Any declarations of the special + * handling methods discussed above are ignored for enum types.

+ * * The serialization runtime associates with each serializable class a version * number, called a serialVersionUID, which is used during deserialization to * verify that the sender and receiver of a serialized object have loaded @@ -152,8 +157,9 @@ * If a serializable class does not explicitly declare a serialVersionUID, then * the serialization runtime will calculate a default serialVersionUID value * for that class based on various aspects of the class, as described in the - * Java(TM) Object Serialization Specification. However, it is strongly - * recommended that all serializable classes explicitly declare + * Java Object Serialization Specification. This specification defines the + * serialVersionUID of an enum type to be 0L. However, it is strongly + * recommended that all serializable classes other than enum types explicitly declare * serialVersionUID values, since the default serialVersionUID computation is * highly sensitive to class details that may vary depending on compiler * implementations, and can thus result in unexpected diff -r bd436284147d -r c4be5e03aff7 src/java.base/share/classes/java/lang/Enum.java --- a/src/java.base/share/classes/java/lang/Enum.java Wed Nov 20 08:12:14 2019 +0800 +++ b/src/java.base/share/classes/java/lang/Enum.java Tue Nov 19 16:26:58 2019 -0800 @@ -47,6 +47,13 @@ * found in section 8.9 of * The Java™ Language Specification. * + * Enumeration types are all serializable and receive special handling + * by the serialization mechanism. The serialized representation used + * for enum constants cannot be customized. Declarations of methods + * and fields that would otherwise interact with serialization are + * ignored, including {@code serialVersionUID}; see the Java + * Object Serialization Specification for details. + * *

Note that when using an enumeration type as the type of a set * or as the type of the keys in a map, specialized and efficient * {@linkplain java.util.EnumSet set} and {@linkplain