以下简单类(repo重现它):
import static org.hamcrest.*;
import static org.junit.Assert.assertThat;
import java.util.*;
import org.junit.Test;
public class TestGenerics {
@Test
public void thisShouldCompile() {
List<String> myList = Arrays.asList("a", "b", "c");
assertThat("List doesn't contain unexpected elements", myList, not(anyOf(hasItem("d"), hasItem("e"), hasItem("f"))));
}
}
行为取决于JDK版本:
出现以下错误:
[ERROR] /tmp/jdk-issue-generics/src/test/java/org/alostale/issues/generics/TestGenerics.java:[17,17] no suitable method found for assertThat(java.lang.String,java.util.List<java.lang.String>,org.hamcrest.Matcher<java.lang.Iterable<? super java.lang.Object>>)
method org.junit.Assert.<T>assertThat(java.lang.String,T,org.hamcrest.Matcher<? super T>) is not applicable
(inference variable T has incompatible bounds
upper bounds: java.lang.String,java.lang.Object
lower bounds: capture#1 of ? super T?,capture#2 of ? super java.lang.Object,capture#3 of ? super java.lang.Object,java.lang.Object,java.lang.String,capture#4 of ? super T?)
method org.junit.Assert.<T>assertThat(T,org.hamcrest.Matcher<? super T>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
这是JDK 9的某些预期更改还是一个错误?
我可以通过这种方式将匹配器提取到类型变量中,这将起作用:
Matcher<Iterable<? super String>> m1 = hasItem("d");
Matcher<Iterable<? super String>> m2 = hasItem("e");
Matcher<Iterable<? super String>> m3 = hasItem("f");
assertThat(myList, not(anyOf(m1, m2, m3)));
但是问题仍然是:javac
<= 8是否能够推断类型,但不能在9+中推断出来?
答案 0 :(得分:11)
经过一些研究,我相信我们可以将其排除在Junit或hamcrest问题之外。实际上,这似乎是一个JDK错误。以下代码将无法在JDK> 8中编译:
AnyOf<Iterable<? super String>> matcher = CoreMatchers.anyOf(
CoreMatchers.hasItem("d"), CoreMatchers.hasItem("e"), CoreMatchers.hasItem("f"));
Error:(23, 63) java: incompatible types: inference variable T has incompatible bounds equality constraints: java.lang.String lower bounds: java.lang.Object,java.lang.String
将其转换为不使用任何库的MCVE:
class Test {
class A<S> { } class B<S> { } class C<S> { } class D { }
<T> A<B<? super T>> foo() { return null; }
<U> C<U> bar(A<U> a1, A<? super U> a2) { return null; }
C<B<? super D>> c = bar(foo(), foo());
}
使用bar
中的单个变量可以达到类似的效果,这导致上限相等约束而不是下限:
class Test {
class A<S> { } class B<S> { } class C<S> { } class D { }
<T> A<B<? super T>> foo() { return null; }
<U> C<U> bar(A<? super U> a) { return null; }
C<B<? super D>> c = bar(foo());
}
Error:(21, 28) java: incompatible types: inference variable U has incompatible bounds equality constraints: com.Test.B<? super com.Test.D> upper bounds: com.Test.B<? super capture#1 of ? super com.Test.D>,java.lang.Object
当JDK试图合理化? super U
时,它似乎找不到要使用的适当通配符类。更有趣的是,如果您完全指定foo
的类型,则编译器实际上将成功。 这对于MCVE和原始帖子都适用:
// This causes compile to succeed even though an IDE will call it redundant
C<B<? super D>> c = bar(this.<D>foo(), this.<D>foo());
就像您介绍的情况一样,将执行分为多行将产生正确的结果:
A<B<? super D>> a1 = foo();
A<B<? super D>> a2 = foo();
C<B<? super D>> c = bar(a1, a2);
因为有多种方法可以编写功能上等效的代码,并且鉴于其中只有一部分可以编译,所以我的结论是,这不是JDK的预期行为。在通配符的评估中,某个绑定super
的地方存在一个错误。
我的建议是针对JDK 8编译现有代码,对于要求JDK> 8的较新代码完全指定通用值。
答案 1 :(得分:1)
我创建了另一个MCVE,显示了类型推断的差异:
import java.util.Arrays;
import java.util.List;
public class Example {
public class Matcher<T> {
private T t;
public Matcher(T t) {
this.t = t;
}
}
public <N> Matcher<N> anyOf(Matcher<N> first, Matcher<? super N> second) {
return first;
}
public <T> Matcher<List<? super T>> hasItem1(T item) {
return new Matcher<>(Arrays.asList(item));
}
public <T> Matcher<List<? super T>> hasItem2(T item) {
return new Matcher<>(Arrays.asList(item));
}
public void thisShouldCompile() {
Matcher x = (Matcher<List<? super String>>) anyOf(hasItem1("d"), hasItem2("e"));
}
}
JDK8编译通过,JDK10给出:
Example.java:27: error: incompatible types: Example.Matcher<List<? super Object>> cannot be converted to Example.Matcher<List<? super String>>
Matcher x = (Matcher<List<? super String>>) anyOf(hasItem1("d"), hasItem2("e"));
因此JDK10似乎有一个错误,可以解决N
至List<? super String>
的问题
Matcher<N> anyOf(Matcher<N> first, Matcher<? super N> second)
打电话时
anyOf(Matcher<List<? super String>>, Matcher<List<? super String>>)
我建议将此问题报告给OpenJDK(在此处链接问题),并可能将问题报告给hamcrest项目。