[英]Copy non-null properties from one object to another using BeanUtils or similar
[英]reflection copy non null properties from one object to another 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...
}
}
我只想使用反射复制no-null 属性 。 这里用beanUtils 描述的方法效果很好,但是它复制了所有非空包装的对象,而不仅仅是非空字段值:
//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已被覆盖。 是否可以从BeanUtilsBean
修改@Override copyProperty
方法,以便还从包装的对象中仅递归复制所需的属性(非null)?
否则,您是否知道其他方式?
您可以通过在NullAwareBeanUtilsBean
进行简单的调整来实现此NullAwareBeanUtilsBean
。 我在这里所做的是检查要复制的属性的类型,如果它不是原始类型,则递归调用copyProperties
方法。 Java自动装箱将原语转换为其包装类,因此我使用一组来标识原语类型。 或者,您可以检查包的类类型以识别自定义对象,并仅针对自定义对象进行递归调用,并避免使用所有Java对象(如String,Date等)。还要注意,此类不处理原始值。 如果目标中的年龄设置为一个值,而源中的年龄未初始化,它将仍然被覆盖。 例如,如果源中的age设置为20,并且未使用设置新对象的age,则复制会将值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();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.