1、定义
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
2、用途
在日常的第三方应用开发过程中,经常会遇到某个类的某个成员变量、方法或是属性是私有的或是只对系统应用开放,这时候就可以利用Java的反射机制通过反射来获取所需的私有成员或是方法。当然,也不是所有的都适合反射,之前就遇到一个案例,通过反射得到的结果与预期不符。阅读源码发现,经过层层调用后在最终返回结果的地方对应用的权限进行了校验,对于没有权限的应用返回值是没有意义的缺省值,否则返回实际值起到保护用户的隐私目的。
3、与Java反射相关的类如下:
类名用途Class类代表类的实体,在运行的Java应用程序中表示类和接口Field类代表类的成员变量(成员变量也称为类的属性)Method类代表类的方法Constructor类代表类的构造方法
4、Class类
Class代表类的实体,在运行的Java应用程序中表示类和接口。在这个类中提供了很多有用的方法,这里对他们简单的分类介绍。
- 获得类相关的方法
方法用途asSubclass(Class clazz)把传递的类的对象转换成代表其子类的对象Cast把对象转换成代表类或是接口的对象getClassLoader()获得类的加载器getClasses()返回一个数组,数组中包含该类中所有公共类和接口类的对象getDeclaredClasses()返回一个数组,数组中包含该类中所有类和接口类的对象forName(String className)根据类名返回类的对象getName()获得类的完整路径名字newInstance()创建类的实例getPackage()获得类的包getSimpleName()获得类的名字getSuperclass()获得当前类继承的父类的名字getInterfaces()获得当前类实现的类或是接口
- 获得类中属性相关的方法
方法用途getField(String name)获得某个公有的属性对象getFields()获得所有公有的属性对象getDeclaredField(String name)获得某个属性对象getDeclaredFields()获得所有属性对象
- 获得类中注解相关的方法
方法用途getAnnotation(Class annotationClass)返回该类中与参数类型匹配的公有注解对象getAnnotations()返回该类所有的公有注解对象getDeclaredAnnotation(Class annotationClass)返回该类中与参数类型匹配的所有注解对象getDeclaredAnnotations()返回该类所有的注解对象
- 获得类中构造器相关的方法
方法用途getConstructor(Class...> parameterTypes)获得该类中与参数类型匹配的公有构造方法getConstructors()获得该类的所有公有构造方法getDeclaredConstructor(Class...> parameterTypes)获得该类中与参数类型匹配的构造方法getDeclaredConstructors()获得该类所有构造方法
- 获得类中方法相关的方法
方法用途getMethod(String name, Class...> parameterTypes)获得该类某个公有的方法getMethods()获得该类所有公有的方法getDeclaredMethod(String name, Class...> parameterTypes)获得该类某个方法getDeclaredMethods()获得该类所有方法
- 类中其他重要的方法
方法用途isAnnotation()如果是注解类型则返回trueisAnnotationPresent(Class extends Annotation> annotationClass)如果是指定类型注解类型则返回trueisAnonymousClass()如果是匿名类则返回trueisArray()如果是一个数组类则返回trueisEnum()如果是枚举类则返回trueisInstance(Object obj)如果obj是该类的实例则返回trueisInterface()如果是接口类则返回trueisLocalClass()如果是局部类则返回trueisMemberClass()如果是内部类则返回true
5、Field类
Field代表类的成员变量(成员变量也称为类的属性)。
方法用途equals(Object obj)属性与obj相等则返回trueget(Object obj)获得obj中对应的属性值set(Object obj, Object value)设置obj中对应属性值
6、Method类
Method代表类的方法。
方法用途invoke(Object obj, Object... args)传递object对象及参数调用该对象对应的方法
7、Constructor类
Constructor代表类的构造方法。
方法用途newInstance(Object... initargs)根据传递的参数创建类的对象
8、实例演示
package com.lj;import java.lang.annotation.Annotation;import java.lang.reflect.*;public class User { private String name; private Integer age; private Double income; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getIncome() { return income; } public void setIncome(Double income) { this.income = income; } public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { User user = new User(); user.setAge(20); user.setName("张三"); Class clazz=User.class;//获取Class对象方式1 Class clazz2=Class.forName("com.lj.User");//获取Class对象方式2 String clazzName=clazz.getName();//获取类名,含包名 String clazzSimpleName=clazz.getSimpleName();//获取类名,不含包名 System.out.println("带包名的类名是:"+clazzName+" 类名是:"+clazzSimpleName); int mod=clazz.getModifiers();//获取类修饰符 System.out.println("类修饰符isPublic:"+ Modifier.isPublic(mod));//判断类修饰符 System.out.println("类修饰符isProtected:"+Modifier.isProtected(mod));//判断类修饰符 Package p=clazz.getPackage();//获取包 System.out.println("包名是:"+p); Class superClass=clazz.getSuperclass();//获取父类 System.out.println("父类是:"+superClass); Class[] interfaces=clazz.getInterfaces();//获取实现接口 System.out.println("实现的接口数量:"+interfaces.length); Constructor[] constructors = clazz.getConstructors();//构造方法 System.out.println("构造器的数量:"+constructors.length); Method[] methods1=clazz.getMethods();//获取所有方法 System.out.println("这个类所有的方法数量:"+methods1.length); for(Method method:methods1){ System.out.println("这个类所有的方法的名字:"+method); } System.out.println("=====================1====================="); Method[] methods=clazz.getDeclaredMethods();//获取私有方法 System.out.println("这个类所有的私有方法数量:"+methods.length); for(Method method:methods){ System.out.println("这个类所有的私有方法名字:"+method.getName()); } System.out.println("=====================2====================="); Method method=clazz.getMethod("getName");//获取指定方法。传入方法名,可以获得这个方法的对象 System.out.println("根据方法名获得这个方法的对象:"+method); Object methodVlaue=method.invoke(user, null);//调用方法(反射调用方法) System.out.println("反射调用方法:"+methodVlaue); System.out.println("=====================3====================="); Method method3=clazz.getMethod("setIncome", Double.class);//获取指定方法 System.out.println("根据方法名获得这个方法的对象:"+method3); Object methodVlaue3=method3.invoke(user, 5000.00);//调用setter方法,该方法没有返回值,所以methodVlaue3为null;此处注意参数5000.00 ,不能用null System.out.println("method.invoke(,):"+methodVlaue3); System.out.println("通过反射已经将5000.00设置给user了,打印一下user.getIncome:"+user.getIncome()); System.out.println("=====================4====================="); Field[] fields=clazz.getDeclaredFields();//获取变量 System.out.println("获取变量:"+fields.length); for(Field field:fields){ field.setAccessible(true); field.set(user,null);//设置字段的值 System.out.println("field.getAnnotations:"+field.getAnnotations().length+" field.getName:"+field.getName()+"field.get:"+field.get(user));//获取实例属性名和值 } Annotation[] annos=clazz.getAnnotations();//获取类注解 System.out.println("类注解个数:"+annos.length); }}
9、打印结果
带包名的类名是:com.lj.User 类名是:User类修饰符isPublic:true类修饰符isProtected:false包名是:package com.lj父类是:class java.lang.Object实现的接口数量:0构造器的数量:1这个类所有的方法数量:16这个类所有的方法的名字:public static void com.lj.User.main(java.lang.String[]) throws java.lang.ClassNotFoundException,java.lang.IllegalArgumentException,java.lang.IllegalAccessException,java.lang.NoSuchMethodException,java.lang.reflect.InvocationTargetException这个类所有的方法的名字:public java.lang.String com.lj.User.getName()这个类所有的方法的名字:public void com.lj.User.setName(java.lang.String)这个类所有的方法的名字:public java.lang.Integer com.lj.User.getAge()这个类所有的方法的名字:public void com.lj.User.setAge(java.lang.Integer)这个类所有的方法的名字:public java.lang.Double com.lj.User.getIncome()这个类所有的方法的名字:public void com.lj.User.setIncome(java.lang.Double)这个类所有的方法的名字:public final void java.lang.Object.wait() throws java.lang.InterruptedException这个类所有的方法的名字:public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException这个类所有的方法的名字:public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException这个类所有的方法的名字:public boolean java.lang.Object.equals(java.lang.Object)这个类所有的方法的名字:public java.lang.String java.lang.Object.toString()这个类所有的方法的名字:public native int java.lang.Object.hashCode()这个类所有的方法的名字:public final native java.lang.Class java.lang.Object.getClass()这个类所有的方法的名字:public final native void java.lang.Object.notify()这个类所有的方法的名字:public final native void java.lang.Object.notifyAll()=====================1=====================这个类所有的私有方法数量:7这个类所有的私有方法名字:main这个类所有的私有方法名字:getName这个类所有的私有方法名字:setName这个类所有的私有方法名字:getAge这个类所有的私有方法名字:setAge这个类所有的私有方法名字:getIncome这个类所有的私有方法名字:setIncome=====================2=====================根据方法名获得这个方法的对象:public java.lang.String com.lj.User.getName()反射调用方法:张三=====================3=====================根据方法名获得这个方法的对象:public void com.lj.User.setIncome(java.lang.Double)method.invoke(,):null通过反射已经将5000.00设置给user了,打印一下user.getIncome:5000.0=====================4=====================获取变量:3field.getAnnotations:0 field.getName:name field.get:nullfield.getAnnotations:0 field.getName:age field.get:nullfield.getAnnotations:0 field.getName:income field.get:null类注解个数:0
![aa4d1c82909fb780fefb455ed45866aa.png](https://img-blog.csdnimg.cn/img_convert/aa4d1c82909fb780fefb455ed45866aa.png)
喜欢可以搜索或扫码,关注我的微信公众号——“java控”