How does guice's TypeLiteral work?

前端 未结 2 1431
走了就别回头了
走了就别回头了 2021-01-30 22:29

How does Guice\'s TypeLiteral overcome the Java generic types erasure procedure?

It works wonders but how is this accomplished?

2条回答
  •  礼貌的吻别
    2021-01-30 23:01

    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>() {} Guice can call getClass().getGenericSuperclass() on it and get a java.lang.reflect.ParameterizedType on which exists a method getActualTypeArguments() to get List as an instance of ParameterizedType.

提交回复
热议问题