日期:2014-05-20  浏览次数:20714 次

BeanUtils类的方法求指教?
实现BeanUtils类的两个copyProperty方法(我是初学者,见笑了)

------解决方案--------------------
Java code

看吧:
    public void copyProperties(Object dest, Object orig)
        throws IllegalAccessException, InvocationTargetException
    {
        if(dest == null)
            throw new IllegalArgumentException("No destination bean specified");
        if(orig == null)
            throw new IllegalArgumentException("No origin bean specified");
        if(log.isDebugEnabled())
            log.debug("BeanUtils.copyProperties(" + dest + ", " + orig + ")");
        if(orig instanceof DynaBean)
        {
            DynaProperty origDescriptors[] = ((DynaBean)orig).getDynaClass().getDynaProperties();
            for(int i = 0; i < origDescriptors.length; i++)
            {
                String name = origDescriptors[i].getName();
                if(getPropertyUtils().isWriteable(dest, name))
                {
                    Object value = ((DynaBean)orig).get(name);
                    copyProperty(dest, name, value);
                }
            }

        } else
        if(orig instanceof Map)
        {
            for(Iterator names = ((Map)orig).keySet().iterator(); names.hasNext();)
            {
                String name = (String)names.next();
                if(getPropertyUtils().isWriteable(dest, name))
                {
                    Object value = ((Map)orig).get(name);
                    copyProperty(dest, name, value);
                }
            }

        } else
        {
            PropertyDescriptor origDescriptors[] = getPropertyUtils().getPropertyDescriptors(orig);
            for(int i = 0; i < origDescriptors.length; i++)
            {
                String name = origDescriptors[i].getName();
                if(!"class".equals(name) && getPropertyUtils().isReadable(orig, name) && getPropertyUtils().isWriteable(dest, name))
                    try
                    {
                        Object value = getPropertyUtils().getSimpleProperty(orig, name);
                        copyProperty(dest, name, value);
                    }
                    catch(NoSuchMethodException e) { }
            }

        }
    }

    public void copyProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException
    {
        if(log.isTraceEnabled())
        {
            StringBuffer sb = new StringBuffer("  copyProperty(");
            sb.append(bean);
            sb.append(", ");
            sb.append(name);
            sb.append(", ");
            if(value == null)
                sb.append("<NULL>");
            else
            if(value instanceof String)
                sb.append((String)value);
            else
            if(value instanceof String[])
            {
                String values[] = (String[])value;
                sb.append('[');
                for(int i = 0; i < values.length; i++)
                {
                    if(i > 0)
                        sb.append(',');
                    sb.append(values[i]);
                }

                sb.append(']');
            } else
            {
                sb.append(value.toString());
            }
            sb.append(')');
            log.trace(sb.toString());
        }
        Object target = bean;
        int delim = name.lastIndexOf('.');
        if(delim >= 0)
        {
            try
            {
                target = getPropertyUtils().getProperty(bean, name.substring(0, delim));
            }
            catch(NoSuchMethodException e)
            {
                return;
            }
            name = name.substring(delim + 1);
            if(log.isTraceEnabled())
            {
                log.trace("    Target bean = " + target);
                log.trace("    Target name = " + name);
            }
        }
        String propName = null;
        Class type = null;
        int index = -1;
        String key = null;
        propName = name;
        int i = propName.indexOf('[');
        if(i >= 0)
        {
            int k = propName.indexOf(']');
            try
            {
                index = Integer.parseInt(propName.substring(i + 1, k));
            }
            catch(NumberFormatException e) { }
            propName = propName.substring(0, i);
        }
        int j = propName.indexOf('(');
        if(j >= 0)
        {
            int k = propName.indexOf(')');
            try
            {
                key = propName.substring(j + 1, k);
            }
            catch(IndexOutOfBoundsException e) { }
            propName = propName.substring(0, j);
        }
        if(target instanceof DynaBean)
        {
            DynaClass dynaClass = ((DynaBean)target).getDynaClass();
            DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
            if(dynaProperty == null)
                return;
            type = dynaProperty.getType();
        } else
        {
            PropertyDescriptor descriptor = null;
            try
            {
                descriptor = getPropertyUtils().getPropertyDescriptor(target, name);
                if(descriptor == null)
                    return;
            }
            catch(NoSuchMethodException e)
            {
                return;
            }
            type = descriptor.getPropertyType();
            if(type == null)
            {
                if(log.isTraceEnabled())
                    log.trace("    target type for property '" + propName + "' is null, so skipping ths setter");
                return;
            }
        }
        if(log.isTraceEnabled())
            log.trace("    target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key);
        if(index >= 0)
        {
            Converter converter = getConvertUtils().lookup(type.getComponentType());
            if(converter != null)
            {
                log.trace("        USING CONVERTER " + converter);
                value = converter.convert(type, value);
            }
            try
            {
                getPropertyUtils().setIndexedProperty(target, propName, index, value);
            }
            catch(NoSuchMethodException e)
            {
                throw new InvocationTargetException(e, "Cannot set " + propName);
            }
        } else
        if(key != null)
        {
            try
            {
                getPropertyUtils().setMappedProperty(target, propName, key, value);
            }
            catch(NoSuchMethodException e)
            {
                throw new InvocationTargetException(e, "Cannot set " + propName);
            }
        } else
        {
            Converter converter = getConvertUtils().lookup(type);
            if(converter != null)
            {
                log.trace("        USING CONVERTER " + converter);
                value = converter.convert(type, value);
            }
            try
            {
                getPropertyUtils().setSimpleProperty(target, propName, value);
            }
            catch(NoSuchMethodException e)
            {
                throw new InvocationTargetException(e, "Cannot set " + propName);
            }
        }
    }