public interface ParameterizedType extends Type {}
方法用途举例Type[] getActualTypeArguments();获取泛型<T,U>的内容,若无泛型则返回空数组对于Map<String,Integer>返回的Type[]为String和IntegerType getRawType();获取<>前面的类对于Map<String,Integer>返回的Type为MapType getOwnerType();获取<>前面的类(记为A)的拥有者,如果A是顶层类,那么此方法返回nullif this type is {@code O<T>.I<S>}, return a representation of {@code O<T>}public interface GenericArrayType extends Type {}
方法用途举例Type getGenericComponentType();获取数组的门面类型List<?>[]得到的是List<?>public interface TypeVariable extends Type, AnnotatedElement {}
方法用途举例Type[] getBounds();获取泛型的上界,无显示定义extends定义上界,则默认上界为ObjectD getGenericDeclaration();Java中可以声明泛型变量的地方有三个class,contructor和method。此方法就是获取到声明类型变量的语法元素(指向哪个class,哪个class的哪个构造方法,哪个class的哪个方法)String getName();获取名称,即K,VAnnotatedType[] getAnnotatedBounds();public interface TypeVariable extends Type, AnnotatedElement {}
方法用途举例Type[] getUpperBounds();获取?的上界,默认上界为ObjectType[] getLowerBounds();获取?的下界。若无下界则返回空数组类和对象的关系判断
instanceof 用法 Object instanceof Classinstanceof是Java中的二元运算符,左边是对象,右边是类;当对象是右边类或子类所创建对象时,返回true;否则,返回false左边不能是基础类型null用instanceof跟任何类型比较时都是false Class.isInstanceof(Object) 与instanceof功能等价 Class.isAssignableFrom(Class) 类和类的关系判断,如果左侧是右侧的父类或本类,则返回true public static class Father { } public static class Son extends Father { } public static void main(String[] args) { Father father = new Father(); Son son = new Son(); System.out.println(father instanceof Father);// true System.out.println(son instanceof Father);// true System.out.println(Father.class.isInstance(father));// true System.out.println(Father.class.isInstance(son));// true System.out.println(Father.class.isAssignableFrom(Father.class));// true System.out.println(Father.class.isAssignableFrom(Son.class));// true System.out.println(Son.class.isAssignableFrom(Father.class));// false }获取名称
getName() 获取全路径类名 public String getName() { String name = this.name; if (name == null) this.name = name = getName0(); return name; } private native String getName0();如果是基本类型,则返回byte、void等 如果是对象类型,则返回全路径(包名+类名) 如果是数组类型,则按照下面的格式返回 2. getTypeName() 和getName()区别在于数组类型,对数组类型的展示方法进行优化,转换符合java语法的形式
public String getTypeName() { if (isArray()) { try { Class<?> cl = this; int dimensions = 0; while (cl.isArray()) { dimensions++; cl = cl.getComponentType(); } StringBuilder sb = new StringBuilder(); sb.append(cl.getName()); for (in t i = 0; i < dimensions; i++) { sb.append("[]"); } return sb.toString(); } catch (Throwable e) { /*FALLTHRU*/ } } return getName(); } System.out.println((new Object[3]).getClass().getName());//[Ljava.lang.Object; System.out.println((new Object[3]).getClass().getTypeName());//java.lang.Object[] System.out.println((new int[3][4][5][6][7][8][9]).getClass().getName());//[[[[[[[I System.out.println((new int[3][4][5][6][7][8][9]).getClass().getTypeName());//int[][][][][][][] getCanonicalName() 和getTypeName()区别在于内部类,对于普通内部类,返回符合java语言书写规范的全路径。 对于匿名内部类则返回null public class ClassTest { public static class Father { } public static class Son extends Father { } public static void main(String[] args) { // 普通类 Father father = new Father(); System.out.println("----普通类----"); System.out.println(father.getClass().getName());//ClassTest$Father System.out.println(father.getClass().getTypeName());//ClassTest$Father System.out.println(father.getClass().getCanonicalName());//ClassTest.Father System.out.println(father.getClass().getSimpleName()); } } getSimpleName() 获取简单类名,在getCanonicalName()的基础上,去掉package路径对于不同类型,以上方法的返回值
public class ClassTest { public static class Father { } public static void main(String[] args) { // 普通类 Father father = new Father(); System.out.println("----普通类----"); System.out.println(father.getClass().getName());//ClassTest$Father System.out.println(father.getClass().getTypeName());//ClassTest$Father System.out.println(father.getClass().getCanonicalName());//ClassTest.Father System.out.println(father.getClass().getSimpleName());//Father // 匿名类 Father inner = new Father(){}; System.out.println("----内部类----"); System.out.println(inner.getClass().getName());//ClassTest$1 System.out.println(inner.getClass().getTypeName());//ClassTest$1 System.out.println(inner.getClass().getCanonicalName());//null System.out.println(inner.getClass().getSimpleName());//空字符串 // 数组 Father[] array = new Father[1]; System.out.println("----对象数组类----"); System.out.println(array.getClass().getName());//[LClassTest$Father; System.out.println(array.getClass().getTypeName());//ClassTest$Father[] System.out.println(array.getClass().getCanonicalName());//ClassTest.Father[] System.out.println(array.getClass().getSimpleName());//Father[] // 基本类型数组 int[] rawArray = new int[1]; System.out.println("----基本类型数组类----"); System.out.println(rawArray.getClass().getName());//[I System.out.println(rawArray.getClass().getTypeName());//int[] System.out.println(rawArray.getClass().getCanonicalName());//int[] System.out.println(rawArray.getClass().getSimpleName());//int[] } } class.getDeclaringClass()和field.getDeclaringClass()方法的用途 @FieldNameConstants public class ClassTest { private Father father; public ClassTest() { this.father = new Father(); } public static class Father { } @SneakyThrows public static void main(String[] args) { ClassTest classTest = new ClassTest(); // class ClassTest System.out.println(classTest.father.getClass().getDeclaringClass()); // class ClassTest Field field = ClassTest.class.getDeclaredField(ClassTest.FIELD_FATHER); System.out.println(field.getDeclaringClass()); } } 获取class的注解信息 class注解操作(AnnotatedElement) Spring注解工具类AnnotatedElementUtils和AnnotationUtilsSpring工具类