我意识到 Java 8 仍处于测试阶段,但这个让我觉得很奇怪:
public class Fields<C extends Enum<C>> {
public Fields(Set<C> columns) {
// A sample column used to find the universe of the enum of Columns.
C sampleCol = columns.iterator().next();
// Java 8 needs a cast here.
Set<C> allColumns = EnumSet.allOf((/*Class<C>)*/ sampleCol.getClass());
// ... there's more to this that I've deleted.
}
}
错误显示:
error: incompatible types: inferred type does not conform to equality constraint(s)
Set<C> allColumns = EnumSet.allOf(sampleCol.getClass());
inferred: C
equality constraints(s): C,CAP#1
where C is a type-variable:
C extends Enum<C> declared in class Test.Fields
where CAP#1 is a fresh type-variable:
CAP#1 extends Enum from capture of ? extends Enum
这是 Java 8 的错误还是新特性?
最佳答案
有趣,这是raw types 的处理方式的细微变化.
首先,让我们澄清一下您的示例。 Object.getClass
的返回类型很特别:
The actual result type is
Class<? extends |X|>
where|X|
is the erasure of the static type of the expression on whichgetClass
is called.
在这种情况下,X
将是类型参数 C
, 其中 erases至Enum
.所以sampleCol.getClass()
返回 Class<? extends Enum>
. EnumSet.allOf
声明类型参数E extends Enum<E>
, 在你的情况下 ? extends Enum
被推断为其类型参数。
重要的部分是 Enum
是原始类型。已经看到使用原始类型会删除看似无关的泛型,例如在这篇文章中:Why won't this generic java code compile?在他的回答中,Jon Skeet 引用了 JLS §4.8 (“原始类型”)来涵盖这种不直观的行为。
在您使用 Java 7 的示例中似乎发生了类似的行为:EnumSet.allOf(sampleCol.getClass())
允许编译时出现“未经检查的调用”警告(由于将生成的原始 EnumSet
分配给 Set<C>
,随后的“未经检查的转换”警告会隐藏此警告)。
问题变成了:在通用通配符的边界中出现的原始类型是否应该允许未经检查的转换? JLS §4.8 没有提到这一点,所以它是模棱两可的。可能这是一个错误,但这似乎是对这种行为的合理收紧。而像 Enum
这样的标准原始类型遗留 API 本身可能是预期的,一种“半生不熟”的类型,如 Class<? extends Enum>
只能发生在泛型之后,因此让它破坏泛型类型检查是没有意义的。
无论如何,我很想看看是否有人可以指出有关此更改的文档 - 我的搜索没有找到任何东西。
关于您的具体代码:您应该使用 getDeclaringClass()
反而。编译器无法知道调用 getClass
在 C
将准确返回 Class<C>
;事实上,如果在具有特定常量类的枚举上使用它就不会。这正是 Enum
的用例。声明该方法。
关于Java 8 需要强制转换,而 Java 7 不需要 - enum.getClass/getDeclaringClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21640002/