javac之BridgeMethod及泛型擦除重写

本文深入探讨了Java泛型擦除机制及桥接方法的生成原理,解释了为何在类型擦除后,子类方法不会覆盖父类方法,以及编译器如何通过生成桥接方法来保持多态性。

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. You normally don't need to worry about bridge methods, but you might be puzzled if one appears in a stack trace.

public class Node<T> {

    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<Integer> {
    public MyNode(Integer data) { super(data); }

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

After type erasure, the Node and MyNode classes become:

public class Node {

    public Object data;

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

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

 

class MyNode extends Node {
    
    public MyNode(Integer data) {
        super(data);
    }
    
    public void setData(Integer data) {
        System.out.println("MyNode.setData");
        super.setData(data);
    }
    
    /*synthetic*/ public void setData(Object x0) {
        this.setData((Integer)x0);
    }
}

  

After type erasure, the method signatures do not match. The Node method becomes setData(Object) and the MyNode method becomes setData(Integer). Therefore, the MyNodesetData method does not override the Node setData method.

To solve this problem and preserve the polymorphism of generic types after type erasure, a Java compiler generates a bridge method to ensure that subtyping works as expected. For the MyNode class, the compiler generates the following bridge method for setData:

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);
    }

    // ...
}

As you can see, the bridge method, which has the same method signature as the Node class's setData method after type erasure, delegates to the original setData method.

 

下面来看个泛型擦除重写的例子,如下:

class Cell<A> {
	A value;
}

class Test {
	public void t() {
		Cell<Integer> cell = null;
		Object x1 = cell.value;
		Integer x2 = cell.value;
		int x3 = cell.value;
		long x4 = cell.value;
	}
}

最后必定为如下的格式:

class Test {
    Test() { }

    public void t() {
        Cell<Integer> cell = null;
        Object x1 = ((Cell)cell).value;
        Integer x2 = (Integer)((Cell)cell).value;
        int x3 = ((Integer)((Cell)cell).value).intValue();
        long x4 = (long)((Integer)((Cell)cell).value).intValue();
    }
}

  

  

 

 

参考:

(1)http://www.baeldung.com/java-type-erasure

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/extjs4/p/7500609.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值