复制原始对象属性到目标对象

/**
 *
 * @param dest 目标对象
 * @param orig 原始对象
 * @throws Exception
 */
public static void copyProperties(Object orig, Object dest)
{

    try
    {
        if (dest == null)
        {
            throw new IllegalArgumentException("No destination bean specified");
        }
        if (orig == null)
        {
            throw new IllegalArgumentException("No origin bean specified");
        }

        Class origClass = orig.getClass();
        Class destClass = dest.getClass();
        if (origClass == String.class || origClass.isPrimitive())
        {
            dest = orig;
        }
        if (orig.getClass().isArray())
        {
            Object[] destArr = (Object[]) dest;
            Object[] origArr = (Object[]) orig;
            Class elemenClass = destArr.getClass().getComponentType();

            for (int i = 0; i < origArr.length; i++)
            {
                if (destArr[i] == null)
                {
                    destArr[i] = elemenClass.newInstance();
                }

                copyProperties(origArr[i], destArr[i]);
            }
        }
        String classLogInfo = "origClass:" + origClass.getName() + ",destClass:" + destClass.getName() + ",";
        PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(orig);
        for (int i = 0; i < origDescriptors.length; i++)
        {
            String name = origDescriptors[i].getName();
            if ("class".equals(name))
            {
                continue;
            }
            Object value = null;
            if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name))
            {
                try
                {
                    value = PropertyUtils.getSimpleProperty(orig, name);
                    PropertyUtils.setSimpleProperty(dest, name, value);
                }
                catch (IllegalArgumentException e)
                {
                    // 类型不同
                    try
                    {
                        PropertyDescriptor targetDescriptor = PropertyUtils.getPropertyDescriptor(dest, name);
                        Object new_value = targetDescriptor.getPropertyType().newInstance();
                        copyProperties(value, new_value);
                        //                      LOG.info(new_value);
                        PropertyUtils.setSimpleProperty(dest, name, new_value);
                    }
                    catch (IllegalArgumentException e1)
                    {
                        //
                    }
                    catch (IllegalAccessException e1)
                    {
                        logger.warn("",e1);
                        throw e1;
                    }
                    catch (InvocationTargetException e1)
                    {
                        logger.warn("",e1);
                        throw e1;
                    }
                    catch (NoSuchMethodException e1)
                    {
                        logger.warn("",e1);
                        throw e1;
                    }
                    catch (InstantiationException e1)
                    {
                        logger.warn("",e1);
                        throw e1;
                    }
                }
                catch (NoSuchMethodException e)
                {
                    throw e;
                }
                catch (IllegalAccessException e)
                {
                    throw e;
                }
                catch (InvocationTargetException e)
                {
                    throw e;
                }
            }
        }
    }
    catch (Exception e)
    {

    }

}

转载于:https://my.oschina.net/u/3031088/blog/983190

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值