(1)、首先需要学习Type,第一步掌握Class的 getSuperclass与getGenericSuperclass
getSuperclass:返回继承的父类,由于泛型擦除,所以不会获取到泛型参数。
返回表示此 Class
所表示的实体(类、接口、基本类型或 void)的超类的 Class;
如果此 Class
表示 Object
类、一个接口、一个基本类型或 void,则返回 null;
如果此对象表示一个数组类,则返回表示该 Object
类的 Class
对象。
getGenericSuperclass:返回继承的父类,包含泛型参数。
如果超类是参数化类型,则返回的 Type 对象必须准确反映源代码中所使用的实际类型参数。如果以前未曾创建表示超类的参数化类型,则创建这个类型。有关参数化类型创建过程的语义,请参阅 ParameterizedType
声明。
如果此 Class 表示 Object 类、接口、基本类型或 void,则返回 null。
如果此对象表示一个数组类,则返回表示 Object 类的 Class 对象。
- 抛出:
GenericSignatureFormatError
- 如果常规类签名不符合 Java Virtual Machine Specification, 3rd edition 规定的格式;TypeNotPresentException
- 如果常规超类引用不存在的类型声明;MalformedParameterizedTypeException
- 如果常规超类引用的参数化类型由于某种原因无法实例化。- 使用例子进行理解:
-
public class Test { public static void main(String[] args) { System.out.println("Student.class.getSuperclass() " + Student.class.getSuperclass()); System.out.println("Student.class.getGenericSuperclass() " + Student.class.getGenericSuperclass()); System.out.println("Test.class.getSuperclass() " + Test.class.getSuperclass()); System.out.println("Test.class.getGenericSuperclass() " + Test.class.getGenericSuperclass()); System.out.println("Object.class.getGenericSuperclass() " + Object.class.getGenericSuperclass()); System.out.println("Object.class.getSuperclass() " + Object.class.getSuperclass()); System.out.println("void.class.getSuperclass() " + void.class.getSuperclass()); System.out.println("void.class.getGenericSuperclass() " + void.class.getGenericSuperclass()); System.out.println("int[].class.getSuperclass() " + int[].class.getSuperclass()); System.out.println("int[].class.getGenericSuperclass() " + int[].class.getGenericSuperclass()); } } class Person<T> { } class Student extends Person<Test> { }
获取打印结果:
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
(2):java-Type
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() " + Student.class.getGenericSuperclass()); Type type = Student.class.getGenericSuperclass(); System.out.println("type.getRawType() " + ((ParameterizedType)type).getRawType()); System.out.println("type.getActualTypeArguments() " + ((ParameterizedType)type).getActualTypeArguments()); System.out.println("type.getOwnerType() " + ((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 的学习可参考该文章