How does guice's TypeLiteral work?

╄→гoц情女王★ 提交于 2019-12-02 16:17:44
Moritz

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.

By a combination of anonymous types, subclassing and the fact that Java does not completely erase ALL generic declarations.

If you look closely the TypeLiteral has a protected constructor so you use an additional {} when constructing a new one which creates an anonymous subclass of TypeLiteral.

In Java generic declarations are preserved on Class and Method declarations, so if I write this.

public abstract class Class1<T>
{
}

public class Class2 extends Class1<Integer>
{
}

I can actually write code in Class1 that can figure out that its own generic type is Integer if Class2 was the subclass.

Check out the java.lang.Class API for the appropriate methods (they have Generic in the name).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!