开发者

Enum.valueOf throws a warning for unknown type of class that extends Enum?

开发者 https://www.devze.com 2023-03-28 05:55 出处:网络
Give this: Class<? extends Enum> enumClass = ...; // being passed in from a constructor Enum e = Enum.valueOf(enumClass, aString); // produces a warning that looks like

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>开发者_C百科 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.


Both Enum and Class are generic. So if you don't want any warnings:

class Foo<T extends Enum<T>>{
    Class<T> enumClass;
    T e = Enum.valueOf(enumClass, str);
}

Or you can have a generic method:

public <T extends Enum<T>> T getEnumValue(Class<T> clazz, String name) {
    T e = Enum.valueOf(clazz, name);
    return e;
}

But if you don't use generics, you are using raw types, and therefore the compiler issues warnings - there is no option apart from suppressing them.


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.

<W extends Enum> // a new type parameter 
Class<W> enumClass; // the type of the argument after capture conversion

Then, type inference is done for Enum.<T>valueOf(enumClass...), the result is T=W.

Then, check the bound of T after substitution, i.e. whether W is subtype of Enum<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 W is a subtype of Enum, it cannot deduce that W is a subtype of Enum<W>. (Well, we know that it's true, barring W=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 a MyEnum hierarchy, 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 Raw to Raw<X> with an unchecked warning. Why this is allowed is another question (it shouldn't be), but compiler does have a sense that Raw is assignable to Raw<X>. Apparently this rule is mistakenly mixed into the above subtype checking step, compiler thinks that since W is Enum, it is somehow also a Enum<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 enumClass is not already in the recursive form of Class<X extends Enum<X>>, no amount of castings/conversions can make it into that form, therefore there is no way to match the signature of Enum.valueOf method. Maybe javac guys deliberately violated the spec just to make this kind of code compile!


If you think about what has to happen inside valueOf, you'll realize that your code cannot possibly work as written. Enum.valueOf needs as an argument an instance of an actual enum class; it then simply iterates through the values() for that class looking for a match.

Because of type erasure, generics won't work in your code. No actual type is being passed into Enum.valueOf.

0

精彩评论

暂无评论...
验证码 换一张
取 消