概述
我们可以通过反射,获取对应的运行时类中所有的属性(Field)、方法(Method)、构造器(Constructor)、父类(Superclass)、接口(Interface)、父类的泛型、包、注解(Annotation )、异常等。。。。
获取全部的属性

field类位于java.lang.reflect包下。在Java反射中Field类描述的是类的属性信息
/*** 获取当前运行时类的属性结构** @author shkstart* @create 2019 下午 3:23*/public class FieldTest {@Testpublic void test1(){Class clazz = Person.class;//获取属性结构//getFields():获取当前运行时类及其父类中声明为public访问权限的属性Field[] fields = clazz.getFields();for(Field f : fields){System.out.println(f);}System.out.println();//getDeclaredFields():获取当前运行时类中声明的所有属性。(不包含父类中声明的属性)Field[] declaredFields = clazz.getDeclaredFields();for(Field f : declaredFields){System.out.println(f);}}//权限修饰符 数据类型 变量名@Testpublic void test2(){Class clazz = Person.class;Field[] declaredFields = clazz.getDeclaredFields();for(Field f : declaredFields){//1.权限修饰符int modifier = f.getModifiers();System.out.print(Modifier.toString(modifier) + "\t");//2.数据类型Class type = f.getType();System.out.print(type.getName() + "\t");//3.变量名String fName = f.getName();System.out.print(fName);System.out.println();}}}
获取全部的方法

@Testpublic void test1(){Class clazz = Person.class;//getMethods():获取当前运行时类及其所有父类中声明为public权限的方法Method[] methods = clazz.getMethods();for(Method m : methods){System.out.println(m);}System.out.println();//getDeclaredMethods():获取当前运行时类中声明的所有方法。(不包含父类中声明的方法)Method[] declaredMethods = clazz.getDeclaredMethods();for(Method m : declaredMethods){System.out.println(m);}}/*@Xxxx权限修饰符 返回值类型 方法名(参数类型1 形参名1,...) throws XxxException{}*/@Testpublic void test2(){Class clazz = Person.class;Method[] declaredMethods = clazz.getDeclaredMethods();for(Method m : declaredMethods){//1.获取方法声明的注解Annotation[] annos = m.getAnnotations();for(Annotation a : annos){System.out.println(a);}//2.权限修饰符System.out.print(Modifier.toString(m.getModifiers()) + "\t");//3.返回值类型System.out.print(m.getReturnType().getName() + "\t");//4.方法名System.out.print(m.getName());System.out.print("(");//5.形参列表Class[] parameterTypes = m.getParameterTypes();if(!(parameterTypes == null && parameterTypes.length == 0)){for(int i = 0;i < parameterTypes.length;i++){if(i == parameterTypes.length - 1){System.out.print(parameterTypes[i].getName() + " args_" + i);break;}System.out.print(parameterTypes[i].getName() + " args_" + i + ",");}}System.out.print(")");//6.抛出的异常Class[] exceptionTypes = m.getExceptionTypes();if(exceptionTypes.length > 0){System.out.print("throws ");for(int i = 0;i < exceptionTypes.length;i++){if(i == exceptionTypes.length - 1){System.out.print(exceptionTypes[i].getName());break;}System.out.print(exceptionTypes[i].getName() + ",");}}System.out.println();}}
获取构造器结构

@Testpublic void test1(){Class clazz = Person.class;//getConstructors():获取当前运行时类中声明为public的构造器Constructor[] constructors = clazz.getConstructors();for(Constructor c : constructors){System.out.println(c);}System.out.println();//getDeclaredConstructors():获取当前运行时类中声明的所有的构造器Constructor[] declaredConstructors = clazz.getDeclaredConstructors();for(Constructor c : declaredConstructors){System.out.println(c);}}
获取运行时类的父类


/*获取运行时类的父类*/@Testpublic void test2(){Class clazz = Person.class;Class superclass = clazz.getSuperclass();System.out.println(superclass);}/*获取运行时类的带泛型的父类*/@Testpublic void test3(){Class clazz = Person.class;Type genericSuperclass = clazz.getGenericSuperclass();System.out.println(genericSuperclass);}/*获取运行时类的带泛型的父类的泛型代码:逻辑性代码 vs 功能性代码*/@Testpublic void test4(){Class clazz = Person.class;Type genericSuperclass = clazz.getGenericSuperclass();ParameterizedType paramType = (ParameterizedType) genericSuperclass;//获取泛型类型Type[] actualTypeArguments = paramType.getActualTypeArguments();// System.out.println(actualTypeArguments[0].getTypeName());System.out.println(((Class)actualTypeArguments[0]).getName());}
获取运行时类实现的接口

/*获取运行时类实现的接口*/@Testpublic void test5(){Class clazz = Person.class;Class[] interfaces = clazz.getInterfaces();for(Class c : interfaces){System.out.println(c);}System.out.println();//获取运行时类的父类实现的接口Class[] interfaces1 = clazz.getSuperclass().getInterfaces();for(Class c : interfaces1){System.out.println(c);}}
获取运行时类所在的包
类所在的包Package getPackage()
/*获取运行时类所在的包*/@Testpublic void test6(){Class clazz = Person.class;Package pack = clazz.getPackage();System.out.println(pack);}
获取运行时类声明的注解

/*获取运行时类声明的注解*/@Testpublic void test7(){Class clazz = Person.class;Annotation[] annotations = clazz.getAnnotations();for(Annotation annos : annotations){System.out.println(annos);}}}
