java的两个bean之间复制属性,所有属性中替换某几个字符
以下是实现代码:
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
public class BeanCopyUtil {
/**
* 复制bean属性
*
* @param source 源bean
* @param target 目标bean
* @param replaceChars 需要替换的字符列表
*/
public static void copyProperties(Object source, Object target, List<String> replaceChars) {
BeanUtils.copyProperties(source, target);
if (replaceChars != null && !replaceChars.isEmpty()) {
PropertyDescriptor[] sourcePds = BeanUtils.getPropertyDescriptors(source.getClass());
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
for (PropertyDescriptor sourcePd : sourcePds) {
if (sourcePd.getReadMethod() != null) {
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null && sourcePd.getName().equals(targetPd.getName())) {
try {
Object sourceValue = sourcePd.getReadMethod().invoke(source);
if (sourceValue != null && sourceValue instanceof String) {
String targetValue = (String) sourceValue;
for (String replaceChar : replaceChars) {
targetValue = targetValue.replace(replaceChar, "");
}
targetPd.getWriteMethod().invoke(target, targetValue);
} else {
targetPd.getWriteMethod().invoke(target, sourceValue);
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
break;
}
}
}
}
}
}
/**
* 复制bean属性
*
* @param source 源bean
* @param targetClass 目标bean的class
* @param replaceChars 需要替换的字符列表
* @return 目标bean
*/
public static <T> T copyProperties(Object source, Class<T> targetClass, List<String> replaceChars) {
T target = null;
try {
target = targetClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
copyProperties(source, target, replaceChars);
return target;
}
/**
* 复制bean属性
*
* @param sourceList 源bean列表
* @param targetClass 目标bean的class
* @param replaceChars 需要替换的字符列表
* @return 目标bean列表
*/
public static <T> List<T> copyProperties(List<?> sourceList, Class<T> targetClass, List<String> replaceChars) {
List<T> targetList = new ArrayList<>();
for (Object source : sourceList) {
T target = copyProperties(source, targetClass, replaceChars);
targetList.add(target);
}
return targetList;
}
}
使用示例:
public class Test {
public static void main(String[] args) {
// 复制单个bean属性
User source = new User();
source.setId(1L);
source.setName("Tom");
source.setAge(20);
User target = new User();
BeanCopyUtil.copyProperties(source, target, Arrays.asList("o"));
System.out.println(target.getId()); // 1
System.out.println(target.getName()); // Tm
System.out.println(target.getAge()); // 20
// 复制bean列表属性
List<User> sourceList = new ArrayList<>();
sourceList.add(source);
List<UserVO> targetList = BeanCopyUtil.copyProperties(sourceList, UserVO.class, Arrays.asList("o"));
System.out.println(targetList.get(0).getId()); // 1
System.out.println(targetList.get(0).getName()); // Tm
System.out.println(targetList.get(0).getAge()); // 20
}
}
class User {
private Long id;
private String name;
private Integer age;
// 省略getter和setter方法
}
class UserVO {
private Long id;
private String name;
private Integer age;
// 省略getter和setter方法
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)