< prev index next >

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

Print this page




   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  *


 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          * Returns 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 }


   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 jdk.internal.lang.annotation.Foldable;
  29 
  30 import java.io.IOException;
  31 import java.io.InvalidObjectException;
  32 import java.io.ObjectInputStream;
  33 import java.io.ObjectStreamException;
  34 import java.io.Serializable;
  35 import java.lang.constant.ClassDesc;
  36 import java.lang.constant.Constable;
  37 import java.lang.constant.ConstantDesc;
  38 import java.lang.constant.ConstantDescs;
  39 import java.lang.constant.DynamicConstantDesc;
  40 import java.lang.invoke.MethodHandles;
  41 import java.util.Optional;
  42 
  43 import static java.util.Objects.requireNonNull;
  44 
  45 /**
  46  * This is the common base class of all Java language enumeration types.
  47  *
  48  * More information about enums, including descriptions of the
  49  * implicitly declared methods synthesized by the compiler, can be
  50  * found in section 8.9 of
  51  * <cite>The Java&trade; Language Specification</cite>.
  52  *
  53  * <p> Note that when using an enumeration type as the type of a set
  54  * or as the type of the keys in a map, specialized and efficient
  55  * {@linkplain java.util.EnumSet set} and {@linkplain
  56  * java.util.EnumMap map} implementations are available.
  57  *


 302          * @param constantType a {@link ClassDesc} describing the {@code enum} class
 303          * @param constantName the unqualified name of the enum constant
 304          * @throws NullPointerException if any argument is null
 305          * @jvms 4.2.2 Unqualified Names
 306          */
 307         private EnumDesc(ClassDesc constantType, String constantName) {
 308             super(ConstantDescs.BSM_ENUM_CONSTANT, requireNonNull(constantName), requireNonNull(constantType));
 309         }
 310 
 311         /**
 312          * Returns a nominal descriptor for the specified {@code enum} class and name
 313          *
 314          * @param <E> the type of the enum constant
 315          * @param enumClass a {@link ClassDesc} describing the {@code enum} class
 316          * @param constantName the unqualified name of the enum constant
 317          * @return the nominal descriptor
 318          * @throws NullPointerException if any argument is null
 319          * @jvms 4.2.2 Unqualified Names
 320          * @since 12
 321          */
 322         @Foldable
 323         public static<E extends Enum<E>> EnumDesc<E> of(ClassDesc enumClass,
 324                                                         String constantName) {
 325             return new EnumDesc<>(enumClass, constantName);
 326         }
 327 
 328         @Override
 329         @SuppressWarnings("unchecked")
 330         public E resolveConstantDesc(MethodHandles.Lookup lookup)
 331                 throws ReflectiveOperationException {
 332             return Enum.valueOf((Class<E>) constantType().resolveConstantDesc(lookup), constantName());
 333         }
 334 
 335         @Override
 336         public String toString() {
 337             return String.format("EnumDesc[%s.%s]", constantType().displayName(), constantName());
 338         }
 339 
 340         @Override
 341         public Optional<? extends ConstantDesc> describeConstable() {
 342             return Optional.empty();
 343         }
 344     }
 345 }
< prev index next >