【The Java™ Tutorials】【Generics】8. Type Erasure

泛型信息只存在于代码编译阶段,在进入 JVM 之前,与泛型相关的信息会被擦除掉。

引入泛型的目的:
Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming.

为了实现泛型,Java编译器进行了在编译的时候会进行类型擦除:

  • Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
  • Insert type casts if necessary to preserve type safety.
  • Generate bridge methods to preserve polymorphism in extended generic types.

Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.

Erasure of Generic Types

unbound

public class Node {

    private T data;
    private Node next;

    public Node(T data, Node next) {
        this.data = data;
        this.next = next;
    }

    public T getData() { return data; }
    // ...
}

类型擦除之后:

public class Node {

    private Object data;
    private Node next;

    public Node(Object data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Object getData() { return data; }
    // ...
}

bound

public class Node> {

    private T data;
    private Node next;

    public Node(T data, Node next) {
        this.data = data;
        this.next = next;
    }

    public T getData() { return data; }
    // ...
}

类型擦除之后:

public class Node {

    private Comparable data;
    private Node next;

    public Node(Comparable data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Comparable getData() { return data; }
    // ...
}

注意经过类型擦除之后,Node变为Node,Comparable变为Comparable。

Erasure of Generic Methods

unbound

// Counts the number of occurrences of elem in anArray.
//
public static  int count(T[] anArray, T elem) {
    int cnt = 0;
    for (T e : anArray)
        if (e.equals(elem))
            ++cnt;
        return cnt;
}

类型擦除之后:

public static int count(Object[] anArray, Object elem) {
    int cnt = 0;
    for (Object e : anArray)
        if (e.equals(elem))
            ++cnt;
        return cnt;
}

bound

假设有如下类定义:

class Shape { /* ... */ }
class Circle extends Shape { /* ... */ }
class Rectangle extends Shape { /* ... */ }
public static  void draw(T shape) { /* ... */ }

类型擦除之后变为:

public static void draw(Shape shape) { /* ... */ }

Effects of Type Erasure and Bridge Methods

类型擦除可能会导致一些你没有预料到的情况,比如下面这个例子:

有下面两个类:

public class Node {

    public T data;

    public Node(T data) { this.data = data; }

    public void setData(T data) {
        System.out.println("Node.setData");
        this.data = data;
    }
}

public class MyNode extends Node {
    public MyNode(Integer data) { super(data); }

    public void setData(Integer data) {
        System.out.println("MyNode.setData");
        super.setData(data);
    }
}

下面这段代码:

MyNode mn = new MyNode(5);
Node n = mn;            // A raw type - compiler throws an unchecked warning
n.setData("Hello");     
Integer x = mn.data;    // Causes a ClassCastException to be thrown.

经过类型擦除之后会变为:

MyNode mn = new MyNode(5);
Node n = (MyNode)mn;         // A raw type - compiler throws an unchecked warning
n.setData("Hello");
Integer x = (String)mn.data; // Causes a ClassCastException to be thrown.

这段代码编译的时候并不会报错,但是这显然不合理。因为我们传进去一个String,但是在mn看来,里面存的是一个Integer。我们运行的时候会出现如下错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at Test.main(Test.java:10)

需要注意的是,是n.setData("Hello")抛出了ClassCastException异常,这是为什么呢?n.setData("Hello")调用的不是Node中的setData(Object data)吗?

Bridge Methods

When compiling a class or interface that extends a parameterized class or implements a parameterized interface, the compiler may need to create a synthetic method, called a bridge method, as part of the type erasure process.

在类型擦除之后,Node中的方法变为setData(Object data),而MyNode中的方法setData(Integer data),这两个方法的签名是不一样的,即MyNode中的setData不是对Node的setData的重写。

为了保留类型擦除之后的多态性,Java编译器会自动生成一个bridge method,使得子类的行为符合预期,如下所示:

class MyNode extends Node {

    // Bridge method generated by the compiler
    //
    public void setData(Object data) {
        setData((Integer) data);
    }

    public void setData(Integer data) {
        System.out.println("MyNode.setData");
        super.setData(data);
    }

    // ...
}

这就可以理解上面的代码为什么编译的时候不会报错,而执行的时候会抛出异常了(相当于执行了以下代码)。

Integer i = (Integer)(Object) "Hello";

Restrictions on Generics

Cannot Instantiate Generic Types with Primitive Types

Pair p = new Pair<>(8, 'a');  // compile-time error

可以用Java的自动装箱修改以上代码:

Pair p = new Pair<>(8, 'a');

Cannot Create Instances of Type Parameters

public static  void append(List list) {
    E elem = new E();  // compile-time error
    list.add(elem);
}

这是很好理解的,因为你根本不知道E有哪些构造函数,有些类是没有不带参数的构造函数的,new E()不知道具体调用哪个构造函数。

可以利用反射绕过上面的问题:

public static  void append(List list, Class cls) throws Exception {
    E elem = cls.newInstance();   // OK
    list.add(elem);
}

这样我们就可以像下面这样调用append方法:

List ls = new ArrayList<>();
append(ls, String.class);

Cannot Declare Static Fields Whose Types are Type Parameters

A class's static field is a class-level variable shared by all non-static objects of the class. Hence, static fields of type parameters are not allowed.
假设是允许的,就会在语义上出现混乱:

public class MobileDevice {
    private static T os;

    // ...
}
MobileDevice phone = new MobileDevice<>();
MobileDevice pager = new MobileDevice<>();
MobileDevice pc = new MobileDevice<>();

因为os是被phone、pager和pc共享的,那现在os的具体类型是什么呢?

Cannot Use Casts or instanceof with Parameterized Types

Because the Java compiler erases all type parameters in generic code, you cannot verify which parameterized type for a generic type is being used at runtime:

public static  void rtti(List list) {
    if (list instanceof ArrayList) {  // compile-time error
        // ...
    }
}

运行的时候并没有跟踪类型参数,所以并不能区分ArrayList和ArrayList。我们能做的,最多是用unbounded wildcard来区分一个list是不是ArrayList:

public static void rtti(List list) {
    if (list instanceof ArrayList) {  // OK; instanceof requires a reifiable type
        // ...
    }
}

特别的,你不能强制转换为参数化的类型(除非是unbounded wildcards):

List li = new ArrayList<>();
List  ln = (List) li;  // compile-time error

但是像下面这种情况是可以强制转换的:

List l1 = ...;
ArrayList l2 = (ArrayList)l1;  // OK

Cannot Create Arrays of Parameterized Types

List[] arrayOfLists = new List[2];  // compile-time error

我们先来看一段非泛型代码:

Object[] strings = new String[2];
strings[0] = "hi";   // OK
strings[1] = 100;    // An ArrayStoreException is thrown.

我们假设是能创建泛型数组的,我们用泛型数组做一样的事:

Object[] stringLists = new List[];  // compiler error, but pretend it's allowed
stringLists[0] = new ArrayList();   // OK
stringLists[1] = new ArrayList();  // An ArrayStoreException should be thrown, but the runtime can't detect it.

如果可以创建泛型数组,上面的代码就会如法抛出ArrayStoreException异常。

Cannot Create, Catch, or Throw Objects of Parameterized Types

泛型类不能直接或间接继承Throwable:

// Extends Throwable indirectly
class MathException extends Exception { /* ... */ }    // compile-time error

// Extends Throwable directly
class QueueFullException extends Throwable { /* ... */ // compile-time error

不能捕获类型参数实例:

public static  void execute(List jobs) {
    try {
        for (J job : jobs)
            // ...
    } catch (T e) {   // compile-time error
        // ...
    }
}

但是可以在throws语句中使用类型参数:

class Parser {
    public void parse(File file) throws T {     // OK
        // ...
    }
}

Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type

A class cannot have two overloaded methods that will have the same signature after type erasure.
所以下面的代码会出现编译错误:

public class Example {
    public void print(Set strSet) { }
    public void print(Set intSet) { }
}

你可能感兴趣的:(【The Java™ Tutorials】【Generics】8. Type Erasure)