getSuperclass:返回继承的父类,因为泛型擦除,因此不会获取到泛型参数。java
返回表示此 Class
所表示的实体(类、接口、基本类型或 void)的超类的 Class;
spring
若是此 Class
表示 Object
类、一个接口、一个基本类型或 void,则返回 null;数组
若是此对象表示一个数组类,则返回表示该 Object
类的 Class
对象。ide
getGenericSuperclass:返回继承的父类,包含泛型参数。学习
若是超类是参数化类型,则返回的 Type 对象必须准确反映源代码中所使用的实际类型参数。若是之前不曾建立表示超类的参数化类型,则建立这个类型。有关参数化类型建立过程的语义,请参阅 ParameterizedType
声明。测试
若是此 Class 表示 Object 类、接口、基本类型或 void,则返回 null。spa
若是此对象表示一个数组类,则返回表示 Object 类的 Class 对象。 code
GenericSignatureFormatError
- 若是常规类签名不符合 Java Virtual Machine Specification, 3rd edition 规定的格式;
TypeNotPresentException
- 若是常规超类引用不存在的类型声明;
MalformedParameterizedTypeException
- 若是常规超类引用的参数化类型因为某种缘由没法实例化。
public class Test { public static void main(String[] args) { System.out.println("Student.class.getSuperclass()\t" + Student.class.getSuperclass()); System.out.println("Student.class.getGenericSuperclass()\t" + Student.class.getGenericSuperclass()); System.out.println("Test.class.getSuperclass()\t" + Test.class.getSuperclass()); System.out.println("Test.class.getGenericSuperclass()\t" + Test.class.getGenericSuperclass()); System.out.println("Object.class.getGenericSuperclass()\t" + Object.class.getGenericSuperclass()); System.out.println("Object.class.getSuperclass()\t" + Object.class.getSuperclass()); System.out.println("void.class.getSuperclass()\t" + void.class.getSuperclass()); System.out.println("void.class.getGenericSuperclass()\t" + void.class.getGenericSuperclass()); System.out.println("int[].class.getSuperclass()\t" + int[].class.getSuperclass()); System.out.println("int[].class.getGenericSuperclass()\t" + int[].class.getGenericSuperclass()); } } class Person<T> { } class Student extends Person<Test> { }
获取打印结果:orm
Student.class.getSuperclass() class com.spring.type.Person Student.class.getGenericSuperclass() com.spring.type.Person<com.spring.type.Test> Test.class.getSuperclass() class java.lang.Object Test.class.getGenericSuperclass() class java.lang.Object Object.class.getGenericSuperclass() null Object.class.getSuperclass() null void.class.getSuperclass() null void.class.getGenericSuperclass() null int[].class.getSuperclass() class java.lang.Object int[].class.getGenericSuperclass() class java.lang.Object
Type是对java数据类型的分类抽象,子接口有:ParameterizedType、TypeVariable、WildcardType、GenericArrayType;对象
ParameterizedType:java泛型参数类型,在(1)中讲述方法getGenericSuperclass获取的类型即为参数化类型
方法介绍:
Type[] getActualTypeArguments():获取<>中的实际类型;
Type getRawType():获取<>前面的实际类型;
Type getOwnerType():获取全部者类型,不然会返回null.
/*以上文中说起的Person类和Student类进行举例*/ public static void main(String[] args) { System.out.println("Student.class.getGenericSuperclass()\t" + Student.class.getGenericSuperclass()); Type type = Student.class.getGenericSuperclass(); System.out.println("type.getRawType()\t" + ((ParameterizedType)type).getRawType()); System.out.println("type.getActualTypeArguments()\t" + ((ParameterizedType)type).getActualTypeArguments()); System.out.println("type.getOwnerType()\t" + ((ParameterizedType)type).getOwnerType()); }
如下为测试结果:
Student.class.getGenericSuperclass() com.spring.type.Person<com.spring.type.Test> type.getRawType() class com.spring.type.Person type.getActualTypeArguments() [Ljava.lang.reflect.Type;@1218025c //虽然Person类写在Test类中,可是不是子类,因此getOwnerType()方法返回null
type.getOwnerType() null
若是修改Person类为Test子类:
//写在Test类中 static class Person<T> { }
能够看到:
//会返回Person的全部类 type.getOwnerType() class com.spring.type.Test
TypeVariable:表明泛型中的变量,以下例中的“T”;
方法介绍:
Type[] getBounds():返回泛型中变量类型的上限数组,若是没有就直接打印Object;
String getName():返回变量类型名;
D getGenericDeclaration():获取声明该类型变量的实体.
AnnotatedType[] getAnnotatedBounds():JDK1.8中新增的方法,AnnotatedType接口用来表明被注解修饰的类型,能够获取变量上线类型的AnnotatedType封装类型数组,经过getType(),能够获取类型名称
如下举例说明:
public class TypeVariableTest<T extends Integer & Override> { T t; }
测试例:
public static void main(String[]args) throws NoSuchFieldException { Field fieldT=TypeVariableTest.class.getDeclaredField("t"); TypeVariable typeVariable=(TypeVariable)fieldT.getGenericType(); //返回类型的上限数组,若是没有就直接打印Object Type[] types = typeVariable.getBounds(); for (Type type:types) { System.out.println(type); } //打印类型的名称 System.out.println(typeVariable.getName()); //获取声明该类型变量的实体() System.out.println(typeVariable.getGenericDeclaration()); AnnotatedType[] annotatedBounds = typeVariable.getAnnotatedBounds(); for (AnnotatedType type:annotatedBounds) { System.out.println(type.getType()); } }
WildcardType:获取<>中的类型;
直接使用https://www.jianshu.com/p/7649f86614d3帮助理解:
public class WildcardTypeTest { private List<? extends String> listStr; private List<? super String> b; public static void testGetBounds(String field) throws NoSuchFieldException { System.out.println(field); Field fieldNum = WildcardTypeTest.class.getDeclaredField(field); Type typeNum = fieldNum.getGenericType(); ParameterizedType parameterizedTypeTypeNum = (ParameterizedType) typeNum; Type[] typesNum = parameterizedTypeTypeNum.getActualTypeArguments(); WildcardType wildcardType = (WildcardType) typesNum[0]; { Type[] types = wildcardType.getUpperBounds(); for (Type type : types) { System.out.println(type); } types = wildcardType.getLowerBounds(); for (Type type : types) { System.out.println(type); } } } public static void testGetUpperBounds() throws NoSuchFieldException { testGetBounds("listNum"); testGetBounds("b"); } public static void main(String[] args) throws NoSuchFieldException { testGetUpperBounds(); } }
GenericArrayType:表示泛型数组类型。泛型数组类型,例如List<String>[] 、T[]等;
方法:
Type getGenericComponentType():仅仅脱去最右边的[]以后剩下的内容就做为这个方法的返回值。
https://www.iteye.com/blog/jinnianshilongnian-1993608 ResolvableType 的学习可参考该文章