Prototype javac 2.0: enum.valueOf(String) does not work as specified
May 31, 2003 7:56 AM
Hi!
According to the enum draft spec at http://jcp.org/aboutJava/communityprocess/jsr/tiger/enum.html each enum type should have a static valueOf(String) method which returns the enum element specified by the String argument. Subsequently the following code should compile just fine:
class EnumTest
{
privatevoid foo()
{
bar(SomeEnum.valueOf("A"));
}
privatevoid bar(SomeEnum arg)
{
}
}
enum SomeEnum
{
A
};
The new prototype compiler reports an error. Is this an intended spec change or a bug?
Hans
--
Hans Kratz
Omnicore Software
www.omnicore.com
Oddly enough, javap (actually, my SinjP tool) run on your SomeEnum class reports:
$ javap SomeEnum
Compiled from "SomeEnum.java"
class SomeEnum extends java.lang.Enum<SomeEnum>
{
public static final java.util.List<SomeEnum> VALUES;
public static final SomeEnum A;
public final java.util.List<SomeEnum> family();
public static java.util.List<SomeEnum> valueOf(java.lang.String);
void <init>();
public int compareTo(java.lang.Enum);
public int compareTo(java.lang.Object);
static void <clinit>();
}
$
i.e. the return type of valueOf is a List not an instance of SomeEnum. That explains why your code doesn't compile, but why was this change made?
Re: Prototype javac 2.0: enum.valueOf(String) does not work as specified
May 31, 2003 9:09 AM
(reply 2
of 3) (In reply to
#1 )
OK, here's the bytecode for the valueOf() method:
Codeview "bytecode" for public static java.util.List SomeEnum.valueOf(java.lang.String):
#2/SomeEnum.java:1 - getstatic public static final java.util.List SomeEnum.VALUES
#3/SomeEnum.java:1 - aload_0 lv_0
#4/SomeEnum.java:1 - invokestatic protected static java.lang.Enum java.lang.Enum.valueOf(java.util.List,java.lang.String)
#5/SomeEnum.java:1 - areturn
It looks like the code is correct (returns an Enum, not a list); it's only the method signature which is wrong. This is clearly a bug; I'll report it to jsr14-prototype-comments@sun.com and post it on
http://cag.lcs.mit.edu/~cananian/Projects/GJ/#bugs