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

前端 未结 3 492
野性不改
野性不改 2021-01-07 06:33

Give this:

Class enumClass = ...; // being passed in from a constructor
Enum e = Enum.valueOf(enumClass, aString); // produces a warnin         


        
相关标签:
3条回答
  • 2021-01-07 06:55

    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 讨论(0)
  • 2021-01-07 06:56

    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!

    0 讨论(0)
  • 2021-01-07 07:05

    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.

    0 讨论(0)
提交回复
热议问题