给这个:
Class<? extends Enum> enumClass = ...; // being passed in from a constructor
Enum e = Enum.valueOf(enumClass, aString); // produces a warning that looks like
[unchecked] 未检查的方法调用:
java.lang.Enum 中的 valueOf(java.lang.Class,java.lang.String) 是
应用于 (java.lang.Class,java.lang.String)
我不想使用泛型,因为这是一个重大变化。我不想压抑。我不明白为什么会出现这个警告。我想这是因为无法扩展枚举类型。我明白了。但我不明白为什么通配符类会抛出这个奇怪的错误。有没有办法在不使用 @SupressWarning
或使用泛型的情况下解决这个问题?
编辑:为了澄清,以下使用泛型的代码使警告消失。
class Foo<T extends Enum<T>>{
Class<T> enumClass;
Enum e = Enum.valueOf(enumClass, aString);
}
的用法就是我所说的使用泛型。我不能这样做,因为这将是一个巨大的级联变化。
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.
发布评论
评论(3)
Enum
和Class
都是通用的。因此,如果您不想要任何警告:或者您可以使用泛型方法:
但是如果您不使用泛型,则您使用的是原始类型,因此编译器会发出警告 - 除了抑制它们之外没有其他选择。
Both
Enum
andClass
are generic. So if you don't want any warnings:Or you can have a generic method:
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.
这似乎是一个编译器错误 - 它应该是一个错误,而不是警告。
编译方法调用表达式
Enum.valueOf(enumClass...)
时,首先将捕获转换应用于参数类型。然后,对
Enum.valueOf(enumClass...)
进行类型推断,结果为T=W
。然后,检查替换后
T
的边界,即W
是否是Enum
的子类型。(这个过程对于 15.12.2.2 和 15.12.2.3 是相同的;并且 15.12.2.7 肯定会产生 T=W)
这里,检查应该失败。编译器只知道
W
是Enum
的子类型,它不能推断出W
是Enum< 的子类型/代码>。 (好吧,我们知道这是真的,除非
W=Enum
;但是子类型化规则中不存在这种知识,因此编译器不会使用它 - 我们可以通过使用运行此示例来验证这一点>MyEnum
层次结构,编译器的行为是相同的。)那么为什么编译器通过绑定检查时只发出警告呢?还有另一个规则允许从
Raw
分配到Raw
并带有未经检查的警告。为什么允许这样做是另一个问题(不应该如此),但编译器确实感觉到Raw
可以分配给Raw
。显然这条规则被错误地混入了上面的子类型检查步骤中,编译器认为由于W
是Enum
,所以它在某种程度上也是一个Enum
>,编译器仅通过警告就通过了子类型检查,这违反了规范。如果这样的方法调用不应该编译,正确的方法是什么?我看不到任何 - 只要参数
enumClass
的类型尚未采用Class>
的递归形式,就没有大量的转换/转换可以使其成为这种形式,因此无法匹配Enum.valueOf
方法的签名。也许javac的家伙故意违反规范只是为了让这种代码编译!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
T
after substitution, i.e. whetherW
is 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
W
is a subtype ofEnum
, it cannot deduce thatW
is 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 aMyEnum
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
toRaw<X>
with an unchecked warning. Why this is allowed is another question (it shouldn't be), but compiler does have a sense thatRaw
is assignable toRaw<X>
. Apparently this rule is mistakenly mixed into the above subtype checking step, compiler thinks that sinceW
isEnum
, 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
enumClass
is 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.valueOf
method. Maybe javac guys deliberately violated the spec just to make this kind of code compile!如果您考虑 valueOf 内部会发生什么,您就会意识到您的代码不可能按编写的那样工作。 Enum.valueOf 需要一个实际枚举类的实例作为参数;然后,它只是迭代该类的
values()
来查找匹配项。由于类型擦除,泛型在您的代码中不起作用。没有实际类型被传递到
Enum.valueOf
中。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
.