java反射获得泛型参数getGenericSuperclass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class Person<T> { } import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class Student extends Person<Student> { public static void main(String[] args) { Student st=new Student(); Class clazz=st.getClass();
System.out.println(clazz.getSuperclass());
Type type=clazz.getGenericSuperclass(); System.out.println(type);
ParameterizedType p=(ParameterizedType)type;
Class c=(Class) p.getActualTypeArguments()[0]; System.out.println(c); } }
|
打印结果:
1 2 3
| class com.test.Person com.test.Person<com.test.Student> class com.test.Student
|
文章目录