Get type of a generic parameter in Java with reflection

后端 未结 18 1957
死守一世寂寞
死守一世寂寞 2020-11-22 05:56

Is it possible to get the type of a generic parameter?

An example:

public final class Voodoo {
    public static void chill(List aListWithTy         


        
18条回答
  •  情歌与酒
    2020-11-22 06:15

    One construct, I once stumbled upon looked like

    Class persistentClass = (Class)
       ((ParameterizedType)getClass().getGenericSuperclass())
          .getActualTypeArguments()[0];
    

    So there seems to be some reflection-magic around that I unfortunetly don't fully understand... Sorry.

提交回复
热议问题