Java Reflect(反射)机制详解

来源:互联网 发布:数据可视化工作 编辑:程序博客网 时间:2024/05/22 20:14

Java Reflect(反射)机制详解

反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

反射机制主要提供了以下功能:
-在运行时判断任意一个对象所属的类;
-在运行时构造任意一个类的对象;
-在运行时判断任意一个类所具有的成员变量和方法;
-在运行时调用任意一个对象的方法;
-生成动态代理。

反射机制的相关API
通过一个对象获得完整的包名和类名

/** * 获得完整的包名和类名 * @author mazaiting */public class Reflect1 {    public static void main(String[] args) {        Reflect1 reflect1 = new Reflect1();        System.out.println("包名: " + reflect1.getClass().getPackage());        System.out.println("类名: " + reflect1.getClass().getName());        System.out.println("简单名: " + reflect1.getClass().getSimpleName());        System.out.println("规范名: " + reflect1.getClass().getCanonicalName());        System.out.println("类型名: " + reflect1.getClass().getTypeName());        /**         * 包名: package com.mazaiting         * 类名: com.mazaiting.Reflect1         * 简单名: Reflect1         * 规范名: com.mazaiting.Reflect1         * 类型名: com.mazaiting.Reflect1         */    }}

实例化Class类对象

/** * 实例化Class类对象 * @author mazaiting */public class Reflect2 {    public static void main(String[] args) throws ClassNotFoundException {        Class<?> class1 = null;        Class<?> class2 = null;        Class<?> class3 = null;                class1 = Class.forName("com.mazaiting.Reflect2");        class2 = new Reflect2().getClass();        class3 = Reflect2.class;                System.out.println("类名: " + class1.getName());        System.out.println("类名: " + class2.getName());        System.out.println("类名: " + class3.getName());        /**         * 类名: com.mazaiting.Reflect2         * 类名: com.mazaiting.Reflect2         * 类名: com.mazaiting.Reflect2         */         }}

获取一个对象的父类与实现的接口

/** * 获取一个对象的父类与实现的接口 * @author mazaiting */public class Reflect3 implements Serializable{    public static void main(String[] args) throws ClassNotFoundException {        Class<?> clazz = Class.forName("com.mazaiting.Reflect3");        Class<?> superclass = clazz.getSuperclass();        System.out.println("clazz的父类: " + superclass.getName());                Class<?>[] interfaces = clazz.getInterfaces();        for (int i = 0; i < interfaces.length; i++){            System.out.println("接口" + i + ": " + interfaces[i].getName());        }        /**         * clazz的父类: java.lang.Object         * 接口0: java.io.Serializable         */         }}

获取某个类中的全部构造函数

/** * 获取某个类中的全部构造函数 * 通过反射机制实例化一个类的对象 * @author mazaiting */public class Reflect4 {    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{        Class<?> clazz = Class.forName("com.mazaiting.User");                Constructor<?>[] constructors = clazz.getConstructors();        System.out.println("构造方法共 " + constructors.length + "个");        for (int i=0;i<constructors.length; i++){            Class<?>[] parameterTypes = constructors[i].getParameterTypes();            System.out.print("cons[" + i + "] (");            for (int j=0; j<parameterTypes.length; j++){                if (j == parameterTypes.length - 1)                    System.out.print(parameterTypes[j].getName());                else                    System.out.print(parameterTypes[j].getName() + ",");            }            System.out.println(")");        }        /**         * 构造方法共 3个         * cons[0] (java.lang.String,int)         * cons[1] (java.lang.String)         * cons[2] ()         */                User user0 = (User) constructors[0].newInstance("mazaiting", 23);        System.out.println(user0.toString());                User user1 = (User) constructors[1].newInstance("mazaiting");        System.out.println(user1.toString());                User user2 = (User) constructors[2].newInstance();        System.out.println(user2.toString());                /**         * User [name=mazaiting, age=23]         * User [name=mazaiting, age=0]         * User [name=null, age=0]         */         }}
public class User {    private String name;    private int age;        public User() {        super();    }    public User(String name) {        super();        this.name = name;    }    public User(String name, int age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public String toString() {        return "User [name=" + name + ", age=" + age + "]";    }   }

获取某个类的全部属性

/** * 获取某个类的全部属性 * @author mazaiting */public class Reflect5 implements Serializable{    private static final long serialVersionUID = -2862585049955236662L;        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{        Class<?> clazz = Class.forName("com.mazaiting.Reflect5");        System.out.println("===============本类属性===============");        // 取得本类的全部属性        Field[] fields = clazz.getDeclaredFields();        for(int i=0;i<fields.length; i++){            // 权限修饰            int modifiers = fields[i].getModifiers();            String mo = Modifier.toString(modifiers);            // 属性类型            Class<?> type = fields[i].getType();            System.out.println(mo + " " + type.getName() + " " + fields[i].getName() + ";");        }                System.out.println("==========实现的接口或者父类的属性==========");                // 取得实现的接口或者父类的属性        Field[] fields2 = clazz.getFields();        for(int i=0;i<fields2.length;i++){            int modifiers = fields2[i].getModifiers();            String mo = Modifier.toString(modifiers);                        Class<?> type = fields2[i].getType();            System.out.println(mo + " " + type.getName() + " " + fields2[i].getName() + ";");        }                /**         * ===============本类属性===============         * private static final long serialVersionUID;         * ==========实现的接口或者父类的属性==========         *          */    }}

获取某个类的全部方法

/** * 获取某个类的全部方法 * @author mazaiting */public class Reflect6 implements Serializable{    private static final long serialVersionUID = -2862585049955236662L;        public static void main(String[] args) throws ClassNotFoundException {        Class<?> clazz = Class.forName("com.mazaiting.User");        // 获取与父类的全部方法,如果要获取当前类的方法所有方法        // 则使用clazz.getDeclaredMethods()方法        Method[] methods = clazz.getMethods();            for(int i=0;i<methods.length;i++){            // 返回类型            Class<?> returnType = methods[i].getReturnType();            int modifiers = methods[i].getModifiers();            // 权限修饰符            String modifier = Modifier.toString(modifiers);            Class<?>[] parameterTypes = methods[i].getParameterTypes();            Class<?>[] exceptionTypes = methods[i].getExceptionTypes();            System.out.println("函数名: " + methods[i].getName());            System.out.println("权限修饰: " + modifier);            System.out.println("返回值: " + returnType.getName());            System.out.println("参数个数: " + parameterTypes.length);            System.out.println("异常个数: " + exceptionTypes.length);            System.out.println("---------------------------------------");        }        /**         * 函数名: toString         * 权限修饰: public         * 返回值: java.lang.String         * 参数个数: 0         * 异常个数: 0         * ---------------------------------------         * 函数名: getName         * 权限修饰: public         * 返回值: java.lang.String         * 参数个数: 0         * 异常个数: 0         * ---------------------------------------         * 函数名: setName         * 权限修饰: public         * 返回值: void         * 参数个数: 1         * 异常个数: 0         * ---------------------------------------         * 函数名: getAge         * 权限修饰: public         * 返回值: int         * 参数个数: 0         * 异常个数: 0         * ---------------------------------------         * 函数名: setAge         * 权限修饰: public         * 返回值: void         * 参数个数: 1         * 异常个数: 0         * ---------------------------------------         * 函数名: wait         * 权限修饰: public final         * 返回值: void         * 参数个数: 0         * 异常个数: 1         * ---------------------------------------         * 函数名: wait         * 权限修饰: public final         * 返回值: void         * 参数个数: 2         * 异常个数: 1         * ---------------------------------------         * 函数名: wait         * 权限修饰: public final native         * 返回值: void         * 参数个数: 1         * 异常个数: 1         * ---------------------------------------         * 函数名: equals         * 权限修饰: public         * 返回值: boolean         * 参数个数: 1         * 异常个数: 0         * ---------------------------------------         * 函数名: hashCode         * 权限修饰: public native         * 返回值: int         * 参数个数: 0         * 异常个数: 0         * ---------------------------------------         * 函数名: getClass         * 权限修饰: public final native         * 返回值: java.lang.Class         * 参数个数: 0         * 异常个数: 0         * ---------------------------------------         * 函数名: notify         * 权限修饰: public final native         * 返回值: void         * 参数个数: 0         * 异常个数: 0         * ---------------------------------------         * 函数名: notifyAll         * 权限修饰: public final native         * 返回值: void         * 参数个数: 0         * 异常个数: 0         * ---------------------------------------         */    }}

通过反射机制调用某个类的方法

/** * 反射机制调用某个类的方法 * @author mazaiting */public class Reflect7 implements Serializable{    private static final long serialVersionUID = -2862585049955236662L;        public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {        Class<?> clazz = Class.forName("com.mazaiting.Reflect7");                // 获取当前类的reflect1方法        Method reflect1 = clazz.getDeclaredMethod("reflect1");        // 执行方法        reflect1.invoke(clazz.newInstance());                Method reflect2 = clazz.getDeclaredMethod("reflect2", int.class, String.class);        reflect2.invoke(clazz.newInstance(), 20, "mazaiting");                  /**         * Java 反射机制 - 调用某个类的方法1.         * Java 反射机制 - 调用某个类的方法2.         * age -> 20. name -> mazaiting         */    }        public void reflect1() {        System.out.println("Java 反射机制 - 调用某个类的方法1.");    }    public void reflect2(int age, String name) {        System.out.println("Java 反射机制 - 调用某个类的方法2.");        System.out.println("age -> " + age + ". name -> " + name);    }}

通过反射机制操作某个类的属性

/** * 反射机制更改属性值 * @author mazaiting */public class Reflect8 implements Serializable{    private static final long serialVersionUID = -2862585049955236662L;    private String proprety = null;    public static void main(String[] args) throws Exception{        // 获取类        Class<?> clazz = Class.forName("com.mazaiting.Reflect8");        // 得到当前类的实例        Reflect8 instance = (Reflect8) clazz.newInstance();        // 获取要更改的属性        Field proprety = clazz.getDeclaredField("proprety");        // 设置可访问        proprety.setAccessible(true);        // 设置属性        proprety.set(instance, "属性");        System.out.println(proprety.get(instance));             /**         * 属性         */    }}

反射机制的动态代理

/** * 反射机制的动态代理 * 在java中有三种类类加载器。 * 1) Bootstrap ClassLoader 此加载器采用c++编写,一般开发中很少见。 * 2) Extension ClassLoader 用来进行扩展类的加载,一般对应的是jrelibext目录中的类 * 3) AppClassLoader 加载classpath指定的类,是最常用的加载器。同时也是java中默认的加载器。 * 如果想要完成动态代理,首先需要定义一个InvocationHandler接口的子类,已完成代理的具体操作。 * @author mazaiting */public class Reflect9 {        public static void main(String[] args) throws Exception{        MyInvocationHandler invocationHandler = new MyInvocationHandler();        Subject sub = (Subject) invocationHandler.bind(new RealSubject());        String info = sub.say("mazaiting", 23);        System.out.println(info);        /**         * mazaiting, 23         */    }}class MyInvocationHandler implements InvocationHandler{    private Object obj = null;    public Object bind(Object obj) {        this.obj = obj;        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),                 obj.getClass().getInterfaces(), this);    }    @Override    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        Object temp = method.invoke(obj, args);        return temp;    }    }class RealSubject implements Subject{    @Override    public String say(String name, int age) {        return name + ", " + age;    }    }interface Subject{    String say(String name, int age);}

实例
在泛型为Integer的ArrayList中存放一个String类型的对象。

/** * 在泛型为Integer的ArrayList中存放一个String类型的对象。 * @author mazaiting */public class Reflect10 {        public static void main(String[] args) throws Exception{                ArrayList<Integer> list = new ArrayList<>();                Method method = list.getClass().getDeclaredMethod("add", Object.class);                method.invoke(list, "Java反射机制实例");                System.out.println(list.get(0));        /**         * Java反射机制实例         */    }}

通过反射取得并修改数组的信息

/** * 反射取得并修改数组的信息 * @author mazaiting */public class Reflect11 {        public static void main(String[] args) throws Exception{        int[] temp = {1, 2, 3, 4, 5};        Class<?> componentType = temp.getClass().getComponentType();        System.out.println("数组类型: " + componentType.getName());        System.out.println("数组长度: " + Array.getLength(temp));        System.out.println("数组的第一个元素: " + Array.get(temp, 0));        // 设置第一个元素数据为100        Array.set(temp, 0, 100);        System.out.println("修改之后数组第一个元素为: " + Array.get(temp, 0));    }}

通过反射机制修改数组的大小

/** * 反射机制修改数组的大小 * @author mazaiting */public class Reflect12 {        public static void main(String[] args) throws Exception {        int[] temp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };        int[] newTemp = (int[]) arrayInc(temp, 15);        print(newTemp);        String[] atr = { "a", "b", "c" };        String[] str1 = (String[]) arrayInc(atr, 8);        print(str1);        /**         * 数组长度为: 15         * 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0          * 数组长度为: 8         * a b c null null null null null          */    }    // 修改数组大小    public static Object arrayInc(Object obj, int len) {        Class<?> arr = obj.getClass().getComponentType();        Object newArr = Array.newInstance(arr, len);        int co = Array.getLength(obj);        System.arraycopy(obj, 0, newArr, 0, co);        return newArr;    }    // 打印    public static void print(Object obj) {        Class<?> c = obj.getClass();        if (!c.isArray()) {            return;        }        System.out.println("数组长度为: " + Array.getLength(obj));        for (int i = 0; i < Array.getLength(obj); i++) {            System.out.print(Array.get(obj, i) + " ");        }        System.out.println();    }}

将反射机制应用于工厂模式

/** * 反射机制应用于工厂模式 * @author mazaiting */public class Reflect13 {        public static void main(String[] args) throws Exception {//      Fruit f = Factory.getInstance("com.mazaiting.Orange");        Fruit f = Factory.getInstance("com.mazaiting.Apple");        if (f != null) {            f.eat();        }        /**         * Apple         */    }    }interface Fruit {    public abstract void eat();}class Apple implements Fruit {    public void eat() {        System.out.println("Apple");    }}class Orange implements Fruit {    public void eat() {        System.out.println("Orange");    }}class Factory {    public static Fruit getInstance(String ClassName) {        Fruit f = null;        try {            f = (Fruit) Class.forName(ClassName).newInstance();        } catch (Exception e) {            e.printStackTrace();        }        return f;    }}

代码下载