< prev index next >

src/java.base/share/classes/java/lang/Enum.java

Print this page
rev 52786 : 8210031: implementation for JVM Constants API


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.Serializable;
  29 import java.io.IOException;
  30 import java.io.InvalidObjectException;
  31 import java.io.ObjectInputStream;
  32 import java.io.ObjectStreamException;









  33 
  34 /**
  35  * This is the common base class of all Java language enumeration types.
  36  *
  37  * More information about enums, including descriptions of the
  38  * implicitly declared methods synthesized by the compiler, can be
  39  * found in section 8.9 of
  40  * <cite>The Java&trade; Language Specification</cite>.
  41  *
  42  * <p> Note that when using an enumeration type as the type of a set
  43  * or as the type of the keys in a map, specialized and efficient
  44  * {@linkplain java.util.EnumSet set} and {@linkplain
  45  * java.util.EnumMap map} implementations are available.
  46  *
  47  * @param <E> The enum type subclass
  48  * @serial exclude
  49  * @author  Josh Bloch
  50  * @author  Neal Gafter
  51  * @see     Class#getEnumConstants()
  52  * @see     java.util.EnumSet
  53  * @see     java.util.EnumMap
  54  * @since   1.5
  55  */
  56 @SuppressWarnings("serial") // No serialVersionUID needed due to
  57                             // special-casing of enum types.
  58 public abstract class Enum<E extends Enum<E>>
  59         implements Comparable<E>, Serializable {
  60     /**
  61      * The name of this enum constant, as declared in the enum declaration.
  62      * Most programmers should use the {@link #toString} method rather than
  63      * accessing this field.
  64      */
  65     private final String name;
  66 
  67     /**
  68      * Returns the name of this enum constant, exactly as declared in its
  69      * enum declaration.
  70      *
  71      * <b>Most programmers should use the {@link #toString} method in
  72      * preference to this one, as the toString method may return
  73      * a more user-friendly name.</b>  This method is designed primarily for
  74      * use in specialized situations where correctness depends on getting the
  75      * exact name, which will not vary from release to release.
  76      *
  77      * @return the name of this enum constant
  78      */
  79     public final String name() {


 187     /**
 188      * Returns the Class object corresponding to this enum constant's
 189      * enum type.  Two enum constants e1 and  e2 are of the
 190      * same enum type if and only if
 191      *   e1.getDeclaringClass() == e2.getDeclaringClass().
 192      * (The value returned by this method may differ from the one returned
 193      * by the {@link Object#getClass} method for enum constants with
 194      * constant-specific class bodies.)
 195      *
 196      * @return the Class object corresponding to this enum constant's
 197      *     enum type
 198      */
 199     @SuppressWarnings("unchecked")
 200     public final Class<E> getDeclaringClass() {
 201         Class<?> clazz = getClass();
 202         Class<?> zuper = clazz.getSuperclass();
 203         return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
 204     }
 205 
 206     /**















 207      * Returns the enum constant of the specified enum type with the
 208      * specified name.  The name must match exactly an identifier used
 209      * to declare an enum constant in this type.  (Extraneous whitespace
 210      * characters are not permitted.)
 211      *
 212      * <p>Note that for a particular enum type {@code T}, the
 213      * implicitly declared {@code public static T valueOf(String)}
 214      * method on that enum may be used instead of this method to map
 215      * from a name to the corresponding enum constant.  All the
 216      * constants of an enum type can be obtained by calling the
 217      * implicit {@code public static T[] values()} method of that
 218      * type.
 219      *
 220      * @param <T> The enum type whose constant is to be returned
 221      * @param enumType the {@code Class} object of the enum type from which
 222      *      to return a constant
 223      * @param name the name of the constant to return
 224      * @return the enum constant of the specified enum type with the
 225      *      specified name
 226      * @throws IllegalArgumentException if the specified enum type has


 240         throw new IllegalArgumentException(
 241             "No enum constant " + enumType.getCanonicalName() + "." + name);
 242     }
 243 
 244     /**
 245      * enum classes cannot have finalize methods.
 246      */
 247     @SuppressWarnings("deprecation")
 248     protected final void finalize() { }
 249 
 250     /**
 251      * prevent default deserialization
 252      */
 253     private void readObject(ObjectInputStream in) throws IOException,
 254         ClassNotFoundException {
 255         throw new InvalidObjectException("can't deserialize enum");
 256     }
 257 
 258     private void readObjectNoData() throws ObjectStreamException {
 259         throw new InvalidObjectException("can't deserialize enum");




















































 260     }
 261 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 

  28 import java.io.IOException;
  29 import java.io.InvalidObjectException;
  30 import java.io.ObjectInputStream;
  31 import java.io.ObjectStreamException;
  32 import java.io.Serializable;
  33 import java.lang.constant.ClassDesc;
  34 import java.lang.constant.Constable;
  35 import java.lang.constant.ConstantDescs;
  36 import java.lang.constant.DynamicConstantDesc;
  37 import java.lang.invoke.MethodHandles;
  38 import java.util.Optional;
  39 
  40 import static java.util.Objects.requireNonNull;
  41 
  42 /**
  43  * This is the common base class of all Java language enumeration types.
  44  *
  45  * More information about enums, including descriptions of the
  46  * implicitly declared methods synthesized by the compiler, can be
  47  * found in section 8.9 of
  48  * <cite>The Java&trade; Language Specification</cite>.
  49  *
  50  * <p> Note that when using an enumeration type as the type of a set
  51  * or as the type of the keys in a map, specialized and efficient
  52  * {@linkplain java.util.EnumSet set} and {@linkplain
  53  * java.util.EnumMap map} implementations are available.
  54  *
  55  * @param <E> The enum type subclass
  56  * @serial exclude
  57  * @author  Josh Bloch
  58  * @author  Neal Gafter
  59  * @see     Class#getEnumConstants()
  60  * @see     java.util.EnumSet
  61  * @see     java.util.EnumMap
  62  * @since   1.5
  63  */
  64 @SuppressWarnings("serial") // No serialVersionUID needed due to
  65                             // special-casing of enum types.
  66 public abstract class Enum<E extends Enum<E>>
  67         implements Constable, Comparable<E>, Serializable {
  68     /**
  69      * The name of this enum constant, as declared in the enum declaration.
  70      * Most programmers should use the {@link #toString} method rather than
  71      * accessing this field.
  72      */
  73     private final String name;
  74 
  75     /**
  76      * Returns the name of this enum constant, exactly as declared in its
  77      * enum declaration.
  78      *
  79      * <b>Most programmers should use the {@link #toString} method in
  80      * preference to this one, as the toString method may return
  81      * a more user-friendly name.</b>  This method is designed primarily for
  82      * use in specialized situations where correctness depends on getting the
  83      * exact name, which will not vary from release to release.
  84      *
  85      * @return the name of this enum constant
  86      */
  87     public final String name() {


 195     /**
 196      * Returns the Class object corresponding to this enum constant's
 197      * enum type.  Two enum constants e1 and  e2 are of the
 198      * same enum type if and only if
 199      *   e1.getDeclaringClass() == e2.getDeclaringClass().
 200      * (The value returned by this method may differ from the one returned
 201      * by the {@link Object#getClass} method for enum constants with
 202      * constant-specific class bodies.)
 203      *
 204      * @return the Class object corresponding to this enum constant's
 205      *     enum type
 206      */
 207     @SuppressWarnings("unchecked")
 208     public final Class<E> getDeclaringClass() {
 209         Class<?> clazz = getClass();
 210         Class<?> zuper = clazz.getSuperclass();
 211         return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
 212     }
 213 
 214     /**
 215      * Return an enum descriptor {@code EnumDesc} for this instance, if one can be
 216      * constructed, or an empty {@link Optional} if one cannot be.
 217      *
 218      * @return An {@link Optional} containing the resulting nominal descriptor,
 219      * or an empty {@link Optional} if one cannot be constructed.
 220      * @since 12
 221      */
 222     @Override
 223     public final Optional<EnumDesc<E>> describeConstable() {
 224         return getDeclaringClass()
 225                 .describeConstable()
 226                 .map(c -> EnumDesc.of(c, name));
 227     }
 228 
 229     /**
 230      * Returns the enum constant of the specified enum type with the
 231      * specified name.  The name must match exactly an identifier used
 232      * to declare an enum constant in this type.  (Extraneous whitespace
 233      * characters are not permitted.)
 234      *
 235      * <p>Note that for a particular enum type {@code T}, the
 236      * implicitly declared {@code public static T valueOf(String)}
 237      * method on that enum may be used instead of this method to map
 238      * from a name to the corresponding enum constant.  All the
 239      * constants of an enum type can be obtained by calling the
 240      * implicit {@code public static T[] values()} method of that
 241      * type.
 242      *
 243      * @param <T> The enum type whose constant is to be returned
 244      * @param enumType the {@code Class} object of the enum type from which
 245      *      to return a constant
 246      * @param name the name of the constant to return
 247      * @return the enum constant of the specified enum type with the
 248      *      specified name
 249      * @throws IllegalArgumentException if the specified enum type has


 263         throw new IllegalArgumentException(
 264             "No enum constant " + enumType.getCanonicalName() + "." + name);
 265     }
 266 
 267     /**
 268      * enum classes cannot have finalize methods.
 269      */
 270     @SuppressWarnings("deprecation")
 271     protected final void finalize() { }
 272 
 273     /**
 274      * prevent default deserialization
 275      */
 276     private void readObject(ObjectInputStream in) throws IOException,
 277         ClassNotFoundException {
 278         throw new InvalidObjectException("can't deserialize enum");
 279     }
 280 
 281     private void readObjectNoData() throws ObjectStreamException {
 282         throw new InvalidObjectException("can't deserialize enum");
 283     }
 284 
 285     /**
 286      * A <a href="package-summary.html#nominal">nominal descriptor</a> for an
 287      * {@code enum} constant.
 288      *
 289      * @param <E> the type of the enum constant
 290      *
 291      * @since 12
 292      */
 293     public static final class EnumDesc<E extends Enum<E>>
 294             extends DynamicConstantDesc<E> {
 295 
 296         /**
 297          * Construct a nominal descriptor for the specified {@code enum} class and name.
 298          *
 299          * @param constantType a {@link ClassDesc} describing the {@code enum} class
 300          * @param constantName the unqualified name of the enum constant
 301          * @throws NullPointerException if any argument is null
 302          * @jvms 4.2.2 Unqualified Names
 303          */
 304         private EnumDesc(ClassDesc constantType, String constantName) {
 305             super(ConstantDescs.BSM_ENUM_CONSTANT, requireNonNull(constantName), requireNonNull(constantType));
 306         }
 307 
 308         /**
 309          * Return a nominal descriptor for the specified {@code enum} class and name
 310          *
 311          * @param <E> the type of the enum constant
 312          * @param enumClass a {@link ClassDesc} describing the {@code enum} class
 313          * @param constantName the unqualified name of the enum constant
 314          * @return the nominal descriptor
 315          * @throws NullPointerException if any argument is null
 316          * @jvms 4.2.2 Unqualified Names
 317          * @since 12
 318          */
 319         public static<E extends Enum<E>> EnumDesc<E> of(ClassDesc enumClass,
 320                                                         String constantName) {
 321             return new EnumDesc<>(enumClass, constantName);
 322         }
 323 
 324         @Override
 325         @SuppressWarnings("unchecked")
 326         public E resolveConstantDesc(MethodHandles.Lookup lookup)
 327                 throws ReflectiveOperationException {
 328             return Enum.valueOf((Class<E>) constantType().resolveConstantDesc(lookup), constantName());
 329         }
 330 
 331         @Override
 332         public String toString() {
 333             return String.format("EnumDesc[%s.%s]", constantType().displayName(), constantName());
 334         }
 335     }
 336 }
< prev index next >