package com.rscode.credits.util; import java.util.HashSet;
import java.util.Set; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
/**
* @author 作者 tn
* @version 创建时间:2018年11月13日
* 类说明 复制bean
*/
public class BeanCopyUtil {
//source中的非空属性复制到target中
/**
*
* @param source 源
* @param target 目标
*/
public static <T> void beanCopy(T source, T target) {
BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
}
//source中的非空属性复制到target中,但是忽略指定的属性,也就是说有些属性是不可修改的(个人业务需要)
public static <T> void beanCopyWithIngore(T source, T target, String... ignoreProperties) {
String[] pns = getNullAndIgnorePropertyNames(source, ignoreProperties);
BeanUtils.copyProperties(source, target, pns);
}
public static String[] getNullAndIgnorePropertyNames(Object source, String... ignoreProperties) {
Set<String> emptyNames = getNullPropertyNameSet(source);
for (String s : ignoreProperties) {
emptyNames.add(s);
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static String[] getNullPropertyNames(Object source) {
Set<String> emptyNames = getNullPropertyNameSet(source);
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static Set<String> getNullPropertyNameSet(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
return emptyNames;
}
}