< prev index next >

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

Print this page




 258     public static <T extends Enum<T>> T valueOf(Class<T> enumType,
 259                                                 String name) {
 260         T result = enumType.enumConstantDirectory().get(name);
 261         if (result != null)
 262             return result;
 263         if (name == null)
 264             throw new NullPointerException("Name is null");
 265         throw new IllegalArgumentException(
 266             "No enum constant " + enumType.getCanonicalName() + "." + name);
 267     }
 268 
 269     /**
 270      * enum classes cannot have finalize methods.
 271      */
 272     @SuppressWarnings("deprecation")
 273     protected final void finalize() { }
 274 
 275     /**
 276      * prevent default deserialization
 277      */

 278     private void readObject(ObjectInputStream in) throws IOException,
 279         ClassNotFoundException {
 280         throw new InvalidObjectException("can't deserialize enum");
 281     }
 282 

 283     private void readObjectNoData() throws ObjectStreamException {
 284         throw new InvalidObjectException("can't deserialize enum");
 285     }
 286 
 287     /**
 288      * A <a href="{@docRoot}/java.base/java/lang/constant/package-summary.html#nominal">nominal descriptor</a> for an
 289      * {@code enum} constant.
 290      *
 291      * @param <E> the type of the enum constant
 292      *
 293      * @since 12
 294      */
 295     public static final class EnumDesc<E extends Enum<E>>
 296             extends DynamicConstantDesc<E> {
 297 
 298         /**
 299          * Constructs a nominal descriptor for the specified {@code enum} class and name.
 300          *
 301          * @param constantType a {@link ClassDesc} describing the {@code enum} class
 302          * @param constantName the unqualified name of the enum constant




 258     public static <T extends Enum<T>> T valueOf(Class<T> enumType,
 259                                                 String name) {
 260         T result = enumType.enumConstantDirectory().get(name);
 261         if (result != null)
 262             return result;
 263         if (name == null)
 264             throw new NullPointerException("Name is null");
 265         throw new IllegalArgumentException(
 266             "No enum constant " + enumType.getCanonicalName() + "." + name);
 267     }
 268 
 269     /**
 270      * enum classes cannot have finalize methods.
 271      */
 272     @SuppressWarnings("deprecation")
 273     protected final void finalize() { }
 274 
 275     /**
 276      * prevent default deserialization
 277      */
 278     @java.io.Serial
 279     private void readObject(ObjectInputStream in) throws IOException,
 280         ClassNotFoundException {
 281         throw new InvalidObjectException("can't deserialize enum");
 282     }
 283 
 284     @java.io.Serial
 285     private void readObjectNoData() throws ObjectStreamException {
 286         throw new InvalidObjectException("can't deserialize enum");
 287     }
 288 
 289     /**
 290      * A <a href="{@docRoot}/java.base/java/lang/constant/package-summary.html#nominal">nominal descriptor</a> for an
 291      * {@code enum} constant.
 292      *
 293      * @param <E> the type of the enum constant
 294      *
 295      * @since 12
 296      */
 297     public static final class EnumDesc<E extends Enum<E>>
 298             extends DynamicConstantDesc<E> {
 299 
 300         /**
 301          * Constructs a nominal descriptor for the specified {@code enum} class and name.
 302          *
 303          * @param constantType a {@link ClassDesc} describing the {@code enum} class
 304          * @param constantName the unqualified name of the enum constant


< prev index next >