Java反射(一)反射机制

反射

一.反射机制

1.一个需求引出反射

  • 请看下面的问题 1.根据配置文件 re.properties 指定信息,创建对象并调用方法
  • classfullpath=com.hspedu.Catmethod=hi
  • 思考:1.使用现有技术,你能做的吗?

2.这样的需求在学习框架时特别多,即通过外部文件配置,在不修改源码情况下,来控制程序,也符合设计模式的 ocp 原则(开闭原则)

3.快速入门 com.gbx.reflection.questionReflectionQuestion.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.反射原理图

3.反射相关类

  • Java 反射机制可以完成
  • 在运行时判断任意一个对象所属的类
  • 在运行时构造任意一个类的对象
  • 在运行时得到任意一个类所具有的成员变量和方法
  • 在运行时调用任意一个对象的成员变量和方法
  • 生成动态代理
  • 反射相关的主要类
  • java.lang.Class:代表一个类,Class 对象表示某个类加载后在堆中的对象
  • java.lang.reflect.Method:代表类的方法,Method 对象表示某个类的方法
  • java.lang.reflect.Field:代表类的成员变量,Field 对象表示某个类的成员变量
  • 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.hspedu.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));
    }
}

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

 //反射调优优化 + 关闭访问检查
    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));
    }

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
Java反射(一)反射机制
2.这样的需求在学习框架时特别多,即通过外部文件配置,在不修改源码情况下,来控制程序,也符合设计模式的 ocp 原则(开闭原则)
<<上一篇
下一篇>>