集册 Java实例教程 用二传手设置字段链值

用二传手设置字段链值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

469
用二传手设置字段链值


import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;/*N o w J a v a . c o m - 时代Java*/

import java.lang.reflect.Method;

import java.util.Collection;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

import java.util.logging.Level;

import java.util.logging.Logger;


public class Main{

    public static void main(String[] argv) throws Exception{

        Object baseObject = "nowjava.com";

        String fieldNameChain = "nowjava.com";

        Object value = "nowjava.com";

        setFieldChainValueWithSetter(baseObject,fieldNameChain,value);

    }

    private static Logger log = Logger.getLogger(BeanUtil.class

            .getSimpleName());

    public static void setFieldChainValueWithSetter(Object baseObject,

            String fieldNameChain, Object value) {/**来 自 时   代     Java  公  众  号 - nowjava.com**/

        int last = fieldNameChain.lastIndexOf(".");

        if (last != -1)

            baseObject = getFieldChainValueWithGetters(

                    fieldNameChain.subSequence(0, last), fieldNameChain);

        try {

            setFieldValueWithSetter(baseObject,

                    fieldNameChain.substring((last == -1) ? 0 : last),

                    value);

        } catch (Throwable e) {

            log.log(Level.SEVERE, "Unable to set value.", e);

        }

    }

    public static Object getFieldChainValueWithGetters(Object baseObject,

            String fieldNameChain) {

        String[] chain = fieldNameChain.split("[.]");

        if (chain.length == 1)

            return getFieldValueWithGetter(baseObject, chain[0]);


        return getFieldChainValueWithGetters(

                getFieldValueWithGetter(baseObject, chain[0]),

                fieldNameChain.substring(chain[0].length() + 1));

    }

    public static void setFieldValueWithSetter(Object obj,

            String fieldName, Object value) throws NoSuchMethodException,

            InvocationTargetException, IllegalAccessException {

        Method method = getSetterMethodForField(obj,

                getSetterMethodNameForField(fieldName), value.getClass());

        method.invoke(obj, value);

    }

    public static Object getFieldValueWithGetter(Object obj, Field field) {

        try {

            Method m = getGetterMethodForField(obj, field);

            m.setAccessible(true);

            return m.invoke(obj);

        } catch (NoSuchMethodException e) {

            log.log(Level.INFO, "No getter for field: " + field.getName()

                    + ", using field it's self", e);

            try {

                field.setAccessible(true);

                return field.get(obj);

            } catch (Throwable iae) {

                log.log(Level.SEVERE,

                        "Unable to access field even after enabling access.",

                        iae);

            }

        } catch (InvocationTargetException e) {

            log.log(Level.SEVERE,

                    "Unable to invoke getter on supplied object: " + obj

                            + ", field: " + field.getName(), e);

        } catch (IllegalAccessException e) {

            log.log(Level.SEVERE,

                    "Access not allowed on explicitly marked accessible field through reflection.",

                    e);

        }

        return null;

    }

    public static Object getFieldValueWithGetter(Object obj,

            String fieldName) {

        try {

            return getFieldValueWithGetter(obj, obj.getClass()

                    .getDeclaredField(fieldName));

        } catch (NoSuchFieldException e) {

            log.log(Level.SEVERE, "Unable to find field on object", e);

            return null;

        }

    }

    public static Method getSetterMethodForField(Object obj,

            String fieldName, Class type) throws NoSuchMethodException {

        return obj.getClass().getDeclaredMethod(fieldName, type);

    }

    public static String getSetterMethodNameForField(String fieldName) {

        return "set" + fieldName.substring(0, 1).toUpperCase()

                + fieldName.substring(1);

    }

    public static Method getGetterMethodForField(Object obj, Field field)

            throws NoSuchMethodException {

        try {

            return obj.getClass().getDeclaredMethod(

            
展开阅读全文