< prev index next >

src/java.base/share/classes/java/util/EnumSet.java

Print this page
8192935: Fix EnumSet's SerializationProxy javadoc
Reviewed-by: smarks, rriggs

*** 73,98 **** * Java Collections Framework</a>. * * @author Josh Bloch * @since 1.5 * @see EnumMap - * @serial exclude */ @SuppressWarnings("serial") // No serialVersionUID due to usage of // serial proxy pattern public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, java.io.Serializable { /** * The class of all the elements of this set. */ ! final Class<E> elementType; /** ! * All of the values comprising T. (Cached for performance.) */ ! final Enum<?>[] universe; EnumSet(Class<E>elementType, Enum<?>[] universe) { this.elementType = elementType; this.universe = universe; } --- 73,97 ---- * Java Collections Framework</a>. * * @author Josh Bloch * @since 1.5 * @see EnumMap */ @SuppressWarnings("serial") // No serialVersionUID due to usage of // serial proxy pattern public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, java.io.Serializable { /** * The class of all the elements of this set. */ ! final transient Class<E> elementType; /** ! * All of the values comprising E. (Cached for performance.) */ ! final transient Enum<?>[] universe; EnumSet(Class<E>elementType, Enum<?>[] universe) { this.elementType = elementType; this.universe = universe; }
*** 414,424 **** * to ensure that the existence of a particular implementation type is * an implementation detail. * * @serial include */ ! private static class SerializationProxy <E extends Enum<E>> implements java.io.Serializable { private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0]; --- 413,423 ---- * to ensure that the existence of a particular implementation type is * an implementation detail. * * @serial include */ ! private static class SerializationProxy<E extends Enum<E>> implements java.io.Serializable { private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
*** 439,467 **** SerializationProxy(EnumSet<E> set) { elementType = set.elementType; elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY); } ! // instead of cast to E, we should perhaps use elementType.cast() ! // to avoid injection of forged stream, but it will slow the implementation @SuppressWarnings("unchecked") private Object readResolve() { EnumSet<E> result = EnumSet.noneOf(elementType); for (Enum<?> e : elements) result.add((E)e); return result; } private static final long serialVersionUID = 362491234563181265L; } Object writeReplace() { return new SerializationProxy<>(this); } ! // readObject method for the serialization proxy pattern ! // See Effective Java, Second Ed., Item 78. ! private void readObject(java.io.ObjectInputStream stream) throws java.io.InvalidObjectException { throw new java.io.InvalidObjectException("Proxy required"); } } --- 438,485 ---- SerializationProxy(EnumSet<E> set) { elementType = set.elementType; elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY); } ! /** ! * Returns an {@code EnumSet} object with initial state ! * held by this proxy. ! * ! * @return a {@code EnumSet} object with initial state ! * held by this proxy ! */ @SuppressWarnings("unchecked") private Object readResolve() { + // instead of cast to E, we should perhaps use elementType.cast() + // to avoid injection of forged stream, but it will slow the + // implementation EnumSet<E> result = EnumSet.noneOf(elementType); for (Enum<?> e : elements) result.add((E)e); return result; } private static final long serialVersionUID = 362491234563181265L; } + /** + * Returns a + * <a href="../../serialized-form.html#java.util.EnumSet.SerializationProxy"> + * SerializationProxy</a> + * representing the state of this instance. + * + * @return a {@link SerializationProxy} + * representing the state of this instance + */ Object writeReplace() { return new SerializationProxy<>(this); } ! /** ! * @param s the stream ! * @throws java.io.InvalidObjectException always ! */ ! private void readObject(java.io.ObjectInputStream s) throws java.io.InvalidObjectException { throw new java.io.InvalidObjectException("Proxy required"); } }
< prev index next >