java实体类转换
程序员文章站
2022-06-15 12:00:11
...
//实体类转换成另外一个实体类
public static T sourceToTarget(Object source, Class target) {
if (source == null) {
return null;
} else {
Object targetObject = null;
try {
targetObject = target.newInstance();
BeanUtils.copyProperties(source, targetObject);
} catch (Exception var4) {
logger.error("convert error ", var4);
}
return targetObject;
}
}
public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target) {
if (sourceList == null) {
return null;
} else {
ArrayList targetList = new ArrayList(sourceList.size());
try {
Iterator var3 = sourceList.iterator();
while(var3.hasNext()) {
Object source = var3.next();
T targetObject = target.newInstance();
BeanUtils.copyProperties(source, targetObject);
targetList.add(targetObject);
}
} catch (Exception var6) {
logger.error("convert error ", var6);
}
return targetList;
}
}
上一篇: 深入解析Python设计模式编程中建造者模式的使用
下一篇: java安全编码指南之:表达式规则说明