|
|
@@ -6,6 +6,11 @@ import net.sf.cglib.beans.BeanCopier;
|
|
|
import net.sf.cglib.beans.BeanMap;
|
|
|
import net.sf.cglib.core.Converter;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.BiConsumer;
|
|
|
+import java.util.function.Supplier;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
/**
|
|
|
* Cglib工具类
|
|
|
*
|
|
|
@@ -22,6 +27,7 @@ public class CglibUtil {
|
|
|
* @param source 源bean对象
|
|
|
* @param targetClass 目标bean类,自动实例化此对象
|
|
|
* @return 目标对象
|
|
|
+ * @since 5.4.1
|
|
|
*/
|
|
|
public static <T> T copy(Object source, Class<T> targetClass) {
|
|
|
return copy(source, targetClass, null);
|
|
|
@@ -36,10 +42,11 @@ public class CglibUtil {
|
|
|
* @param targetClass 目标bean类,自动实例化此对象
|
|
|
* @param converter 转换器,无需可传{@code null}
|
|
|
* @return 目标对象
|
|
|
+ * @since 5.4.1
|
|
|
*/
|
|
|
public static <T> T copy(Object source, Class<T> targetClass, Converter converter) {
|
|
|
final T target = ReflectUtil.newInstanceIfPossible(targetClass);
|
|
|
- copy(source, target);
|
|
|
+ copy(source, target, converter);
|
|
|
return target;
|
|
|
}
|
|
|
|
|
|
@@ -48,6 +55,7 @@ public class CglibUtil {
|
|
|
*
|
|
|
* @param source 源bean对象
|
|
|
* @param target 目标bean对象
|
|
|
+ * @since 5.4.1
|
|
|
*/
|
|
|
public static void copy(Object source, Object target) {
|
|
|
copy(source, target, null);
|
|
|
@@ -66,18 +74,84 @@ public class CglibUtil {
|
|
|
|
|
|
final Class<?> sourceClass = source.getClass();
|
|
|
final Class<?> targetClass = target.getClass();
|
|
|
- final BeanCopier beanCopier = BeanCopierCache.INSTANCE.get(
|
|
|
- sourceClass, targetClass,
|
|
|
- () -> BeanCopier.create(sourceClass, targetClass, null != converter));
|
|
|
+ final BeanCopier beanCopier = BeanCopierCache.INSTANCE.get(sourceClass, targetClass, converter);
|
|
|
|
|
|
beanCopier.copy(source, target, converter);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 拷贝List Bean对象属性
|
|
|
+ *
|
|
|
+ * @param source 源bean对象list
|
|
|
+ * @param target 目标bean对象
|
|
|
+ * @param <S> 源bean类型
|
|
|
+ * @param <T> 目标bean类型
|
|
|
+ * @return 目标bean对象list
|
|
|
+ * @since 5.4.1
|
|
|
+ */
|
|
|
+ public static <S, T> List<T> copyList(List<S> source, Supplier<T> target) {
|
|
|
+ return copyList(source, target, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝List Bean对象属性
|
|
|
+ *
|
|
|
+ * @param source 源bean对象list
|
|
|
+ * @param target 目标bean对象
|
|
|
+ * @param converter 转换器,无需可传{@code null}
|
|
|
+ * @param <S> 源bean类型
|
|
|
+ * @param <T> 目标bean类型
|
|
|
+ * @return 目标bean对象list
|
|
|
+ * @since 5.4.1
|
|
|
+ */
|
|
|
+ public static <S, T> List<T> copyList(List<S> source, Supplier<T> target, Converter converter) {
|
|
|
+ return copyList(source, target, converter, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝List Bean对象属性
|
|
|
+ *
|
|
|
+ * @param source 源bean对象list
|
|
|
+ * @param target 目标bean对象
|
|
|
+ * @param callback 回调对象
|
|
|
+ * @param <S> 源bean类型
|
|
|
+ * @param <T> 目标bean类型
|
|
|
+ * @return 目标bean对象list
|
|
|
+ * @since 5.4.1
|
|
|
+ */
|
|
|
+ public static <S, T> List<T> copyList(List<S> source, Supplier<T> target, BiConsumer<S, T> callback) {
|
|
|
+ return copyList(source, target, null, callback);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝List Bean对象属性
|
|
|
+ *
|
|
|
+ * @param source 源bean对象list
|
|
|
+ * @param target 目标bean对象
|
|
|
+ * @param converter 转换器,无需可传{@code null}
|
|
|
+ * @param callback 回调对象
|
|
|
+ * @param <S> 源bean类型
|
|
|
+ * @param <T> 目标bean类型
|
|
|
+ * @return 目标bean对象list
|
|
|
+ * @since 5.4.1
|
|
|
+ */
|
|
|
+ public static <S, T> List<T> copyList(List<S> source, Supplier<T> target, Converter converter, BiConsumer<S, T> callback) {
|
|
|
+ return source.stream().map(s -> {
|
|
|
+ T t = target.get();
|
|
|
+ copy(source, t, converter);
|
|
|
+ if (callback != null) {
|
|
|
+ callback.accept(s, t);
|
|
|
+ }
|
|
|
+ return t;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 将Bean转换为Map
|
|
|
*
|
|
|
* @param bean Bean对象
|
|
|
* @return {@link BeanMap}
|
|
|
+ * @since 5.4.1
|
|
|
*/
|
|
|
public static BeanMap toMap(Object bean) {
|
|
|
return BeanMap.create(bean);
|