update 使用 spring cglib 替换 停止维护的 cglib

2.X
疯狂的狮子li 3 years ago
parent 06b59e404c
commit 30bc36f5bf

@ -215,12 +215,6 @@
</exclusions>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- 代码生成使用模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>

@ -66,11 +66,6 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
<!-- Java Servlet -->
<dependency>
<groupId>javax.servlet</groupId>

@ -1,15 +1,20 @@
package com.ruoyi.common.core.utils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.SimpleCache;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.cglib.CglibUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.cglib.core.Converter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* bean( cglib )
@ -33,7 +38,8 @@ public class BeanCopyUtils {
if (ObjectUtil.isNull(desc)) {
return null;
}
return CglibUtil.copy(source, desc);
final V target = ReflectUtil.newInstanceIfPossible(desc);
return copy(source, target);
}
/**
@ -50,7 +56,8 @@ public class BeanCopyUtils {
if (ObjectUtil.isNull(desc)) {
return null;
}
CglibUtil.copy(source, desc);
BeanCopier beanCopier = BeanCopierCache.INSTANCE.get(source.getClass(), desc.getClass(), null);
beanCopier.copy(source, desc, null);
return desc;
}
@ -68,7 +75,11 @@ public class BeanCopyUtils {
if (CollUtil.isEmpty(sourceList)) {
return CollUtil.newArrayList();
}
return CglibUtil.copyList(sourceList, () -> ReflectUtil.newInstanceIfPossible(desc));
return sourceList.stream().map(source -> {
V target = ReflectUtil.newInstanceIfPossible(desc);
copy(source, target);
return target;
}).collect(Collectors.toList());
}
/**
@ -77,12 +88,12 @@ public class BeanCopyUtils {
* @param bean
* @return map
*/
@SuppressWarnings("all")
@SuppressWarnings("unchecked")
public static <T> Map<String, Object> copyToMap(T bean) {
if (ObjectUtil.isNull(bean)) {
return null;
}
return CglibUtil.toMap(bean);
return BeanMap.create(bean);
}
/**
@ -99,7 +110,8 @@ public class BeanCopyUtils {
if (ObjectUtil.isNull(beanClass)) {
return null;
}
return CglibUtil.toBean(map, beanClass);
T bean = ReflectUtil.newInstanceIfPossible(beanClass);
return mapToBean(map, bean);
}
/**
@ -116,6 +128,54 @@ public class BeanCopyUtils {
if (ObjectUtil.isNull(bean)) {
return null;
}
return CglibUtil.fillBean(map, bean);
BeanMap.create(bean).putAll(map);
return bean;
}
/**
* BeanCopier<br>
*
*
* @author Looly
* @since 5.4.1
*/
public enum BeanCopierCache {
/**
* BeanCopier
*/
INSTANCE;
private final SimpleCache<String, BeanCopier> cache = new SimpleCache<>();
/**
* key{@link BeanCopier}Map
*
* @param srcClass Bean
* @param targetClass Bean
* @param converter
* @return MapBeanCopier
*/
public BeanCopier get(Class<?> srcClass, Class<?> targetClass, Converter converter) {
final String key = genKey(srcClass, targetClass, converter);
return cache.get(key, () -> BeanCopier.create(srcClass, targetClass, converter != null));
}
/**
* key
*
* @param srcClass Bean
* @param targetClass Bean
* @param converter
* @return Mapkey
*/
private String genKey(Class<?> srcClass, Class<?> targetClass, Converter converter) {
final StringBuilder key = StrUtil.builder()
.append(srcClass.getName()).append('#').append(targetClass.getName());
if(null != converter){
key.append('#').append(converter.getClass().getName());
}
return key.toString();
}
}
}

Loading…
Cancel
Save