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

从Java Bean复制属性

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

548
从Java Bean复制属性


import java.beans.BeanInfo;

import java.beans.IntrospectionException;//来自 nowjava - 时代Java

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

import java.math.BigDecimal;

import java.math.BigInteger;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import java.util.Map;// from 时 代 J a v a - N o w J a v a . c o m


public class Main{

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

        Object target = "nowjava.com";

        Object source = "nowjava.com";

        copyProperties(target,source);

    }

    public static void copyProperties(Object target, Object source) {

        copyProperties(target, source, null);

    }

    public static void copyProperties(Object target, Object source,

            String[] ignoreProperties) {

        if (target instanceof Map) {

            throw new UnsupportedOperationException(

                    "target is Map unsuported");

        }


        PropertyDescriptor[] targetPds = getPropertyDescriptors(target

                .getClass());

        List ignoreList = (ignoreProperties != null) ? Arrays

                .asList(ignoreProperties) : null;


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

            PropertyDescriptor targetPd = targetPds[i];

            if (targetPd.getWriteMethod() != null

                    && (ignoreProperties == null || (!ignoreList

                            .contains(targetPd.getName())))) {

                try {

                    if (source instanceof Map) {

                        Map map = (Map) source;

                        if (map.containsKey(targetPd.getName())) {

                            Object value = map.get(targetPd.getName());

                            setProperty(target, targetPd, value);

                        }

                    } else {

                        PropertyDescriptor sourcePd = getPropertyDescriptors(

                                source.getClass(), targetPd.getName());

                        if (sourcePd != null

                                && sourcePd.getReadMethod() != null) {

                            Object value = getProperty(source, sourcePd);

                            setProperty(target, targetPd, value);

                        }

                    }

                } catch (Throwable ex) {

                    throw new IllegalArgumentException(

                            "Could not copy properties on:"

                                    + targetPd.getDisplayName(), ex);

                }

            }

        }

    }

    public static PropertyDescriptor[] getPropertyDescriptors(

            Class beanClass) {

        BeanInfo beanInfo = null;

        try {

            beanInfo = Introspector.getBeanInfo(beanClass);

        } catch (IntrospectionException e) {

            return (new PropertyDescriptor[0]);

        }

        PropertyDescriptor[] descriptors = beanInfo

                .getPropertyDescriptors();

        if (descriptors == null) {

            descriptors = new PropertyDescriptor[0];

        }

        return descriptors;

    }

    public static PropertyDescriptor getPropertyDescriptors(

            Class beanClass, String name) {

        for (PropertyDescriptor pd : getPropertyDescriptors(beanClass)) {

            if (pd.getName().equals(name)) {

                return pd;

            }

        }

        return null;

    }

    private static void setProperty(Object target,

            PropertyDescriptor targetPd, Object value)

            throws IllegalAccessException, InvocationTargetException {

        Method writeMethod = targetPd.getWriteMethod();

        if (!Modifier.isPublic(writeMethod.getDeclaringClass()

                .getModifiers())) {

            writeMethod.setAccessible(true);

        }

        writeMethod.invoke(

                target,

                new Object[] { convert(value,

                        writeMethod.getParameterTypes()[0]) });

    }

    private static Object getProperty(Object source,

            PropertyDescriptor sourcePd) throws IllegalAccessException,

            InvocationTargetException {

        Method readMethod = sourcePd.getReadMethod();

        if (!Modifier.isPublic(readMethod.getDeclaringClass()

                .getModifiers())) {

            readMethod.setAccessible(true);

        }

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

        return value;

    }

    private static Object convert(Object value, Class<?> targetType) {

        if (value == null)

            return null;

        if (targetType == String.class) {

            return value.toString();

        } else {

            return convert(value.toString(), targetType);

        }

    }

    private static Object convert(String value, Class<?> targetType) {

        if (targetType == Byte.class || targetType == byte.class) {

            return new Byte(value);

        }

        if (targetType == Short.class || targetType == short.class) {

            return new Short(value);

        }

        if (targetType == Integer.class || targetType == int.class) {

            return new Integer(value);

        }

        if (targetType == Long.class || targetType == long.class) {

            return new Long(value);

        }

        if (targetType == Float.class || targetType == float.class) {

            return new Float(value);

        }

        if (targetType == Double.class || targetType == double.class) {

            return new Double(value);

        }

        if (targetType == BigDecimal.class) {

            
展开阅读全文