我已经切换到 Java 7 build 21 并开始出现奇怪的编译错误。例如,下面的代码片段无法编译(尽管 IntelliJ 没有显示任何错误):
1 Iterable<?> parts = ImmutableList.<String>of("one", "two");
2 Function<?, String> function = new Function<Object, String>() {
3 @Override
4 public String apply(final Object input) {
5 return input.toString();
6 }
7 };
8 Iterable<String> result = Iterables.transform(parts, function);
9 System.out.println(result);
但如果我在第 2 行替换?
为Object
2 Function<Object, String> function = new Function<Object, String>() {
然后编译成功。
我得到的错误有点神秘:
error: method transform in class Iterables cannot be applied to given types;
required: Iterable<F>,Function<? super F,? extends T>
found: Iterable<CAP#1>,Function<CAP#2,String>
reason: no instance(s) of type variable(s) F,T exist so that argument type Function<CAP#2,String>
conforms to formal parameter type Function<? super F,? extends T>
where F,T are type-variables:
F extends Object declared in method <F,T>transform(Iterable<F>,Function<? super F,? extends T>)
T extends Object declared in method <F,T>transform(Iterable<F>,Function<? super F,? extends T>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends Object from capture of ?
CAP#2 extends Object from capture of ? extends Object
将第 2 行更改为
2 Function<? extends Object, String> function = new Function<Object, String>() {
没有效果。
我正在使用 JDK 1.7.0_11-b21;这曾经用 build 4 编译好。
这是一个javac
错误还是我的?