【发布时间】:2011-12-28 19:26:40
【问题描述】:
如果我们采用以下类,它们在 JDK 6 下编译良好:
public final class Foo<V> {
private final V value;
private Foo(final V value) {
this.value = value;
}
public static <T, R extends T> Foo<T> of(final R value) {
return new Foo<T>(value);
}
}
final class Tester {
@Test(groups="unit")
public static void test() {
bar(Foo.of(BigDecimal.ZERO)); // This line fails in JDK 7 but not JDK 6
}
private static void bar(final Foo<? extends Number> target) {
assert target != null;
}
}
但是,在 JDK 7 下,我收到以下错误:
[ERROR] \work\fsb-core\src\test\java\com\fsb\core\Foo.java:[42,8] error:
method bar in class Tester cannot be applied to given types;
我认为 JDK 7 中的类型推断限制较少(例如,添加构造函数推断)。但是,在这里,编译器拒绝在 JDK 6 下有效的类型。
这是一个错误吗?还是对方法的推理规则更加严格?
【问题讨论】:
-
Java 8 (1.8.0_25) 再次编译就好了。
标签: java generics type-inference java-7