阅读背景:

bean对象的数据拷贝

来源:互联网 

public static void copyProperties(Object fromObj, Object toObj, boolean ignoreNull) throws BeanCopyException {
        if (fromObj == null) {
            return;
        }
        
        Class<? extends Object> fromClass = fromObj.getClass();
        Class<? extends Object> toClass = toObj.getClass();
        boolean isStrict = (fromClass == toClass);

        try {
            BeanInfo fromBean = Introspector.getBeanInfo(fromClass);
            BeanInfo toBean = Introspector.getBeanInfo(toClass);

            final PropertyDescriptor[] toPds = toBean.getPropertyDescriptors();
            final PropertyDescriptor[] fromPds = fromBean.getPropertyDescriptors();

            for (PropertyDescriptor toPd : toPds) {
                PropertyDescriptor fromPd = getPropertyDescriptor(fromPds, toPd, isStrict);
                if (fromPd != null && fromPd.getDisplayName().equals(toPd.getDisplayName())) {
                    Method writeMethod = toPd.getWriteMethod();
                    Method readMethod = fromPd.getReadMethod();
                    if (writeMethod != null && readMethod != null) {
                        Object param = readMethod.invoke(fromObj, (Object[]) null);
                        if (ignoreNull && param == null) {
                            continue;
                        }
                        writeMethod.invoke(toObj, param);
                    }
                }
            }
        } catch (Exception e) {
            throw new BeanCopyException(e);
        }
    }public static void copyProperties(Object fromObj,




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: