Closed
Description
The following code sample works with OpenJDK 8 but does not compile with OpenJDK 11:
public class SimpleTest {
@Test
public void test() {
List<String> list = new ArrayList<>();
assertThat(list, anyOf(hasItem("a"), hasItem("b")));
}
}
Compiling with Java 11 fails with
[javac] method Assert.<T#1>assertThat(String,T#1,Matcher<? super T#1>) is not applicable
[javac] (cannot infer type-variable(s) T#1
[javac] (actual and formal argument lists differ in length))
[javac] method Assert.<T#2>assertThat(T#2,Matcher<? super T#2>) is not applicable
[javac] (inference variable T#3 has incompatible bounds
[javac] lower bounds: String,Object
[javac] lower bounds: String,Object)
[javac] where T#1,T#2,T#3 are type-variables:
[javac] T#1 extends Object declared in method <T#1>assertThat(String,T#1,Matcher<? super T#1>)
[javac] T#2 extends Object declared in method <T#2>assertThat(T#2,Matcher<? super T#2>)
[javac] T#3 extends Object declared in method <T#3>hasItem(T#3)
If the first argument of the Matchers.anyOf methods would be changed from Matcher to Matcher<? super T> (like in Matchers.allOf), it would work.
Metadata
Metadata
Assignees
Labels
No labels
Activity
berolinux commentedon May 5, 2019
Even after applying #257, building hamcrest with OpenJDK 12.0.1 fails with
phoenix384 commentedon May 7, 2019
The intention was to make it possible to use anyOf from Java 11 code and not to make Hamcrest compilable with Java 11.
Moreover these errors do not relate to the code I changed.
Either way, my PR is an improvement, since now the method signature is the same as in allOf.
Use "<? super T>" for all arguments of anyOf
vaukai commentedon Jul 5, 2022
Gentoo has a patch making it compile even with
jdk 17
.