javac之BridgeMethod及泛型擦除重写
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
javac之BridgeMethod及泛型擦除重写的更多相关文章
- Java中泛型区别以及泛型擦除详解
一.引言 复习javac的编译过程中的解语法糖的时候看见了泛型擦除中的举例,网上的资料大多比较散各针对性不一,在此做出自己的一些详细且易懂的总结. 二.泛型简介 泛型是JDK 1.5的一项新特性,一种 ...
- 泛型深入--java泛型的继承和实现、泛型擦除
泛型实现类: package generic; /** * 泛型父类:子类为“富二代”:子类的泛型要比父类多 * 1,保留父类的泛型-->子类为泛型类 * 2,不保留父类泛型-->子类按需 ...
- 关于Java泛型"擦除"的一点思考
头次写博客,想说的东西不难,关于泛型的疑问,是前一阵在学习jackson中遇到的. 下面就把我所想到的.遇到的,分享出来. 泛型是JDK1.5后的一个特性,是一个参数类型的应用,可以将这个参数声明在类 ...
- java中的伪泛型---泛型擦除(不需要手工强转类型,却可以调用强转类型的方法)
Java集合如Map.Set.List等所有集合只能存放引用类型数据,它们都是存放引用类型数据的容器,不能存放如int.long.float.double等基础类型的数据. 使用反射可以破解泛型T类型 ...
- 深入理解泛型之JAVA泛型的继承和实现、泛型擦除
很多的基础类设计会采用泛型模式,有些应用在使用的时候处于隔离考虑,会进行继承,此时子类如何继承泛型类就很讲究了,有些情况下需要类型擦除,有些情况下不需要类型擦除,但是大多数情况下,我们需要的是保留父类 ...
- [转] Java 的泛型擦除和运行时泛型信息获取
原文链接 https://my.oschina.net/lifany/blog/875769 前言 现在很多程序员都会在简历中写上精通 Java.但究竟怎样才算是精通 Java 呢?我觉得不仅要熟练掌 ...
- Java笔记——泛型擦除
1. 泛型擦除 package cn.Douzi.T_Demo; import java.util.ArrayList; /** * @Auther: Douzi * @Date: 2019/3/8 ...
- Java的反射机制与泛型擦除
实现方式 反编译:.class–>.java 通过反射机制访问java对象的属性,方法,构造方法等涉及类 java.lang.Class; java.lang.reflect.Construct ...
- Java泛型擦除
Java泛型擦除: 什么是泛型擦除? 首先了解一下什么是泛型?我个人的理解:因为集合中能够存储随意类型的对象.可是集合中最先存储的对象类型一旦确定后,就不能在存储其它类型的对象了,否则,编译时不会报错 ...
随机推荐
- OpenGL + MFC
OpenGL超级宝典(中文版) 2001年 本书是一本完整而详尽的关于OpenGL的参考书,全书分为四部分:第一部分“OpenGL导言”介绍3D图形学的基本原理,读者将在此学会构造使用OpenGL的程 ...
- (字典树模板)统计难题--hdu--1251
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 在自己敲了一遍后终于懂了,这不就用了链表的知识来建立了树,对!就是这样的,然后再查找 代码: #i ...
- ACL授权实例
上一篇关于ACL的文章中:位运算实现ACL授权与认证过程的原理解析,我们学习了通过位运算实现ACL授权与认证的原理核心,今天我们一起来看授权的实例. 实现的功能很简单:打开授权界面时,加载已授权信息. ...
- WPF中得到一个控件相对其他控件的坐标
加入想得到按钮btnTest左上角相对于主窗体winTest的坐标,可以用如下方法:btnTest.TranslatePoint(new Point(0, 0), winTest)这个方法返回一个Po ...
- Debezium for PostgreSQL to Kafka
In this article, we discuss the necessity of segregate data model for read and write and use event s ...
- ActiveMq 总结(二)
4.2.6 MessageConsumer MessageConsumer是一个由Session创建的对象,用来从Destination接收消息. 4.2.6.1 创建MessageConsumer ...
- c#中取整方式
主要用到 System 命名空间下的一个数据类 Math ,调用他的方法 一共有三种方式: 第一种 Math.Round:根据四舍五入取整 第二种 Math.Ceiling:向上取整,有小数,整数加1 ...
- Xml 序列化和反序列化
xml序列化帮助类 using System.IO; using System.Xml; using System.Xml.Serialization; public class XmlHelper ...
- CentOS 6 - 升级内核
有的时候,需要升级Linux内核,今天我就是在CentOS 6中升级内核,在没有升级内核之前,我的CentOS 6只有2.6.32这一个内核,也是默认启动的内核.下面就开始一步步操作升级内核了! 一, ...
- hashlib(摘要算法的模块)--重要 (一)
课件地址:https://www.cnblogs.com/mys6/p/10584933.html 搜索hashlib模块 # 登录认证# 加密 --> 解密# 摘要算法# 两个字符串 :# ...