java 反射取值(Java reflection value)

反射就是把Java类中的各个成分映射成一个个的Java对象。即在运行状态中,对于任意一个类,都能够知道这个类的所以属性和方法;对于任意一个对象,都能调用它的任意一个方法和属性。这种动态获取信息及动态调用对象方法的功能叫Java的反射机制

反射取值一般有两种方式

一 通过属性取值赋值

    /**
     * 属性取值
     * @param t 实例
     * @param field 取值字段
     * @return
     */
    public String getFieldValueByClass(T t, String field) {
        Class<?> aClass = t.getClass();
        try {
            Field f = aClass.getDeclaredField(field);
            f.setAccessible(true);
            return (String) f.get(t);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 属性赋值
     * @param t 实例
     * @param field 赋值的字段
     * @param value 赋的值
     * @return
     */
    public String setFieldValueByClass(T t,String field,String value) {

        Class<?> aClass = t.getClass();
        Field f = null;
        try {
            f = aClass.getDeclaredField(field);
            f.setAccessible(true);
            f.set(t, value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return "";
    }

二 通过方法取值赋值

案列是针对属性的set,get方法,如果是自定义方法,字段参数直接传方法名,去掉手动拼接setXxx,getXxx那一块代码即可,原理不变。

 /**
     * 反射取值
     * @param t 实例对象
     * @param field 取值字段
     * @return
     */
    public String getMethodValueByClass(T t,String field) {

        //将字段首字母大写并取出 x > X
        String firstUpper = String.valueOf(field.charAt(0)).toUpperCase();

        //利用StringBuilder 特有的 replace 方法转换将 xxx > Xxx
        StringBuilder sb = new StringBuilder(field);
        sb.replace(0,1,firstUpper);

        //拼接新的字段 getXxx
        field = "get"+sb.toString();

        try {
            //取值
            Class clazz = t.getClass();
            return clazz.getMethod(field).invoke(t).toString();

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 方法赋值
     * @param t 实例对象
     * @param field 字段
     * @param value 赋的值
     * @return
     */
    public String setMethodValueByClass(T t, String field, String value) {

        String s1 = String.valueOf(field.charAt(0)).toUpperCase();
        StringBuilder sb = new StringBuilder(field);
        sb.replace(0, 1, s1);

        field = "set" + sb.toString();

        try {
            Class clazz = t.getClass();

            //设置方法参数String, 可设置多个参数,clazz.getMethod(field, String.class,Integer.class....) 等对应你属性里面形参即可
            Method method = clazz.getMethod(field, String.class);

            //填充值,对应以上参数个数即可
            return method.invoke(t, value).toString();

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "";
    }

~~~///(^v^)\\\~~~

————————

Reflection is to map the components of a Java class into Java objects one by one. That is, in the running state, you can know all the properties and methods of any class; For any object, you can call any of its methods and properties. This function of dynamically obtaining information and dynamically calling object methods is called java reflection mechanism

< strong > there are generally two ways to get reflection values < / strong >

I. assignment through attribute value

    /**
     * 属性取值
     * @param t 实例
     * @param field 取值字段
     * @return
     */
    public String getFieldValueByClass(T t, String field) {
        Class<?> aClass = t.getClass();
        try {
            Field f = aClass.getDeclaredField(field);
            f.setAccessible(true);
            return (String) f.get(t);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 属性赋值
     * @param t 实例
     * @param field 赋值的字段
     * @param value 赋的值
     * @return
     */
    public String setFieldValueByClass(T t,String field,String value) {

        Class<?> aClass = t.getClass();
        Field f = null;
        try {
            f = aClass.getDeclaredField(field);
            f.setAccessible(true);
            f.set(t, value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return "";
    }

II. Value assignment by method

The case list refers to the set and get methods for attributes. If it is a custom method, the field parameters are directly passed to the method name, and the code of manually splicing setXXX and getxxx can be removed. The principle remains unchanged.

 /**
     * 反射取值
     * @param t 实例对象
     * @param field 取值字段
     * @return
     */
    public String getMethodValueByClass(T t,String field) {

        //将字段首字母大写并取出 x > X
        String firstUpper = String.valueOf(field.charAt(0)).toUpperCase();

        //利用StringBuilder 特有的 replace 方法转换将 xxx > Xxx
        StringBuilder sb = new StringBuilder(field);
        sb.replace(0,1,firstUpper);

        //拼接新的字段 getXxx
        field = "get"+sb.toString();

        try {
            //取值
            Class clazz = t.getClass();
            return clazz.getMethod(field).invoke(t).toString();

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 方法赋值
     * @param t 实例对象
     * @param field 字段
     * @param value 赋的值
     * @return
     */
    public String setMethodValueByClass(T t, String field, String value) {

        String s1 = String.valueOf(field.charAt(0)).toUpperCase();
        StringBuilder sb = new StringBuilder(field);
        sb.replace(0, 1, s1);

        field = "set" + sb.toString();

        try {
            Class clazz = t.getClass();

            //设置方法参数String, 可设置多个参数,clazz.getMethod(field, String.class,Integer.class....) 等对应你属性里面形参即可
            Method method = clazz.getMethod(field, String.class);

            //填充值,对应以上参数个数即可
            return method.invoke(t, value).toString();

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "";
    }

~~~///(^v^)\\\~~~