[Generics] 泛型实现原理

Generic implementation principle...

Posted by Mr.Vincent on 2021-04-27
Estimated Reading Time 1 Minutes
Words 383 In Total
Viewed Times

泛型实现原理

JDK 1.5增加的新特性里面有一个就是泛型。对于泛型的评价,褒贬不一,废话不多说,先来看看他的原理。泛型是提供给 javac 编译器使用的,可以限定集合中的输入类型,让编译器拦截源程序中的非法输入,编译器编译带类型说明的集合时会去掉类型信息,对于参数化得泛型类型,getClass() 方法的返回值和原始类型完全一样。

对于下面这个源程序:

1
2
3
4
5
6
7
8
public class Oliver {  
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("str1");
list.add("str2");
String str = list.get(0);
}
}

编译成 Oliver.class 后反编译的内容:

1
2
3
4
5
6
7
8
9
10
11
public class Oliver {  
public Oliver() {
}

public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("str1");
list.add("str2");
String str = (String) list.get(0);
}
}

也就是说 java 的泛型只是在编译器做了参数限制,其实对性能并没有什么优化!由于编译生成的字节码会去掉泛型的类型信息,只要能跳过编译器,就可以往某个泛型集合中加入其它的类型数据。下面代码展示利用反射机制跳过编译器检查:
1
2
3
4
5
6
7
public class Oliver {  
public static void main(String[] args) throws Exception {
ArrayList<Integer> list = new ArrayList<Integer>();
list.getClass().getMethod("add", Object.class).invoke(list, "ssss");
System.out.println("list:" + list.get(0));
}
}

输出结果:

1
list:ssss


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !