本文介绍了Guice的TypeLiteral如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Guice的TypeLiteral如何克服Java通用类型擦除过程?

它有奇效,但它是如何完成的?

生存擦除。



如果您创建匿名子类 new TypeLiteral< List< String>>(){} Guice可以调用,并获得上存在一个方法获取 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如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:30