JavaBeanUtils工具类
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
* JavaBeanUtils
*
* @author kancy
* @date 2022/8/23 19:09
*/
@Slf4j
@UtilityClass
public class JavaBeanUtils extends BeanUtils {
/**
* 复制bean信息
*
* @param items 源bean
* @param beanClass 转换类型
* @return {@link List}<{@link T}>
*/
public static <E,T> List<T> copyBeans(List<E> items, Class<T> beanClass) {
return copyBeans(items, beanClass, t -> {
});
}
/**
* 复制bean信息
*
* @param items 源bean
* @param beanClass 转换类型
* @param consumer 消费者
* @return {@link List}<{@link T}>
*/
public static <E,T> List<T> copyBeans(List<E> items, Class<T> beanClass, BiConsumer<E, T> consumer) {
List<T> list = new ArrayList<>();
for (E item : items) {
T copyBean = copyBean(item, beanClass, consumer);
list.add(copyBean);
}
return list;
}
/**
* 复制bean信息
*
* @param items 源bean
* @param beanClass 转换类型
* @param consumer 消费者
* @return {@link List}<{@link T}>
*/
public static <E,T> List<T> copyBeans(List<E> items, Class<T> beanClass, Consumer<T> consumer) {
List<T> list = new ArrayList<>();
for (E item : items) {
T copyBean = copyBean(item, beanClass, consumer);
list.add(copyBean);
}
return list;
}
/**
* 复制bean信息
* @param item
* @param beanClass
* @return
*/
public static <E,T> T copyBean(E item, Class<T> beanClass) {
return copyBean(item, beanClass, t -> {
});
}
/**
* 复制bean信息
* @param item 源bean
* @param beanClass 转换类型
* @param consumer 消费者
* @return
*/
public static <E,T> T copyBean(E item, Class<T> beanClass, BiConsumer<E, T> consumer) {
T newInstance = null;
try {
Constructor<T> constructor = ClassUtils.getConstructorIfAvailable(beanClass);
Assert.notNull(constructor, String.format("beanClass [%s] NoArgsConstructor", beanClass));
newInstance = constructor.newInstance();
BeanUtils.copyProperties(item, newInstance);
if (Objects.nonNull(consumer)){
consumer.accept(item, newInstance);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return newInstance;
}
/**
* 复制bean信息
* @param item 源bean
* @param beanClass 转换类型
* @param consumer 消费者
* @return
*/
public static <E,T> T copyBean(E item, Class<T> beanClass, Consumer<T> consumer) {
T newInstance = null;
try {
Constructor<T> constructor = ClassUtils.getConstructorIfAvailable(beanClass);
Assert.notNull(constructor, String.format("beanClass [%s] NoArgsConstructor", beanClass));
newInstance = constructor.newInstance();
BeanUtils.copyProperties(item, newInstance);
if (Objects.nonNull(consumer)){
consumer.accept(newInstance);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return newInstance;
}
}
使用案例
// 单个bean复制
OrderVo orderVo = JavaBeanUtils.copyBean(order, OrderVo.class);
// 多个bean复制
List<OrderVo> orderVo = JavaBeanUtils.copyBeans(orders, OrderVo.class);
// 单个bean复制 (自定义转换)
OrderVo orderVo = JavaBeanUtils.copyBean(order, OrderVo.class, (order, orderVo) ->{
orderVo.setOrderId(order.getId());
});
// 多个bean复制 (自定义转换)
List<OrderVo> orderVo = JavaBeanUtils.copyBeans(orders, OrderVo.class, (order, orderVo) ->{
orderVo.setOrderId(order.getId());
});
标签:List,param,item,bean,赋值,beanClass,consumer,JavaBean,属性
From: https://www.cnblogs.com/kancy/p/javabeans.html