集册 Java实例教程 复制Java Bean的属性

复制Java Bean的属性

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

642
复制Java Bean的属性
/** from 
时 代 J     a    v  a - nowjava.com**/


import java.beans.BeanInfo;

import java.beans.IndexedPropertyDescriptor;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Array;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;


public class Main{

    private static final String INDEXED_DELIM = "[";

    private static final String INDEXED_DELIM2 = "]";/**来自 N o  w  J a v a . c o m - 时  代  Java**/

    private static final String MAPPED_DELIM = "(";

    private static final String MAPPED_DELIM2 = ")";

    private static final String NESTED_DELIM = ".";

    public static void copyProperty(Object fromBean, String name,

            Object toBean) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        Object value = getNestedProperty(fromBean, name);

        setNestedProperty(toBean, name, value);

    }

    public static Object getNestedProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        String[] properties = name.split("\\" + NESTED_DELIM);

        for (int i = 0; i < properties.length; i++) {

            if (properties[i].contains(INDEXED_DELIM)) {

                bean = getIndexedProperty(bean, properties[i]);

                continue;

            }

            if (properties[i].contains(MAPPED_DELIM)) {

                bean = getMappedProperty(bean, properties[i]);

                continue;

            }

            bean = getSimpleProperty(bean, properties[i]);

        }

        return bean;

    }

    public static void setNestedProperty(Object bean, String name,

            Object value) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        String[] properties = name.split("\\" + NESTED_DELIM);

        for (int i = 0; i < properties.length - 1; i++) {

            if (properties[i].contains(INDEXED_DELIM)) {

                bean = getIndexedProperty(bean, properties[i]);

                continue;

            }

            if (properties[i].contains(MAPPED_DELIM)) {

                bean = getMappedProperty(bean, properties[i]);

                continue;

            }

            bean = getSimpleProperty(bean, properties[i]);

        }


        if (properties[properties.length - 1].contains(INDEXED_DELIM)) {

            setIndexedProperty(bean, properties[properties.length - 1],

                    value);

        }

        if (properties[properties.length - 1].contains(MAPPED_DELIM)) {

            setMappedProperty(bean, properties[properties.length - 1],

                    value);

        }

        setSimpleProperty(bean, properties[properties.length - 1], value);


    }

    public static Object getIndexedProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        int delim = name.indexOf(INDEXED_DELIM);

        int delim2 = name.indexOf(INDEXED_DELIM2);

        if ((delim < 0) || (delim2 <= delim))

            throw new IllegalArgumentException("Invalid indexed property '"

                    + name + "'");

        int index = -1;

        try {

            String subscript = name.substring(delim + 1, delim2);

            index = Integer.parseInt(subscript);

        } catch (NumberFormatException e) {

            throw new IllegalArgumentException("Invalid indexed property '"

                    + name + "'");

        }

        name = name.substring(0, delim);

        return getIndexedProperty(bean, name, index);

    }

    public static Object getIndexedProperty(Object bean, String name,

            int index) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,

                name);

        if (propertyDescriptor == null)

            throw new IllegalArgumentException("No property:" + name);

        if (propertyDescriptor instanceof IndexedPropertyDescriptor) {

            Method readMethod = ((IndexedPropertyDescriptor) propertyDescriptor)

                    .getIndexedReadMethod();

            if (readMethod == null)

                throw new IllegalArgumentException(

                        "No readMethod for property:" + name);

            Object value = readMethod.invoke(bean,

                    new Object[] { new Integer(index) });

            return value;

        }

        Method readMethod = propertyDescriptor.getReadMethod();

        if (readMethod == null)

            throw new IllegalArgumentException(

                    "No readMethod for property:" + name);

        Object array = readMethod.invoke(bean, new Object[0]);

        if (array.getClass().isArray()) {

            return Array.get(array, index);

        }

        if (!(array instanceof java.util.List))

            throw new IllegalArgumentException("Property '" + name

                    + "' is not indexed");

        return ((java.util.List<?>) array).get(index);

    }

    public static Object getMappedProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        int delim = name.indexOf(MAPPED_DELIM);

        int delim2 = name.indexOf(MAPPED_DELIM2);

        if ((delim < 0) || (delim2 <= delim))

            throw new IllegalArgumentException("Invalid mapped property '"

                    + name + "'");

        String subscript = name.substring(delim + 1, delim2);

        name = name.substring(0, delim);

        return getMappedProperty(bean, name, subscript);

    }

    public static Object getMappedProperty(Object bean, String name,

            String key) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,

                name);

        if (propertyDescriptor == null)

            throw new IllegalArgumentException("No property:" + name);

        Method readMethod = propertyDescriptor.getReadMethod();

        if (readMethod == null)

            throw new IllegalArgumentException(

                    "No readMethod for property:" + name);

        Object map = readMethod.invoke(bean, new Object[0]);

        if (!(map instanceof java.util.Map))

            throw new IllegalArgumentException("Property '" + name

                    + "' is not mapped");

        return ((java.util.Map<?, ?>) map).get(key);

    }

    public static Object getSimpleProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        Method readMethod = getSimplePropertyReadMethod(bean, name);

        if (readMethod == null)

            throw new IllegalArgumentException(

                    "No readMethod for property:" + name);

        readMethod.setAccessible(true);

        Object value = readMethod.invoke(bean, new Object[0]);

        return value;

    }

    public static void setIndexedProperty(Object bean, String name,

            Object value) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        int delim = name.indexOf(INDEXED_DELIM);

        int delim2 = name.indexOf(INDEXED_DELIM2);

        if ((delim < 0) || (delim2 <= delim))

            throw new IllegalArgumentException("Invalid indexed property '"

                    + name + "'");

        int index = -1;

        try {

            String subscript = name.substring(delim + 1, delim2);

            index = Integer.parseInt(subscript);

        } catch (NumberFormatException e) {

            throw new IllegalArgumentException("Invalid indexed property '"

                    + name + "'");

        }

        name = name.substring(0, delim);

        setIndexedProperty(bean, name, index, value);

    }

    @SuppressWarnings({ "unchecked", "rawtypes" })

    public static void setIndexedProperty(Object bean, String name,

            int index, Object value) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,

                name);

        if (propertyDescriptor == null)

            throw new IllegalArgumentException("No property:" + name);

        if (propertyDescriptor instanceof IndexedPropertyDescriptor) {

            Method writeMethod = ((IndexedPropertyDescriptor) propertyDescriptor)

                    .getIndexedWriteMethod();

            if (writeMethod == null)

                throw new IllegalArgumentException(

                        "No writeMethod for property:" + name);

            writeMethod.invoke(bean, new Object[] { new Integer(index),

                    value });

            return;

        }

        Method readMethod = propertyDescriptor.getReadMethod();

        if (readMethod == null)

            throw new IllegalArgumentException(

                    "No readMethod for property:" + name);

        Object array = readMethod.invoke(bean, new Object[0]);

        if (array.getClass().isArray()) {

            Array.set(array, index, value);

            return;

        }

        if (!(array instanceof java.util.List))

            throw new IllegalArgumentException("Property '" + name

                    + "' is not indexed");

        ((java.util.List) array).set(index, value);

    }

    public static void setMappedProperty(Object bean, String name,

            Object value) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        int delim = name.indexOf(MAPPED_DELIM);

        int delim2 = name.indexOf(MAPPED_DELIM2);

        if ((delim < 0) || (delim2 <= delim))

            throw new IllegalArgumentException("Invalid mapped property '"

                    + name + "'");

        String subscript = name.substring(delim + 1, delim2);

        name = name.substring(0, delim);

        setMappedProperty(bean, name, subscript, value);

    }

    @SuppressWarnings({ "rawtypes", "unchecked" })

    public static void setMappedProperty(Object bean, String name,

            String key, Object value) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,

                name);

        if (propertyDescriptor == null)

            throw new IllegalArgumentException("No property:" + name);

        Method readMethod = propertyDescriptor.getReadMethod();

        if (readMethod == null)

            throw new IllegalArgumentException(

                    
展开阅读全文