JDK-6209030 : Too strict signature of java.lang.Enum.valueOf
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2004-12-15
  • Updated: 2017-05-19
  • Resolved: 2005-06-07
Related Reports
Relates :  
Description
This program doesn't compile:

class Test {
    Enum<?> test(Class<? extends Enum<?>> enumClass, String value) {
	return Enum.valueOf(enumClass, value);
    }
}

unless we change the signature if valueOf to:

<T extends Enum<?>> T valueOf(Class<T> enumType, String name);

###@###.### 2004-12-15 17:08:13 GMT

Comments
WORK AROUND class Test { Enum<?> test(Class<? extends Enum> enumClass, String value) { return Enum.valueOf(enumClass, value); } } ###@###.### 2005-04-27 17:49:55 GMT
27-04-2005

EVALUATION After some investigation, it proves unfeasible to change the signature of Enum.valueOf. The obvious candidate is: <T extends Enum<?>> T valueOf(Class<T> c, String s); However, this would have the undesirable side effect of allowing programs like this: enum MyEnum { FOO{}, BAR } Enum.valueOf(MyEnum.FOO.getClass(), "FOO"); Which would cause an IllegalArgumentException. This can be addressed by changing the specification to allow the valueOf method to find the enum type using getSuperclass if passed a subclass of an enum type. However, this reveals a different problem, the proposed signature is not correct: MyEnum$1 bar = Enum.valueOf(MyEnum$1.class, "BAR"); This would return MyEnum.BAR which is not a subtype of MyEnum$1 (the anonymous subclass of MyEnum used to implement the class of FOO). This signature is more correct: <T extends Enum<T>> T valueOf(Class<? extends T> c, String s); However, this doesn't solve the problem as Enum<?> is not a valid type argument for this method. Given these problems and an acceptable workaround exists, we have decided not to change Enum.valueOf. ###@###.### 2005-06-07 02:19:53 GMT
07-01-2005