classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cp-patches] [generics] Enum#valueOf implementation


From: Mark Wielaard
Subject: Re: [cp-patches] [generics] Enum#valueOf implementation
Date: Mon, 25 Jul 2005 12:38:03 +0200

On Mon, 2005-07-25 at 00:33 -0600, Tom Tromey wrote:
> >>>>> "Ewout" == Ewout Prangsma <address@hidden> writes:
> 
> Ewout> Here's the unified diff.  Again my question if someone can
> Ewout> commit this, or give me CVS access rights.
> 
> It still needs a ChangeLog entry.
> Also the code is not formatted according to the Classpath style.

Here is an example of a correct ChangeLog entry and the patch formatted
as (I also added a little documentation, please check).

2005-07-25  Ewout Prangsma  <address@hidden>

        * java/lang/Enum.java (valueOf): implemented.

ChangeLog and Coding style is discussed int he hacking guide:
http://www.gnu.org/software/classpath/docs/hacking.html#SEC6
http://www.gnu.org/software/classpath/docs/hacking.html#SEC8

One question. Will the IllegalAccessException really never be thrown? I
can imagine that the Enum value fields are always public or package
private to java.lang in which case this is true (but I haven't studied
Enums at all). Otherwise we might need a PrivilegedAction that calls
setAccessible(true).

Andrew, could you please take a look and tell me whether it is OK to
commit (compiles, don't have ecj or gcjx installed here) and whether or
not it interferes with you current merging work?

Thanks,

Mark
Index: java/lang/Enum.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Attic/Enum.java,v
retrieving revision 1.1.2.4
diff -u -r1.1.2.4 Enum.java
--- java/lang/Enum.java 21 Mar 2005 12:10:17 -0000      1.1.2.4
+++ java/lang/Enum.java 25 Jul 2005 10:37:30 -0000
@@ -1,5 +1,5 @@
 /* Enum.java - Base class for all enums
-   Copyright (C) 2004 Free Software Foundation
+   Copyright (C) 2004, 2005 Free Software Foundation
 
 This file is part of GNU Classpath.
 
@@ -60,11 +60,32 @@
     this.ordinal = ordinal;
   }
 
+  /**
+   * Returns an Enum for a enum class given a description string of
+   * the enum constant.
+   *
+   * @exception NullPointerException when etype or s are null.
+   * @exception IllegalArgumentException when there is no value s in
+   * the enum etype.
+   */
+  @SuppressWarnings("unchecked")
   public static <S extends Enum<S>> Enum valueOf(Class<S> etype, String s)
   {
     if (etype == null || s == null)
       throw new NullPointerException();
-    return null;               // FIXME
+
+    try
+      {
+       return (S) etype.getDeclaredField(s).get(null);
+      }
+    catch (NoSuchFieldException exception)
+      {
+       throw new IllegalArgumentException(s);
+      }
+    catch (IllegalAccessException exception)
+      {
+       throw new Error("Unable to access Enum class");
+      }
   }
 
   public final boolean equals(Object o)

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]