@Slf4j
public class BeanHelper {
public static <T> T copyProperties(Object source, Class<T> target) {
if (!ObjectUtils.allNotNull(source, target)) {
return null;
}
try {
T instance = target.getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(source, instance);
return instance;
} catch (Exception exception) {
log.error("【对象转换】目标对象{}实例化异常 异常信息:{}", target.getSimpleName(), ExceptionUtils.getStackTrace(exception));
return null;
}
}
public static <T> Collection<T> copyWithCollection(Collection<Object> source, Class<T> target) {
if (CollectionUtils.isEmpty(source) || Objects.isNull(target)) {
return Collections.emptyList();
}
return source.stream().filter(Objects::nonNull).map(e -> copyProperties(e, target)).filter(Objects::nonNull).collect(Collectors.toList());
}
private static <T> T copyProperties(Object source, Supplier<T> createTarget, BiConsumer<Object, T> consumer, BiFunction<Object, T, T> callback, String... ignoreProperties) {
if (!ObjectUtils.allNotNull(source, createTarget)) {
return null;
}
T newTarget = null;
try {
newTarget = createTarget.get();
return Optional.ofNullable(newTarget).map(target -> {
BeanUtils.copyProperties(source, target, ignoreProperties);
if (Objects.nonNull(consumer)) {
consumer.accept(source, target);
}
if (Objects.nonNull(callback)) {
return callback.apply(source, target);
}
return target;
}).orElse(null);
} catch (Exception exception) {
log.error("【对象转换】目标对象{}数据转换异常 异常信息:{}", newTarget, ExceptionUtils.getStackTrace(exception));
return null;
}
}
public static <T> T copyProperties(Object source, Supplier<T> createTarget, BiFunction<Object, T, T> callback, String... ignoreProperties) {
return copyProperties(source, createTarget, null, callback, ignoreProperties);
}
public static <T> T copyProperties(Object source, Supplier<T> createTarget, BiConsumer<Object, T> consumer, String... ignoreProperties) {
return copyProperties(source, createTarget, consumer, null, ignoreProperties);
}
public static <T> T copyProperties(Object source, Supplier<T> createTarget, String... ignoreProperties) {
return copyProperties(source, createTarget, null, null, ignoreProperties);
}
public static <T> T copyProperties(Object source, Supplier<T> createTarget) {
return copyProperties(source, createTarget, null, null, (String[]) null);
}
private static <T> List<T> copyWithCollection(Collection<?> source, Supplier<T> createTarget, BiConsumer<Object, T> consumer, BiFunction<Object, T, T> callback, String... ignoreProperties) {
if (CollectionUtils.isEmpty(source)) {
return Collections.emptyList();
}
return source.stream().filter(Objects::nonNull).map(e -> copyProperties(e, createTarget, consumer, callback, ignoreProperties)).filter(Objects::nonNull).collect(Collectors.toList());
}
public static <T> List<T> copyWithCollection(Collection<?> source, Supplier<T> createTarget, String... ignoreProperties) {
return copyWithCollection(source, createTarget, null, null, ignoreProperties);
}
public static <T> List<T> copyWithCollection(Collection<?> source, Supplier<T> createTarget) {
return copyWithCollection(source, createTarget, null, null, (String[]) null);
}
public static <T> List<T> copyWithCollection(Collection<?> source, Supplier<T> createTarget, BiConsumer<Object, T> consumer) {
return copyWithCollection(source, createTarget, consumer, null, (String[]) null);
}
public static <T> List<T> copyWithCollection(Collection<?> source, Supplier<T> createTarget, BiConsumer<Object, T> consumer, String... ignoreProperties) {
return copyWithCollection(source, createTarget, consumer, null, ignoreProperties);
}
public static <T> List<T> copyWithCollection(Collection<?> source, Supplier<T> createTarget, BiFunction<Object, T, T> callback) {
return copyWithCollection(source, createTarget, null, callback, (String[]) null);
}
public static <T> List<T> copyWithCollection(Collection<?> source, Supplier<T> createTarget, BiFunction<Object, T, T> callback, String... ignoreProperties) {
return copyWithCollection(source, createTarget, null, callback, ignoreProperties);
}
}