com.feingto.cloud.kit.reflection.BeanConvertKit Maven / Gradle / Ivy
package com.feingto.cloud.kit.reflection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import java.beans.FeatureDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Bean属性转换工具类
*
* @author longfei
*/
@Slf4j
public abstract class BeanConvertKit extends org.springframework.beans.BeanUtils {
/**
* 把Bean数据转换到另外一个类的对象中
*
* @param source 数据源
* @param targetClass 目标对象的类
* @param ignoreProperties 忽略属性
* @return 目标对象
*/
public static T convert(Object source, Class targetClass, String... ignoreProperties) {
if (Objects.isNull(source)) {
return null;
}
try {
T target = targetClass.newInstance();
copyProperties(source, target, ignoreProperties);
return target;
} catch (InstantiationException e) {
log.error("Can't create Instance of Class:" + targetClass, e);
} catch (IllegalAccessException e) {
log.error("Can't access construct method of Class:" + targetClass, e);
}
return null;
}
/**
* 把Bean集合数据转换到另外一个类的集合对象中
*
* @param sources 数据源集合
* @param targetClass 目标对象的类
* @param ignoreProperties 忽略属性
* @return 目标对象
*/
public static List convert(Collection> sources, Class targetClass, String... ignoreProperties) {
if (Objects.isNull(sources)) {
return null;
}
return sources.stream()
.map(obj -> convert(obj, targetClass, ignoreProperties))
.collect(Collectors.toList());
}
/**
* 把Bean数据转换到另外一个类的对象中,源对象的空值不转换
*
* @param source 数据源
* @param target 目标对象
*/
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, "");
}
/**
* 把Bean数据转换到另外一个类的对象中,源对象的空值不转换
*
* @param source 数据源
* @param target 目标对象
* @param ignoreProperties 忽略属性
*/
public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
Stream.of(getPropertyDescriptors(target.getClass()))
.filter(targetPd -> Objects.nonNull(targetPd.getWriteMethod()))
.filter(targetPd -> !Arrays.asList(ignoreProperties).contains(targetPd.getName()))
.forEach(targetPd -> {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (Objects.nonNull(sourcePd) && Objects.nonNull(sourcePd.getReadMethod())) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// 源对象属性不为空(这里也能进行一些特殊要求的处理,例如绑定时格式转换等)
if (Objects.nonNull(value)) {
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
} catch (Exception e) {
throw new FatalBeanException("Could not copy properties from source to target", e);
}
}
});
}
/**
* 提取对象的空属性
*
* @param source 数据源
*/
public static String[] getNullProperties(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
return Stream.of(src.getPropertyDescriptors())
.filter(pd -> Objects.isNull(src.getPropertyValue(pd.getName())))
.map(FeatureDescriptor::getName)
.distinct()
.toArray(String[]::new);
}
}