反射从一个对象复制非null属性到另一个BeanUtils

时间:2014-06-23 16:43:59

标签: java apache-commons-beanutils

有这个:

public class Parent {
    private String name;
    private int age;
    private Date birthDate;
    private Work work;
    static class Work{
        private int years;
        private String employer;
    }

// getters and setters   
    public static void main(String[] args) {
        Parent c = new Parent;
        c.setAge(55)
        Work work=new Parent.Work();
        work.setEmployer("Example");
        c.setWork(work);
        //save c in a DB...
    }
}

我想使用反射仅复制无空属性。使用beanUtils的方法described here非常有效,但它复制了所有无空包装的对象,而不仅仅是无空字段值:

//fetch c from the db...
Parent sameParent= new Parent;
sameParent.setWork(new Parent.Work());
//Directly from https://stackoverflow.com/questions/1301697/helper-in-order-to-copy-non-null-properties-from-object-to-another-java#answer-3521314
BeanUtilsBean notNull=new NullAwareBeanUtilsBean();
notNull.copyProperties(c, sameParent);

现在,Parent c将包含字段age=55。字段work.employer将为null,因为已覆盖对象Work。是否可以从@Override copyProperty修改BeanUtilsBean方法,以便从包装对象中仅递归复制所需(非空)属性?

否则,你知道其他方式吗?

1 个答案:

答案 0 :(得分:4)

您可以通过NullAwareBeanUtilsBean中的简单调整来实现这一目标。我在这里做的是检查要复制的属性的类型,如果它不是基本类型,则递归调用copyProperties方法。 Java自动装箱将基元转换为其包装类,因此我使用一个集来标识基元类型。或者,您可以检查包类的类型以识别自定义对象,并仅针对自定义执行递归调用,并避免所有Java对象(如String,Date等)。另请注意,此类不处理原始值。如果目标中的年龄设置为值且源中的年龄未初始化,则它仍将覆盖。例如,如果源中的年龄设置为20并且未使用设置新对象的年龄,则复制将覆盖值20为0.您可能需要在if-primitive检查中添加其他逻辑。

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtilsBean;

public class NullAwareBeanUtilsBean extends BeanUtilsBean {
    private static final Set<Class<?>> primitiveTypes = new HashSet<Class<?>>(
    Arrays.asList(Boolean.class, Character.class, Byte.class, Short.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Void.class, String.class, Date.class));

    private static final Set<Class<?>> primitiveTypes = new HashSet<Class<?>>() {
    {
        add(Boolean.class);
        add(Character.class);
        add(Byte.class);
        add(Short.class);
        add(Integer.class);
        add(Long.class);
        add(Float.class);
        add(Double.class);
        add(Void.class);
    }
    };

    @Override
    public void copyProperty(Object dest, String name, Object value)
        throws IllegalAccessException, InvocationTargetException {
    if (value == null)
        return;

    if (primitiveTypes.contains(value.getClass())) {
        super.copyProperty(dest, name, value);
    } else {
        try {
        Object childObj = getPropertyUtils().getSimpleProperty(dest, name);
        if (childObj == null || orig instanceof List && !((List)orig).isEmpty() && ((List)orig).get(0).getClass().isEnum()) {
            childObj=orig;
        }else{
            copyProperties(childObj, orig);
        }
        super.copyProperty(dest, name, childObj);
        } catch (NoSuchMethodException e) {
        e.printStackTrace();
        }
    }
    }
}