基于org.springframework.beans.BeanUtils, 扩展了部分自己的方法
- 代码:
package com.visy.utils;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.Function;
/**
* @author visy.wang
* @description: Bean工具包
* @date 2023/4/24 9:54
*/
public class BeanUtils {
private static final Map<String, Map<String, Field>> fieldsCache = new HashMap<>();
/**
* 拷贝srcObj的属性到targetObj
* @param srcObj 源对象
* @param targetObj 目标对象
*/
public static void copyProperties(Object srcObj, Object targetObj){
org.springframework.beans.BeanUtils.copyProperties(srcObj, targetObj);
}
/**
* 拷贝srcObj的属性到targetObj,可忽略部分属性
* @param srcObj 源对象
* @param targetObj 目标对象
* @param ignoreProperties 不拷贝的属性名列表
*/
public static void copyProperties(Object srcObj, Object targetObj, String ...ignoreProperties){
org.springframework.beans.BeanUtils.copyProperties(srcObj, targetObj, ignoreProperties);
}
/**
* 拷贝srcObj的属性到targetObj,忽略srcObj中为null的属性
* @param srcObj 源对象
* @param targetObj 目标对象
*/
public static void copyPropertiesIgnoreNull(Object srcObj, Object targetObj){
List<String> ignoreList = new ArrayList<>();
Map<String, Field> fieldMap = getFieldMap(srcObj.getClass());
fieldMap.forEach((name, field) -> {
try{
if(Objects.isNull(field.get(srcObj))){
ignoreList.add(name);
}
}catch (Exception e){
e.printStackTrace();
}
});
if(CollectionUtils.isEmpty(ignoreList)){
copyProperties(srcObj, targetObj);
}else{
String[] ignoreProperties = new String[ignoreList.size()];
for (int i = 0; i < ignoreList.size(); i++) {
ignoreProperties[i] = ignoreList.get(i);
}
copyProperties(srcObj, targetObj, ignoreProperties);
}
}
/**
* 转换对象中的属性值
* @param obj 待转换对象
* @param converter 属性值转换器
* @param properties 要转换的属性列表
*/
public static void convertProperties(Object obj, Function<Object, ?> converter, String ...properties){
if(Objects.isNull(properties) || properties.length==0){
//没有需要转换的属性
return;
}
List<String> propertyList = Arrays.asList(properties);
Map<String, Field> fieldMap = getFieldMap(obj.getClass());
fieldMap.forEach((name,field) -> {
if(propertyList.contains(name)){
try{
field.set(obj, converter.apply(field.get(obj)));
}catch (Exception e){
e.printStackTrace();
}
}
});
}
/**
* 对象属性自拷贝
* @param obj 对象
* @param mapper 要拷贝的属性对应关系 Map<待拷贝属性名,目标属性名>,注意:K和V对应的属性类型应该相同
*/
public static void copySelfProperties(Object obj, Map<String,String> mapper){
if(Objects.isNull(mapper) || mapper.isEmpty()){
return;
}
Map<String, Field> fieldMap = getFieldMap(obj.getClass());
mapper.forEach((srcName, targetName) -> {
Field srcField = fieldMap.get(srcName), targetField = fieldMap.get(targetName);
if(Objects.nonNull(srcField) && Objects.nonNull(targetField)){
try{
targetField.set(obj, srcField.get(obj));
}catch (Exception e){
e.printStackTrace();
}
}
});
}
/**
* 按类缓存Field
* @param clazz 类Class
* @return Map<属性名,Field>
*/
private static Map<String,Field> getFieldMap(Class<?> clazz){
String clazzName = clazz.getName();
Map<String, Field> fieldMap = fieldsCache.get(clazzName);
if(Objects.nonNull(fieldMap)){
return fieldMap;
}
fieldMap = new HashMap<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if(Modifier.isStatic(field.getModifiers())){
//忽略静态变量
continue;
}
field.setAccessible(true);
fieldMap.put(field.getName(), field);
}
fieldsCache.put(clazzName, fieldMap);
return fieldMap;
}
}