Java反射——获取运行时类的内容
1.获取运行时类的属性
1 public static void test1() { 2 Class clazz = Person.class; 3 //getFields():获取当前运行时类及其父类中声明为public访问权限的属性 4 Field[] fields = clazz.getFields(); 5 for(Field f : fields) { 6 System.out.println(f); 7 } 8 Field[] declaredFields = clazz.getDeclaredFields(); 9 for (Field f : declaredFields) { 10 //1.权限修饰符 11 int modifier = f.getModifiers(); 12 System.out.println(modifier + "--" + Modifier.toString(modifier)); 13 //2.获取数据类型 14 Class type = f.getType(); 15 System.out.println(type); 16 //3.获取变量名 17 String fName = f.getName(); 18 System.out.println(fName); 19 } 20 }
2.获取运行时类的方法
1 public static void test1() { 2 Class clazz = Person.class; 3 //获取当前运行时类及其父类声明为public权限的方法 4 Method[] methods = clazz.getMethods(); 5 for(Method m : methods) { 6 System.out.println(m); 7 } 8 System.out.println(); 9 //获取当前运行时类声明的所有方法 10 Method[] declaredMethods = clazz.getDeclaredMethods(); 11 for (Method m : declaredMethods) { 12 System.out.println(m); 13 } 14 }
获取方法内的有关信息
1 public static void test2() { 2 Class clazz = Person.class; 3 Method[] declaredMethods = clazz.getDeclaredMethods(); 4 for (Method m : declaredMethods) { 5 //1.获取方法声明的注解 6 Annotation[] annotation = m.getAnnotations(); 7 for (Annotation a : annotation) { 8 System.out.println(a); 9 } 10 //2.权限修饰符 11 System.out.println("修饰符为:" + Modifier.toString(m.getModifiers())); 12 //3.获取方法的返回值类型 13 System.out.println("返回值类型为:" + m.getReturnType()); 14 //4.获取方法的方法名 15 System.out.println("方法名为:" + m.getName()); 16 //5.形参列表 17 Class[] parameterTypes = m.getParameterTypes(); 18 System.out.print("形参为:"); 19 if (parameterTypes != null && parameterTypes.length != 0) { 20 for (Class par : parameterTypes) { 21 System.out.print(par.getName() + " "); 22 } 23 } 24 System.out.println(); 25 //6.抛出的异常 26 Class[] exceptionTypes = m.getExceptionTypes(); 27 System.out.print("抛出的异常为:"); 28 if (exceptionTypes != null && exceptionTypes.length != 0) { 29 for (Class exc : exceptionTypes) { 30 System.out.print(exc.getName() + " "); 31 } 32 } 33 System.out.println(); 34 } 35 }
3.获取构造器
1 /* 2 获取构造器结构 3 */ 4 public static void test1() { 5 Class clazz = Person.class; 6 Constructor[] constructors = clazz.getConstructors(); 7 for (Constructor c : constructors) { 8 System.out.println(c); 9 } 10 Constructor[] declaredConstructor = clazz.getDeclaredConstructors(); 11 for (Constructor c : declaredConstructor) { 12 System.out.println(c); 13 } 14 }
4.获取运行时带泛型的父类
1 public static void test2() { 2 Class clazz = Person.class; 3 //获取带泛型父类 4 Type genericSuperclass = clazz.getGenericSuperclass(); 5 //转为参数化类型 6 ParameterizedType paramType = (ParameterizedType) genericSuperclass; 7 //获取所有的参数 8 Type[] actualTypeArguments = paramType.getActualTypeArguments(); 9 System.out.println(genericSuperclass); 10 System.out.println(actualTypeArguments[0].getTypeName()); 11 }
5.获取运行时类所在的接口
1 public static void test3() { 2 Class clazz = Person.class; 3 Class[] interfaces = clazz.getInterfaces(); 4 for (Class c : interfaces) { 5 System.out.println(c); 6 } 7 }
6.获取运行时类所在的包
1 public static void test4() { 2 Class clazz = Person.class; 3 Package pack = clazz.getPackage(); 4 System.out.println(pack); 5 }
7.获取运行时类所在的注解
1 public static void test5() { 2 Class clazz = Person.class; 3 Annotation[] annotation = clazz.getAnnotations(); 4 for (Annotation anno : annotation) { 5 System.out.println(anno); 6 } 7 }