OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / java / lang / Enum.java
index 5344d5c..da2e40b 100644 (file)
@@ -48,10 +48,8 @@ import java.lang.reflect.Field;
  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  * @since 1.5
  */
-/* FIXME[GENERICS]: Should be Enum<T extends Enum<T>>
-   and Comparable<T> */
-public abstract class Enum
-  implements Comparable, Serializable
+public abstract class Enum<T extends Enum<T>>
+  implements Comparable<T>, Serializable
 {
 
   /**
@@ -62,13 +60,13 @@ public abstract class Enum
   /**
    * The name of this enum constant.
    */
-  String name;
+  final String name;
 
   /**
    * The number of this enum constant.  Each constant is given a number
    * which matches the order in which it was declared, starting with zero.
    */
-  int ordinal;
+  final int ordinal;
 
   /**
    * This constructor is used by the compiler to create enumeration constants.
@@ -91,19 +89,22 @@ public abstract class Enum
    * @exception IllegalArgumentException when there is no value s in
    * the enum etype.
    */
-  /* FIXME[GENERICS]: Should be <S extends Enum<S>> S valueOf(Class<S>) */
-  public static Enum valueOf(Class etype, String s)
+  public static <S extends Enum<S>> S valueOf(Class<S> etype, String s)
   {
     if (etype == null || s == null)
       throw new NullPointerException();
 
     try
       {
+        // XXX We should not use getDeclaredField, because it does
+        // an unnecessary security check.
         Field f = etype.getDeclaredField(s);
         if (! f.isEnumConstant())
           throw new IllegalArgumentException(s);
-       /* FIXME[GENERICS]: Should cast to S */
-        return (Enum) f.get(null); 
+        Class.setAccessible(f);
+       @SuppressWarnings("unchecked")
+         S val = (S) f.get(null);
+        return val;
       }
     catch (NoSuchFieldException exception)
       {
@@ -167,7 +168,7 @@ public abstract class Enum
    * @throws ClassCastException if <code>e</code> is not an enumeration
    *                            constant of the same class.
    */ 
-  public final int compareTo(Enum e)
+  public final int compareTo(T e)
   {
     if (getDeclaringClass() != e.getDeclaringClass())
       throw new ClassCastException();
@@ -175,27 +176,6 @@ public abstract class Enum
   }
 
   /**
-   * Returns an integer which represents the relative ordering of this
-   * enumeration constant.  Enumeration constants are ordered by their
-   * ordinals, which represents their declaration order.  So, comparing
-   * two identical constants yields zero, while one declared prior to
-   * this returns a positive integer and one declared after yields a
-   * negative integer.
-   *
-   * @param o the enumeration constant to compare.
-   * @return a negative integer if <code>e.ordinal < this.ordinal</code>,
-   *         zero if <code>e.ordinal == this.ordinal</code> and a positive
-   *         integer if <code>e.ordinal > this.ordinal</code>.
-   * @throws ClassCastException if <code>e</code> is not an enumeration
-   *                            constant of the same class.
-   */ 
-  /* FIXME[GENERICS]: Remove this method */
-  public final int compareTo(Object o)
-  {
-    return compareTo((Enum)o);
-  }
-
-  /**
    * Cloning of enumeration constants is prevented, to maintain their
    * singleton status.
    *
@@ -235,8 +215,7 @@ public abstract class Enum
    *
    * @return the type of this enumeration constant.
    */
-  /* FIXME[GENERICS]: Should return Class<T> */
-  public final Class getDeclaringClass()
+  public final Class<T> getDeclaringClass()
   {
     Class k = getClass();
     // We might be in an anonymous subclass of the enum class, so go
@@ -245,4 +224,14 @@ public abstract class Enum
       k = k.getSuperclass();
     return k;
   }
+
+  /**
+   * Enumerations can not have finalization methods.
+   *
+   * @since 1.6
+   */
+  protected final void finalize()
+  {
+  }
+
 }