问题的起因是在我使用 Spring 的BeanUtils.copyProperties方法时发现两个 POJO 如果拥有相同的字段名,即使泛型类型不同,值也会被拷贝过去。例如类 A 有一个类型为List<Integer>的字段 integerList,类 B 有一个类型为List<String>的字段也叫 integerList,那么使用 BeanUtils.copyProperties(b, a)时,integerList 字段会被拷贝过去。这时我想起了 Java 的类型擦除问题。我想问:
- 上述问题是因为类型擦除导致的吗
- 网上很多文章都是说类型擦除是发生在编译时,即编译后的 class 文件已经没有泛型类型信息了。但 class 文件反编译后的确还是有泛型信息的,如下图。所以类型擦除究竟发生在哪个阶段?

- 如下的代码中,通过打断点可以看到 method 的 signature 字段是有泛型类型信息的,而这已经是运行时了,为什么泛型类型信息还在?
public class InvokeDTO {
private List<Integer> integerList;
public List<Integer> getIntegerList() {
return integerList;
}
public void setIntegerList(List<Integer> integerList) {
this.integerList = integerList;
}
}
public static void main(String[] args) {
InvokeDTO invokeDTO = new InvokeDTO();
Method[] methods = invokeDTO.getClass().getMethods();
for (Method method : methods) {
try {
if (method.getName().equals("setIntegerList")) {
method.invoke(invokeDTO, Lists.newArrayList("string0", "string1"));
}
} catch (Exception e) {}
}
System.out.println(invokeDTO.getIntegerList());
}
