import java.lang.reflect.ParameterizedType;
public class Test<T> {
public Class<T> getTClass() {
return (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
public void test() {
System.out.println(getTClass());
}
public static void main(String s[]) {
(new Test<String>() {
}).test();
}
}
输出结果:
class java.lang.String