java 获取 T.class
转载:http://www.hankcs.com/program/t-class.html
@Test public void Test() throws Exception{ Foo<User> foo = new Foo<User>() { }; // 在类的外部这样获取 Type type = ((ParameterizedType)foo.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; System.out.println(type); // 在类的内部这样获取 System.out.println(foo.getTClass()); } public class Foo<T> { public Class<T> getTClass() { Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; return tClass; } }