bug-classpath
[Top][All Lists]
Advanced

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

[Bug classpath/25189] New: Problems with java.lang.Enum (mainly valueOf


From: subanark at gmail dot com
Subject: [Bug classpath/25189] New: Problems with java.lang.Enum (mainly valueOf method)
Date: 30 Nov 2005 19:36:02 -0000

The valueOf method in Enum assumes that all of the enum's fields are in fact
the enum constant values. Enums can have both instance and static fields. A
check though the values method defined by the compiler in each enum class
should be used (may want to cache it though).
Assuming the enum was created using a complient java compiler, when Sun's
implemention succeedes, so does classpath's and with the same result, but in
the case where a failure should occur, classpath's version may succeed as shown
below, and might even return a different enum type (thus violating the generic
contract on the return type), or throw an unexpected
exception(ClassCastException, when the field is not type enum or
NullPointerException, when the field is not static).

Test case:
public enum Test
{
 A,B,C;
 public static Test D = C;
 public static void main(String[] args)
 {
    System.out.println(valueOf(Test.class,"D"));
 }
}
classpath's result:
C
Sun's result:
java.lang.IllegalArgumentException: No enum const class Test.D

Minor (Probably not needed to be fixed):
Sun actually does do a check in compareTo to enusure the passed in enum value
is the same enum class. (This check should only fail if an incorrect generic
cast was made).


-- 
           Summary: Problems with java.lang.Enum (mainly valueOf method)
           Product: classpath
           Version: 0.19
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: classpath
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: subanark at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25189





reply via email to

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