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

Print this page
rev 815 : 6327048: Enum javadoc could link to JLS
6653154: Exception message for bad Enum.valueOf has spurious "class"
Reviewed-by: emcmanus
   1 /*
   2  * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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  * @author  Josh Bloch
  38  * @author  Neal Gafter
  39  * @see     Class#getEnumConstants()
  40  * @since   1.5
  41  */
  42 public abstract class Enum<E extends Enum<E>>
  43         implements Comparable<E>, Serializable {
  44     /**
  45      * The name of this enum constant, as declared in the enum declaration.
  46      * Most programmers should use the {@link #toString} method rather than
  47      * accessing this field.
  48      */
  49     private final String name;
  50 
  51     /**
  52      * Returns the name of this enum constant, exactly as declared in its
  53      * enum declaration.
  54      *
  55      * <b>Most programmers should use the {@link #toString} method in
  56      * preference to this one, as the toString method may return


 195      * @param enumType the {@code Class} object of the enum type from which
 196      *      to return a constant
 197      * @param name the name of the constant to return
 198      * @return the enum constant of the specified enum type with the
 199      *      specified name
 200      * @throws IllegalArgumentException if the specified enum type has
 201      *         no constant with the specified name, or the specified
 202      *         class object does not represent an enum type
 203      * @throws NullPointerException if {@code enumType} or {@code name}
 204      *         is null
 205      * @since 1.5
 206      */
 207     public static <T extends Enum<T>> T valueOf(Class<T> enumType,
 208                                                 String name) {
 209         T result = enumType.enumConstantDirectory().get(name);
 210         if (result != null)
 211             return result;
 212         if (name == null)
 213             throw new NullPointerException("Name is null");
 214         throw new IllegalArgumentException(
 215             "No enum const " + enumType +"." + name);
 216     }
 217 
 218     /**
 219      * enum classes cannot have finalize methods.
 220      */
 221     protected final void finalize() { }
 222 
 223     /**
 224      * prevent default deserialization
 225      */
 226     private void readObject(ObjectInputStream in) throws IOException,
 227         ClassNotFoundException {
 228             throw new InvalidObjectException("can't deserialize enum");
 229     }
 230 
 231     private void readObjectNoData() throws ObjectStreamException {
 232             throw new InvalidObjectException("can't deserialize enum");
 233     }
 234 }
   1 /*
   2  * Copyright 2003-2009 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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 implicit methods synthesised
  38  * by the compiler, can be found in <i>The Java&trade; Language
  39  * Specification, Third Edition</i>, <a
  40  * href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9">&sect;8.9</a>.
  41  *
  42  * @author  Josh Bloch
  43  * @author  Neal Gafter
  44  * @see     Class#getEnumConstants()
  45  * @since   1.5
  46  */
  47 public abstract class Enum<E extends Enum<E>>
  48         implements Comparable<E>, Serializable {
  49     /**
  50      * The name of this enum constant, as declared in the enum declaration.
  51      * Most programmers should use the {@link #toString} method rather than
  52      * accessing this field.
  53      */
  54     private final String name;
  55 
  56     /**
  57      * Returns the name of this enum constant, exactly as declared in its
  58      * enum declaration.
  59      *
  60      * <b>Most programmers should use the {@link #toString} method in
  61      * preference to this one, as the toString method may return


 200      * @param enumType the {@code Class} object of the enum type from which
 201      *      to return a constant
 202      * @param name the name of the constant to return
 203      * @return the enum constant of the specified enum type with the
 204      *      specified name
 205      * @throws IllegalArgumentException if the specified enum type has
 206      *         no constant with the specified name, or the specified
 207      *         class object does not represent an enum type
 208      * @throws NullPointerException if {@code enumType} or {@code name}
 209      *         is null
 210      * @since 1.5
 211      */
 212     public static <T extends Enum<T>> T valueOf(Class<T> enumType,
 213                                                 String name) {
 214         T result = enumType.enumConstantDirectory().get(name);
 215         if (result != null)
 216             return result;
 217         if (name == null)
 218             throw new NullPointerException("Name is null");
 219         throw new IllegalArgumentException(
 220             "No enum constant " + enumType.getCanonicalName() + "." + name);
 221     }
 222 
 223     /**
 224      * enum classes cannot have finalize methods.
 225      */
 226     protected final void finalize() { }
 227 
 228     /**
 229      * prevent default deserialization
 230      */
 231     private void readObject(ObjectInputStream in) throws IOException,
 232         ClassNotFoundException {
 233         throw new InvalidObjectException("can't deserialize enum");
 234     }
 235 
 236     private void readObjectNoData() throws ObjectStreamException {
 237         throw new InvalidObjectException("can't deserialize enum");
 238     }
 239 }