泛型行为在JDK 8和9中有所不同

alo*_*ale 33 java generics java-8 java-9 java-10

以下简单类(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"))));
  }
}
Run Code Online (Sandbox Code Playgroud)

行为取决于JDK版本:

  • 在JDK <= 8中正确编译(用7和8测试)
  • 使用JDK 9+编译失败(使用9,10和11 EA测试)

出现以下错误:

[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))
Run Code Online (Sandbox Code Playgroud)

这是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)));
Run Code Online (Sandbox Code Playgroud)

但问题仍然是:它是否正确javac<= 8能够推断出类型,但不是9+?

fla*_*kes 11

经过一些研究后,我相信我们可以将其作为Junit或hamcrest问题来排除.实际上,这似乎是一个JDK错误.以下代码将无法在JDK> 8中编译:

AnyOf<Iterable<? super String>> matcher = CoreMatchers.anyOf(
    CoreMatchers.hasItem("d"), CoreMatchers.hasItem("e"), CoreMatchers.hasItem("f"));
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

把它变成一个不使用库的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());
}
Run Code Online (Sandbox Code Playgroud)

使用单个变量可以实现类似的效果,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());
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

看起来当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());
Run Code Online (Sandbox Code Playgroud)

就像你提出的情况一样,将执行分解成多行将产生正确的结果:

A<B<? super D>> a1 = foo();
A<B<? super D>> a2 = foo();
C<B<? super D>> c = bar(a1, a2);
Run Code Online (Sandbox Code Playgroud)

因为有多种方法可以编写应该​​在功能上等效的代码,并且只考虑其中一些编译,我的结论是这不是JDK的预期行为.在具有super绑定的通配符的评估中某处存在错误.

我的建议是针对JDK 8编译现有代码,并针对需要JDK> 8的较新代码,以完全指定通用值.