BeanUtils

BeanUtils.copyProperties(source, target, PublicUtils.getNullPropertyNames(source));

/**
* 获取所有字段为null的属性名
* 用于BeanUtils.copyProperties()拷贝属性时,忽略空值
* @param source
* @return
*/
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

Set<String> emptyNames = new HashSet<String>();
for(java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}

posted @ 2021-12-27 18:32  悄悄地超越  阅读(100)  评论(0编辑  收藏  举报