1

泛型通配符捕获和Helper方法

在某些情况下,编译器会推断出通配符的类型,例如,列表可以定义为List<?>,但是在评估表达式时,编译器会从代码中推断出特定类型,此场景称为通配符捕获。

在大多数情况下,你不必担心通配符捕获,除非你看到包含短语“capture of”的错误消息。

WildcardError示例在编译时产生捕获错误:

import java.util.List;

public class WildcardError {

    void foo(List<?> i) {
        i.set(0, i.get(0));
    }
}

在此示例中,编译器将i输入参数处理为Object类型,当foo方法调用List.set(int, E)时,编译器无法确认插入到列表中的对象的类型,并且会产生错误,发生此类错误时,通常意味着编译器认为你为变量分配了错误的类型,出于这个原因,泛型被添加到Java语言中 — 在编译时强制执行类型安全。

由Oracle的JDK 7 javac实现编译时,WildcardError示例生成以下错误:

WildcardError.java:6: error: method set in interface List<E> cannot be applied to given types;
    i.set(0, i.get(0));
     ^
  required: int,CAP#1
  found: int,Object
  reason: actual argument Object cannot be converted to CAP#1 by method invocation conversion
  where E is a type-variable:
    E extends Object declared in interface List
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
1 error

在此示例中,代码尝试执行安全操作,那么如何解决编译器错误?你可以通过编写捕获通配符的私有Helper方法来修复它,在这种情况下,你可以通过创建私有Helper方法fooHelper来解决此问题,如WildcardFixed中所示:

public class WildcardFixed {

    void foo(List<?> i) {
        fooHelper(i);
    }


    // Helper method created so that the wildcard can be captured
    // through type inference.
    private <T> void fooHelper(List<T> l) {
        l.set(0, l.get(0));
    }

}

由于Helper方法,编译器使用推断来确定T是调用中的CAP#1(捕获变量),该示例现在成功编译。

按照惯例,Helper方法通常命名为originalMethodNameHelper

现在考虑一个更复杂的例子,WildcardErrorBad

import java.util.List;

public class WildcardErrorBad {

    void swapFirst(List<? extends Number> l1, List<? extends Number> l2) {
      Number temp = l1.get(0);
      l1.set(0, l2.get(0)); // expected a CAP#1 extends Number,
                            // got a CAP#2 extends Number;
                            // same bound, but different types
      l2.set(0, temp);        // expected a CAP#1 extends Number,
                            // got a Number
    }
}

在这个例子中,代码正在尝试不安全的操作,例如,考虑以下对swapFirst方法的调用:

List<Integer> li = Arrays.asList(1, 2, 3);
List<Double>  ld = Arrays.asList(10.10, 20.20, 30.30);
swapFirst(li, ld);

List<Integer>List<Double>都符合List<? extends Number>的标准,从Integer值列表中获取项目并尝试将其放入Double值列表中显然是不正确的。

使用Oracle的JDK javac编译器编译代码会产生以下错误:

WildcardErrorBad.java:7: error: method set in interface List<E> cannot be applied to given types;
      l1.set(0, l2.get(0)); // expected a CAP#1 extends Number,
        ^
  required: int,CAP#1
  found: int,Number
  reason: actual argument Number cannot be converted to CAP#1 by method invocation conversion
  where E is a type-variable:
    E extends Object declared in interface List
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Number from capture of ? extends Number
WildcardErrorBad.java:10: error: method set in interface List<E> cannot be applied to given types;
      l2.set(0, temp);      // expected a CAP#1 extends Number,
        ^
  required: int,CAP#1
  found: int,Number
  reason: actual argument Number cannot be converted to CAP#1 by method invocation conversion
  where E is a type-variable:
    E extends Object declared in interface List
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Number from capture of ? extends Number
WildcardErrorBad.java:15: error: method set in interface List<E> cannot be applied to given types;
        i.set(0, i.get(0));
         ^
  required: int,CAP#1
  found: int,Object
  reason: actual argument Object cannot be converted to CAP#1 by method invocation conversion
  where E is a type-variable:
    E extends Object declared in interface List
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
3 errors

这里没有Helper方法来解决这个问题,因为代码根本就是错误的。


上一篇:泛型通配符

博弈
2.5k 声望1.5k 粉丝

态度决定一切