在Java中输入Erasure [英] Type Erasure in Java
问题描述
类型擦除应该消除所有的通用信息...
如果是这种情况,像GSON这样的库如何使用泛型来确定要反序列化的类型?
eg
私人地图< String,Date> tenordates;
这将反序列化为< String,Date>
其中as
私人地图<日期,日期> tenordates;
将反序列化为< Date,Date>
所以不知何故,它在运行时使用通用信息。
类型擦除不会擦除所有类型的信息。它不会从类,字段,返回类型和参数定义中删除它。保留以下示例中的类型信息:
public class Foo extends List< Bar> {..}
私人列表< Foo> FOOS;
公共列表< Foo> getFoos(){..}
public void doSomething(List< Foo> foos){..}
这可通过反射访问 - java.lang.reflect.ParameterizedType
。您可以检查给定的 Type
是否为 instanceof
该类,并将其转换并获取类型信息。
Type erasure is supposed to erase all generic information... If this is the case how does a library like GSON use generics to determine what type to deserialize to?
e.g.
private Map<String,Date> tenordates;
This will deserialize to <String,Date>
where as
private Map<Date,Date> tenordates;
will deserialize to <Date,Date>
so somehow its using the generic info at runtime.
Type erasure does not erase all type information. It does not delete it from class, field, return type and parameter definitions. The type information in the following examples is retained:
public class Foo extends List<Bar> { ..}
private List<Foo> foos;
public List<Foo> getFoos() {..}
public void doSomething(List<Foo> foos) {..}
This is accesible via reflection - the java.lang.reflect.ParameterizedType
. You can check whether a given Type
is instanceof
that class, cast to it and obtain the type information.
这篇关于在Java中输入Erasure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!