Guice的TypeLiteral如何工作? [英] How does guice's TypeLiteral work?
问题描述
它有奇效,但它是如何完成的?
如果您创建匿名子类 new TypeLiteral< List< String>>(){}
Guice可以调用 getClass()。getGenericSuperclass()
,并获得 java.lang.reflect.ParameterizedType
上存在一个方法 ge tActualTypeArguments()
获取 List< String>
作为 ParameterizedType
。
How does Guice's TypeLiteral overcome the Java generic types erasure procedure?
It works wonders but how is this accomplished?
The trick that is used here is that the signatures of generic super types are stored in subclasses and thus survive erasure.
If you create an anonymous subclass new TypeLiteral<List<String>>() {}
Guice can call getClass().getGenericSuperclass()
on it and get a java.lang.reflect.ParameterizedType
on which exists a method getActualTypeArguments()
to get List<String>
as an instance of ParameterizedType
.
这篇关于Guice的TypeLiteral如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!