Enum.valueOf 对扩展 Enum 的未知类型的类抛出警告?

发布于 2024-11-29 04:55:04 字数 776 浏览 4 评论 0 原文

给这个:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬 2024-12-06 04:55:04

EnumClass 都是通用的。因此,如果您不想要任何警告:

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

或者您可以使用泛型方法:

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

但是如果您不使用泛型,则您使用的是原始类型,因此编译器会发出警告 - 除了抑制它们之外没有其他选择。

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.

归属感 2024-12-06 04:55:04

这似乎是一个编译器错误 - 它应该是一个错误,而不是警告。

编译方法调用表达式 Enum.valueOf(enumClass...) 时,首先将捕获转换应用于参数类型。

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

然后,对Enum.valueOf(enumClass...)进行类型推断,结果为T=W

然后,检查替换后T的边界,即W是否是Enum的子类型。

(这个过程对于 15.12.2.2 和 15.12.2.3 是相同的;并且 15.12.2.7 肯定会产生 T=W)

这里,检查应该失败。编译器只知道 WEnum 的子类型,它不能推断出 WEnum< 的子类型/代码>。 (好吧,我们知道这是真的,除非 W=Enum;但是子类型化规则中不存在这种知识,因此编译器不会使用它 - 我们可以通过使用 运行此示例来验证这一点>MyEnum 层次结构,编译器的行为是相同的。)

那么为什么编译器通过绑定检查时只发出警告呢?还有另一个规则允许从 Raw 分配到 Raw 并带有未经检查的警告。为什么允许这样做是另一个问题(不应该如此),但编译器确实感觉到 Raw 可以分配给 Raw。显然这条规则被错误地混入了上面的子类型检查步骤中,编译器认为由于 WEnum,所以它在某种程度上也是一个 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.

<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!

固执像三岁 2024-12-06 04:55:04

如果您考虑 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文