Lambda's work OK in AOT mode. Defender methods work OK. Merging.
2 * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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).
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.
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
28 import java.io.Serializable;
29 import java.io.IOException;
30 import org.apidesign.bck2brwsr.core.JavaScriptBody;
33 * This is the common base class of all Java language enumeration types.
35 * More information about enums, including descriptions of the
36 * implicitly declared methods synthesized by the compiler, can be
37 * found in section 8.9 of
38 * <cite>The Java™ Language Specification</cite>.
40 * <p> Note that when using an enumeration type as the type of a set
41 * or as the type of the keys in a map, specialized and efficient
42 * {@linkplain java.util.EnumSet set} and {@linkplain
43 * java.util.EnumMap map} implementations are available.
45 * @param <E> The enum type subclass
48 * @see Class#getEnumConstants()
49 * @see java.util.EnumSet
50 * @see java.util.EnumMap
53 public abstract class Enum<E extends Enum<E>>
54 implements Comparable<E>, Serializable {
56 * The name of this enum constant, as declared in the enum declaration.
57 * Most programmers should use the {@link #toString} method rather than
58 * accessing this field.
60 private final String name;
63 * Returns the name of this enum constant, exactly as declared in its
66 * <b>Most programmers should use the {@link #toString} method in
67 * preference to this one, as the toString method may return
68 * a more user-friendly name.</b> This method is designed primarily for
69 * use in specialized situations where correctness depends on getting the
70 * exact name, which will not vary from release to release.
72 * @return the name of this enum constant
74 public final String name() {
79 * The ordinal of this enumeration constant (its position
80 * in the enum declaration, where the initial constant is assigned
81 * an ordinal of zero).
83 * Most programmers will have no use for this field. It is designed
84 * for use by sophisticated enum-based data structures, such as
85 * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
87 private final int ordinal;
90 * Returns the ordinal of this enumeration constant (its position
91 * in its enum declaration, where the initial constant is assigned
92 * an ordinal of zero).
94 * Most programmers will have no use for this method. It is
95 * designed for use by sophisticated enum-based data structures, such
96 * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
98 * @return the ordinal of this enumeration constant
100 public final int ordinal() {
105 * Sole constructor. Programmers cannot invoke this constructor.
106 * It is for use by code emitted by the compiler in response to
107 * enum type declarations.
109 * @param name - The name of this enum constant, which is the identifier
110 * used to declare it.
111 * @param ordinal - The ordinal of this enumeration constant (its position
112 * in the enum declaration, where the initial constant is assigned
113 * an ordinal of zero).
115 protected Enum(String name, int ordinal) {
117 this.ordinal = ordinal;
121 * Returns the name of this enum constant, as contained in the
122 * declaration. This method may be overridden, though it typically
123 * isn't necessary or desirable. An enum type should override this
124 * method when a more "programmer-friendly" string form exists.
126 * @return the name of this enum constant
128 public String toString() {
133 * Returns true if the specified object is equal to this
136 * @param other the object to be compared for equality with this object.
137 * @return true if the specified object is equal to this
140 public final boolean equals(Object other) {
145 * Returns a hash code for this enum constant.
147 * @return a hash code for this enum constant.
149 public final int hashCode() {
150 return super.hashCode();
154 * Throws CloneNotSupportedException. This guarantees that enums
155 * are never cloned, which is necessary to preserve their "singleton"
158 * @return (never returns)
160 protected final Object clone() throws CloneNotSupportedException {
161 throw new CloneNotSupportedException();
165 * Compares this enum with the specified object for order. Returns a
166 * negative integer, zero, or a positive integer as this object is less
167 * than, equal to, or greater than the specified object.
169 * Enum constants are only comparable to other enum constants of the
170 * same enum type. The natural order implemented by this
171 * method is the order in which the constants are declared.
173 public final int compareTo(E o) {
174 Enum other = (Enum)o;
176 if (self.getClass() != other.getClass() && // optimization
177 self.getDeclaringClass() != other.getDeclaringClass())
178 throw new ClassCastException();
179 return self.ordinal - other.ordinal;
183 * Returns the Class object corresponding to this enum constant's
184 * enum type. Two enum constants e1 and e2 are of the
185 * same enum type if and only if
186 * e1.getDeclaringClass() == e2.getDeclaringClass().
187 * (The value returned by this method may differ from the one returned
188 * by the {@link Object#getClass} method for enum constants with
189 * constant-specific class bodies.)
191 * @return the Class object corresponding to this enum constant's
194 public final Class<E> getDeclaringClass() {
195 Class clazz = getClass();
196 Class zuper = clazz.getSuperclass();
197 return (zuper == Enum.class) ? clazz : zuper;
201 * Returns the enum constant of the specified enum type with the
202 * specified name. The name must match exactly an identifier used
203 * to declare an enum constant in this type. (Extraneous whitespace
204 * characters are not permitted.)
206 * <p>Note that for a particular enum type {@code T}, the
207 * implicitly declared {@code public static T valueOf(String)}
208 * method on that enum may be used instead of this method to map
209 * from a name to the corresponding enum constant. All the
210 * constants of an enum type can be obtained by calling the
211 * implicit {@code public static T[] values()} method of that
214 * @param <T> The enum type whose constant is to be returned
215 * @param enumType the {@code Class} object of the enum type from which
216 * to return a constant
217 * @param name the name of the constant to return
218 * @return the enum constant of the specified enum type with the
220 * @throws IllegalArgumentException if the specified enum type has
221 * no constant with the specified name, or the specified
222 * class object does not represent an enum type
223 * @throws NullPointerException if {@code enumType} or {@code name}
227 public static <T extends Enum<T>> T valueOf(Class<T> enumType,
229 for (Object o : values(enumType)) {
230 T t = enumType.cast(o);
231 if (name.equals(((Enum)t).name)) {
235 throw new IllegalArgumentException();
238 @JavaScriptBody(args = { "enumType" }, body = "return enumType.cnstr.prototype['_$VALUES']();")
239 private static native Object[] values(Class<?> enumType);
242 * enum classes cannot have finalize methods.
244 protected final void finalize() { }
247 * prevent default deserialization
249 // private void readObject(ObjectInputStream in) throws IOException,
250 // ClassNotFoundException {
251 // throw new InvalidObjectException("can't deserialize enum");
254 // private void readObjectNoData() throws ObjectStreamException {
255 // throw new InvalidObjectException("can't deserialize enum");