src/java.base/share/classes/java/lang/Enum.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55210
e079a4cfad75 8225314: broken links in java.base
jjg
parents: 52914
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4043
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4043
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4043
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4043
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4043
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectStreamException;
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    32
import java.io.Serializable;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    33
import java.lang.constant.ClassDesc;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    34
import java.lang.constant.Constable;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    35
import java.lang.constant.ConstantDescs;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    36
import java.lang.constant.DynamicConstantDesc;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    37
import java.lang.invoke.MethodHandles;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    38
import java.util.Optional;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    39
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    40
import static java.util.Objects.requireNonNull;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This is the common base class of all Java language enumeration types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
1948
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
    45
 * More information about enums, including descriptions of the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
    46
 * implicitly declared methods synthesized by the compiler, can be
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 5506
diff changeset
    47
 * found in section 8.9 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 5506
diff changeset
    48
 * <cite>The Java&trade; Language Specification</cite>.
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
    49
 *
4043
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    50
 * <p> Note that when using an enumeration type as the type of a set
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    51
 * or as the type of the keys in a map, specialized and efficient
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    52
 * {@linkplain java.util.EnumSet set} and {@linkplain
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    53
 * java.util.EnumMap map} implementations are available.
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    54
 *
1948
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
    55
 * @param <E> The enum type subclass
51413
43e41800d579 5075463: (enum) Serialized Form javadoc for java.lang.Enum is misleading
darcy
parents: 47216
diff changeset
    56
 * @serial exclude
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see     Class#getEnumConstants()
4043
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    60
 * @see     java.util.EnumSet
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    61
 * @see     java.util.EnumMap
55237
36334808644d 8225353: Add @jls links to java.lang.Enum
darcy
parents: 55210
diff changeset
    62
 * @jls 8.9 Enum Types
36334808644d 8225353: Add @jls links to java.lang.Enum
darcy
parents: 55210
diff changeset
    63
 * @jls 8.9.3 Enum Members
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
22109
94e298e5a08b 8031210: Remove serial warning from java.lang.Enum
darcy
parents: 14342
diff changeset
    66
@SuppressWarnings("serial") // No serialVersionUID needed due to
94e298e5a08b 8031210: Remove serial warning from java.lang.Enum
darcy
parents: 14342
diff changeset
    67
                            // special-casing of enum types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public abstract class Enum<E extends Enum<E>>
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
    69
        implements Constable, Comparable<E>, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The name of this enum constant, as declared in the enum declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Most programmers should use the {@link #toString} method rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * accessing this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns the name of this enum constant, exactly as declared in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * enum declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <b>Most programmers should use the {@link #toString} method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * preference to this one, as the toString method may return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * a more user-friendly name.</b>  This method is designed primarily for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * use in specialized situations where correctness depends on getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * exact name, which will not vary from release to release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return the name of this enum constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public final String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * in the enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Most programmers will have no use for this field.  It is designed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * for use by sophisticated enum-based data structures, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final int ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns the ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * in its enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Most programmers will have no use for this method.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * designed for use by sophisticated enum-based data structures, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return the ordinal of this enumeration constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public final int ordinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Sole constructor.  Programmers cannot invoke this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * It is for use by code emitted by the compiler in response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * enum type declarations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param name - The name of this enum constant, which is the identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *               used to declare it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param ordinal - The ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *         in the enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *         an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected Enum(String name, int ordinal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.ordinal = ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns the name of this enum constant, as contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * declaration.  This method may be overridden, though it typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * isn't necessary or desirable.  An enum type should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * method when a more "programmer-friendly" string form exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return the name of this enum constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns true if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param other the object to be compared for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return  true if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public final boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return this==other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns a hash code for this enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return a hash code for this enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public final int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return super.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Throws CloneNotSupportedException.  This guarantees that enums
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * are never cloned, which is necessary to preserve their "singleton"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return (never returns)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected final Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Compares this enum with the specified object for order.  Returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * negative integer, zero, or a positive integer as this object is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * than, equal to, or greater than the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Enum constants are only comparable to other enum constants of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * same enum type.  The natural order implemented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * method is the order in which the constants are declared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public final int compareTo(E o) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   189
        Enum<?> other = (Enum<?>)o;
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   190
        Enum<E> self = this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (self.getClass() != other.getClass() && // optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            self.getDeclaringClass() != other.getDeclaringClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return self.ordinal - other.ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns the Class object corresponding to this enum constant's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * enum type.  Two enum constants e1 and  e2 are of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * same enum type if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *   e1.getDeclaringClass() == e2.getDeclaringClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * (The value returned by this method may differ from the one returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * by the {@link Object#getClass} method for enum constants with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * constant-specific class bodies.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return the Class object corresponding to this enum constant's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *     enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   209
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public final Class<E> getDeclaringClass() {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   211
        Class<?> clazz = getClass();
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   212
        Class<?> zuper = clazz.getSuperclass();
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   213
        return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   217
     * Returns an enum descriptor {@code EnumDesc} for this instance, if one can be
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   218
     * constructed, or an empty {@link Optional} if one cannot be.
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   219
     *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   220
     * @return An {@link Optional} containing the resulting nominal descriptor,
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   221
     * or an empty {@link Optional} if one cannot be constructed.
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   222
     * @since 12
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   223
     */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   224
    @Override
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   225
    public final Optional<EnumDesc<E>> describeConstable() {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   226
        return getDeclaringClass()
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   227
                .describeConstable()
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   228
                .map(c -> EnumDesc.of(c, name));
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   229
    }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   230
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   231
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Returns the enum constant of the specified enum type with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * specified name.  The name must match exactly an identifier used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * to declare an enum constant in this type.  (Extraneous whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * characters are not permitted.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
1948
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   237
     * <p>Note that for a particular enum type {@code T}, the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   238
     * implicitly declared {@code public static T valueOf(String)}
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   239
     * method on that enum may be used instead of this method to map
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   240
     * from a name to the corresponding enum constant.  All the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   241
     * constants of an enum type can be obtained by calling the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   242
     * implicit {@code public static T[] values()} method of that
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   243
     * type.
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   244
     *
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   245
     * @param <T> The enum type whose constant is to be returned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param enumType the {@code Class} object of the enum type from which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *      to return a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param name the name of the constant to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return the enum constant of the specified enum type with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *      specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @throws IllegalArgumentException if the specified enum type has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         no constant with the specified name, or the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *         class object does not represent an enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws NullPointerException if {@code enumType} or {@code name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *         is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                                String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        T result = enumType.enumConstantDirectory().get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new NullPointerException("Name is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        throw new IllegalArgumentException(
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
   266
            "No enum constant " + enumType.getCanonicalName() + "." + name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * enum classes cannot have finalize methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 25859
diff changeset
   272
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected final void finalize() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * prevent default deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55237
diff changeset
   278
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        ClassNotFoundException {
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
   281
        throw new InvalidObjectException("can't deserialize enum");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55237
diff changeset
   284
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private void readObjectNoData() throws ObjectStreamException {
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
   286
        throw new InvalidObjectException("can't deserialize enum");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   288
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   289
    /**
55210
e079a4cfad75 8225314: broken links in java.base
jjg
parents: 52914
diff changeset
   290
     * A <a href="{@docRoot}/java.base/java/lang/constant/package-summary.html#nominal">nominal descriptor</a> for an
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   291
     * {@code enum} constant.
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   292
     *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   293
     * @param <E> the type of the enum constant
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   294
     *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   295
     * @since 12
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   296
     */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   297
    public static final class EnumDesc<E extends Enum<E>>
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   298
            extends DynamicConstantDesc<E> {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   299
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   300
        /**
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   301
         * Constructs a nominal descriptor for the specified {@code enum} class and name.
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   302
         *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   303
         * @param constantType a {@link ClassDesc} describing the {@code enum} class
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   304
         * @param constantName the unqualified name of the enum constant
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   305
         * @throws NullPointerException if any argument is null
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   306
         * @jvms 4.2.2 Unqualified Names
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   307
         */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   308
        private EnumDesc(ClassDesc constantType, String constantName) {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   309
            super(ConstantDescs.BSM_ENUM_CONSTANT, requireNonNull(constantName), requireNonNull(constantType));
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   310
        }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   311
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   312
        /**
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   313
         * Returns a nominal descriptor for the specified {@code enum} class and name
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   314
         *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   315
         * @param <E> the type of the enum constant
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   316
         * @param enumClass a {@link ClassDesc} describing the {@code enum} class
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   317
         * @param constantName the unqualified name of the enum constant
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   318
         * @return the nominal descriptor
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   319
         * @throws NullPointerException if any argument is null
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   320
         * @jvms 4.2.2 Unqualified Names
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   321
         * @since 12
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   322
         */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   323
        public static<E extends Enum<E>> EnumDesc<E> of(ClassDesc enumClass,
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   324
                                                        String constantName) {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   325
            return new EnumDesc<>(enumClass, constantName);
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   326
        }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   327
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   328
        @Override
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   329
        @SuppressWarnings("unchecked")
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   330
        public E resolveConstantDesc(MethodHandles.Lookup lookup)
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   331
                throws ReflectiveOperationException {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   332
            return Enum.valueOf((Class<E>) constantType().resolveConstantDesc(lookup), constantName());
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   333
        }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   334
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   335
        @Override
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   336
        public String toString() {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   337
            return String.format("EnumDesc[%s.%s]", constantType().displayName(), constantName());
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   338
        }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 51413
diff changeset
   339
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}