反射 Java提供反射来在运行时状态下动态的获取类的属性、方法等信息,在框架中很多地方都用到了反射
反射的操作 获取Class对象 获取Class对象的四种方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 Class<Person> clazz = Person.class; Person person = new Person(); Class clazz1 = person.getClass(); String className = "com.zhanghe.study.reflect.Person" ; Class clazz2 = Class.forName(className); ClassLoader classLoader = TestClass.class.getClassLoader(); Class clazz3 = classLoader.loadClass(className);
获取构造器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Class clazz = TestConstructor.class; Constructor[] constructors = clazz.getConstructors(); System.out.println("=========clazz.getConstructors()====只能获取到公有的构造器" ); for (Constructor constructor : constructors){ System.out.println(constructor); } System.out.println("*****************" ); System.out.println("=========clazz.getConstructor()=====获取公有无参构造器" ); Constructor constructor = clazz.getConstructor(); System.out.println(constructor); System.out.println("*****************" ); System.out.println("=========clazz.getDeclaredConstructors()=====获取所有构造器(包括私有、受保护、默认、公有)" ); Constructor[] declaredConstructors = clazz.getDeclaredConstructors(); for (Constructor declaredConstructor : declaredConstructors){ System.out.println(declaredConstructor); } System.out.println("*****************" ); System.out.println("=========clazz.getDeclaredConstructor()====获取指定参数类型的构造器" ); Constructor declaredConstructor = clazz.getDeclaredConstructor(String.class); System.out.println(declaredConstructor); System.out.println("*****************" );
获取属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 System.out.println("======getFields===========" ); Field[] fields = clazz.getFields(); for (Field f : fields){ System.out.println(f.getName()); } System.out.println("======getDeclaredFields===========" ); Field[] declaredFields = clazz.getDeclaredFields(); for (Field f : declaredFields){ int i = f.getModifiers(); String m = Modifier.toString(i); System.out.println("Modifier:" +m); Class type = f.getType(); System.out.println("type:" +type.getTypeName()); System.out.println("name:" +f.getName()); }
获取方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 System.out.println("======getMethods===========" ); Method[] methods = clazz.getMethods(); for (Method m : methods){ System.out.println(m.getName()); } System.out.println("======getDeclaredMethods===========" ); Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method m : declaredMethods){ Annotation[] annotations = m.getAnnotations(); System.out.println("Modifier:" +Modifier.toString(m.getModifiers())); Class reture = m.getReturnType(); Class[] params = m.getParameterTypes(); System.out.println(m.getName()); Class[] exceptions = m.getExceptionTypes(); }
获取父类信息 1 2 3 4 5 6 7 8 9 10 Class superClass = clazz.getSuperclass(); Type superType = clazz.getGenericSuperclass(); if (superType instanceof ParameterizedType){ Type[] actualTypeArguments = ((ParameterizedType) superType).getActualTypeArguments(); }
获取接口 1 2 Class[] interfaces = clazz.getInterfaces();
获取包 1 2 Package aPackage = clazz.getPackage();
获取注解 1 2 Annotation[] annotations = clazz.getAnnotations();
反射为属性赋值 1 2 3 4 5 6 Person person = clazz.newInstance(); Field field = clazz.getDeclaredField("age" ); field.setAccessible(true ); field.set(person,20 );
反射执行方法 1 2 3 Method method = clazz.getMethod("display" ); method.invoke(person);