How to get type parameter values using java reflection?

前端 未结 2 903
温柔的废话
温柔的废话 2021-02-20 02:04
interface Foo { ... }
class Bar implements Foo { ... }

I\'ve got a Bar object. How to get the value of T

2条回答
  •  时光说笑
    2021-02-20 02:49

    If you already have Guava on the classpath, this is a bit more robust as you specify the interface/superclass by type rather than index.

    TypeToken baz = TypeToken.of(Bar.class).resolveType(Foo.class.getTypeParameters()[0]);
    System.out.println(baz.getRawType()); // class Baz
    

提交回复
热议问题