Give this:
Class<? extends Enum> enumClass = ...; // being passed in from a constructor
Enum e = Enum.valueOf(enumClass, aString); // produces a warning that looks like
[unchecked] unchecked method invocation:
valueOf(java.lang.Class,java.lang.String) in java.lang.Enum is
applied to (java.lang.Class,java.lang.String)
I don’t want to use generics because that’s a major change. I don’t want to supress. I don’t understand why this warning happens. I imagine it is because one cannot extend an Enum type. I get that. But I don’t get why the wildcard class is throwing this weird error. Is there a way to fix this without using @SupressWarning or using generics?
Edit: To Clarify, the following code using generics makes the warning go away.
class Foo<T extends Enum<T>>{
Class<T> enumClass;
Enum e = Enum.valueOf(enumClass, aString);
}
The usage of <T> is what I mean by using generics. I can’t do that because it would be a huge cascading change.
This seems to be a compiler bug – it should be an error, not a warning.
When compiling the method invocation expression
Enum.valueOf(enumClass...), first, capture conversion is applied to the argument types.Then, type inference is done for
Enum.<T>valueOf(enumClass...), the result isT=W.Then, check the bound of
Tafter substitution, i.e. whetherWis subtype ofEnum<W>.(this process is the same for 15.12.2.2 and 15.12.2.3; and 15.12.2.7 definitely yields T=W)
Here, the check should fail. All compiler knows is that
Wis a subtype ofEnum, it cannot deduce thatWis a subtype ofEnum<W>. (Well, we know that it’s true, barringW=Enum; but this knowledge is not present in subtyping rules, so compiler does not use it – we can verify this by playing this example with aMyEnumhierarchy, the compiler will behave the same.)So why does the compiler pass the bound check with just a warning? There is another rule that allows assignment from
RawtoRaw<X>with an unchecked warning. Why this is allowed is another question (it shouldn’t be), but compiler does have a sense thatRawis assignable toRaw<X>. Apparently this rule is mistakenly mixed into the above subtype checking step, compiler thinks that sinceWisEnum, it is somehow also aEnum<W>, the compiler pass the subtype checking with just a warning, in violation of the spec.If such method invocation shouldn’t compile, what is the correct way? I can’t see any – as long as the type of the argument
enumClassis not already in the recursive form ofClass<X extends Enum<X>>, no amount of castings/conversions can make it into that form, therefore there is no way to match the signature ofEnum.valueOfmethod. Maybe javac guys deliberately violated the spec just to make this kind of code compile!