Did*_*ero 2 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...
}
}
Run Code Online (Sandbox Code Playgroud)
我只想使用反射复制非空属性。该方法在这里描述与BeanUtils的作品非常好,但它会将所有没有空包装的对象,而不仅仅是没有空字段值:
//fetch c from the db...
Parent sameParent= new Parent;
sameParent.setWork(new Parent.Work());
//Directly from /sf/ask/91118821/#answer-3521314
BeanUtilsBean notNull=new NullAwareBeanUtilsBean();
notNull.copyProperties(c, sameParent);
Run Code Online (Sandbox Code Playgroud)
现在,Parent c将有场age=55。该字段work.employer将为空,因为对象 Work 已被覆盖。是否可以修改@Override copyProperty方法 fromBeanUtilsBean以递归地仅从包装对象中复制所需的(非空)属性?
否则,你知道其他方式吗?
您可以通过在 that 中进行简单的调整来实现这一点NullAwareBeanUtilsBean。我在这里所做的是检查要复制的属性的类型,如果它不是原始类型,则递归调用copyProperties方法。Java 自动装箱将原语转换为其包装类,因此我使用一组来标识原语类型。或者,您可以检查包的类类型以识别自定义对象,并仅对自定义进行递归调用,并避免使用所有 java 对象,如字符串、日期等。另请注意,此类不处理原始值。如果目标中的年龄设置为一个值并且源中的年龄未初始化,它仍然会覆盖。例如,如果源中的年龄设置为 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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4315 次 |
| 最近记录: |