一篇文章带你读懂反射(一)

278 阅读5分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的6天,点击查看活动详情

反射

一.反射机制

1.一个需求引出反射

  • 请看下面的问题 1.根据配置文件re.properties指定信息,创建对象并调用方法

    classfullpath=com.hspedu.Cat method=hi

  • 思考:使用现有技术,你能做的吗? 2.这样的需求在学习框架时特别多,即通过外部文件配置,在不修改源码情况下,来控制程序,也符合设计模式的ocp原则(开闭原则)

    3.快速入门com.gbx.reflection.question ReflectionQuestion.java

/**
 * @author LeeZhi
 * @version 1.0
 * 反射问题的引入
 */
@SuppressWarnings({"all"})
public class ReflectionQuestion {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
​
        //根据配置文件re.properties指定信息,创建Cat对象并调用方法hi
        //回忆
​
        //传统的方式  new 对象 -> 调用方法
        //Cat cat = new Cat();
        //cat.hi();====>cat.cry()  修改源码
​
        //我们尝试用io做一下 -> 明白反射
​
        //1.使用properties类,可以读写配置文件
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\re.properties"));
        String classfullpath = properties.getProperty("classfullpath").toString();//"com.hspedu.Cat"
        String method = properties.get("method").toString();//"hi"
        System.out.println("classfullpath:" + classfullpath +"\n"+ "method:" + method);
​
        //2.创建对象,传统的方法,行不通  => 反射机制
        //Cat cat2 = new com.gbx.Cat();//new classfullpath()  -> String
​
        //3.使用反射机制解决
        //(1),返回Class类型的对象
        Class cls = Class.forName(classfullpath);
        //(2)通过 cls 得到你加载的类com.gbx.Cat() 的对象的实例
        Cat o = (Cat)cls.newInstance();
        //(3) 通过cls得到修加载的类com.gbx.Cat 的 methodName"hi" 的方法对象
        //    即:在反射中,可以把方法视为对象(万物皆对象)
        Method method1 = cls.getMethod(method);
        //(4) 通过method1 调用方法:即通过方法对象来实现调用方法
        System.out.println("===================================");
        method1.invoke(o);//传统方法 对象.方法 , 反射机制  方法.invoke(对象)
    }
}

1.反射机制允许程序在执行期借助于Reflection API取得任何类的内部信息(比如成员变量,构造器,成员方法等等),并能操作对象的属性及方法。反射在设计模式和框架底层都会用到

2.加载完类之后,在堆中就产生了一个Class类型的对象(一个类只有一个Classi对象),这个对象包含了类的完整结构信息。通过这个对象得到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以,形象的称之为:反射

p对象-->类型Person类 对象cls-->类型Class类

2.反射原理图

2022-11-18

3.反射相关类

  • Java反射机制可以完成

    1. 在运行时判断任意一个对象所属的类
    2. 在运行时构造任意一个类的对象
    3. 在运行时得到任意一个类所具有的成员变量和方法
    4. 在运行时调用任意一个对象的成员变量和方法
    5. 生成动态代理
  • 反射相关的主要类

    1. java.lang.Class:代表一个类,Class对象表示某个类加载后在堆中的对象
    2. java.lang.reflect.Method:代表类的方法,Method对象表示某个类的方法
    3. java.lang.reflect.Field:代表类的成员变量,Field对象表示某个类的成员变量
    4. java.lang.reflect.Constructor:代表类的构造方法,Constructor对象表示构造器

    这些类在java.lang.reflection包内

public class Reflection01 {
​
    public static void main(String[] args) throws Exception {
        //1.使用properties类,可以读写配置文件
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\re.properties"));
        String classfullpath = properties.getProperty("classfullpath").toString();//"com.gbx.Cat"
        String method = properties.get("method").toString();//"hi"
​
        //2.使用反射机制解决
        //(1),返回Class类型的对象
        Class cls = Class.forName(classfullpath);
        //(2)通过 cls 得到你加载的类com.gbx.Cat() 的对象的实例
        Cat o = (Cat)cls.newInstance();
        //(3) 通过cls得到修加载的类com.gbx.Cat 的 methodName"hi" 的方法对象
        //    即:在反射中,可以把方法视为对象(万物皆对象)
        Method method1 = cls.getMethod(method);
        //(4) 通过method1 调用方法:即通过方法对象来实现调用方法
        System.out.println("===================================");
        method1.invoke(o);//传统方法 对象.方法 , 反射机制  方法.invoke(对象)
​
        //java.lang.reflect.Field:代表类的成员变量,Field对象表示某个类的成员变量
        //得到name字段
        //getField不能得到私有属性
        Field nameField = cls.getField("age");//
        System.out.println(nameField.get(o));       //传统写法  对象.成员变量 , 反射 : 成员变量对象.get(对象)
​
        //java.lang.reflect.Constructor:代表类的构造方法,Constructor对象表示构造器
        Constructor constructor1 = cls.getConstructor();//()中可以指定构造器参数类型,返回无参构造器
        System.out.println(constructor1);
        
        Constructor constructor2 = cls.getConstructor(String.class);  //这里传入的String.class就是String类的Class对象
        System.out.println(constructor2);
    }
}
​
    public int age = 2;//public的
​
    public Cat(){}//无参构造器
    public Cat(String name){
        this.name = name;
    }

4.反射调优

4.1反射的优缺点

优点可以动态的创建和使用对象(也是框架底层核心),使用灵活,没有反射机制,框架技术就失去底层支撑。
缺点使用反射基本是解释执行,对执行速度有影呵,
/**
 * @author LeeZhi
 * @version 1.0
 * 测试反射调用的性能,和优化方案
 */
public class Reflection02 {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
        m1();
        m2();
    }
    //传统方法调用hi
    public static void m1(){
        Cat cat = new Cat();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            cat.hi();
        }
        long end = System.currentTimeMillis();
        System.out.println("m1方法耗时="+(end-start));
    }
    //反射机制调用方法
    public static void m2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class cls = Class.forName("com.gbx.Cat");
        Object o = cls.newInstance();
        Method hi = cls.getMethod("hi");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            hi.invoke(o);//反射调用方法
        }
        long end = System.currentTimeMillis();
        System.out.println("m2方法耗时="+(end-start));
    }
}
​

image-20221120195927601

  • 反射调用优化-关闭访问检查

    1. Method和Field、Constructor对象都有setAccessible()方法
    2. setAccessible作用是启动和禁用访问安全检查的开关
    3. 参数值为tue表示反射的对象在使用时取消访问检查,提高反射的效率。参数值为false则表示反射的对象执行访问检查

image-20221120200242934

 //反射调优优化 + 关闭访问检查
    public static void m3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class cls = Class.forName("com.gbx.Cat");
        Object o = cls.newInstance();
        Method hi = cls.getMethod("hi");
        hi.setAccessible(true);//在反射调用方法时,取消访问检查
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            hi.invoke(o);//反射调用方法
        }
        long end = System.currentTimeMillis();
        System.out.println("m3方法耗时="+(end-start));
    }

image-20221120200907635