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

前端 未结 3 624
野性不改
野性不改 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: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.

     // a new type parameter 
    Class enumClass; // the type of the argument after capture conversion
    

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

    Then, check the bound of T after substitution, i.e. whether W is subtype of Enum.

    (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. (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 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. 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, 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>, 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!

提交回复
热议问题