对象之间属性赋值,改造BeanUtils.copyProperties()方法,属性类型不同也要转换

小可怡情 小可怡情 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我们知道要将DO对象转换成VO这种时候一般都用BeanUtils.copyProperties(),但是有个要求就是属性名和属性的类型都必须完全一致才行,否则该属性就不会拷贝,依旧是null(名称或者类型不同本来也就没办法拷贝)。
我们公司的VO对象里以为某种原因对象参数只能使用基本数据类型,这样DO里有Date,Bigdecimal之类的就惨了,都需要单独进行处理。于是就写了这么个工具类,也是从copyProperties这个方法里面的代码改成的,目前只写了处理Date转String,Bigdecimal转Doub,有其他转换需要的自己再添加(不然不同类型会报错的)。

package util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
* @author :王可可
* @date :Created in 2021/3/27 19:07
*/
public class MyUtil {

public static void copyProperties(Object source, Object target, String dateFormat) throws InvocationTargetException, IllegalAccessException {
copy(source, dateFormat, target);
}

public static void copyProperties(Object source, Object target) throws InvocationTargetException, IllegalAccessException {
String dateFormat = null;
copy(source, dateFormat, target);
}

public static <T> T copyProperties(Object source, Class<T> targetClass) throws InvocationTargetException, IllegalAccessException, InstantiationException {
String dateFormat = null;
T target = null;
if (source != null){
target = targetClass.newInstance();
copy(source, dateFormat, target);
}
return target;
}

public static <T> T copyProperties(Object source, Class<T> targetClass, String dateFormat) throws InvocationTargetException, IllegalAccessException, InstantiationException {
T target = null;
if (source != null){
target = targetClass.newInstance();
copy(source, dateFormat, target);
}
return target;
}

public static <T> List listConverter(List list, Class<T> classT, String dateFormat) throws InvocationTargetException, IllegalAccessException, InstantiationException {
List<T> targetList = new ArrayList();
if (list != null && list.size() != 0){
Iterator var3 = list.iterator();
while(var3.hasNext()) {
Object object = var3.next();
targetList.add(copyProperties(object, classT, dateFormat));
}
}
return targetList;
}

private static void copy(Object source, String dateFormat, Object target) throws IllegalAccessException, InvocationTargetException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
if (dateFormat == null) {
dateFormat = "yyyy-MM-dd HH:mm:ss";
}
Class<?> aClass = source.getClass();
PropertyDescriptor[] apds = BeanUtils.getPropertyDescriptors(aClass);
Class<?> bClass = target.getClass();
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(bClass);
int n = pds.length;
for (int i = 0; i < n; ++i) {
PropertyDescriptor targetPd = pds[i];
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null) {
PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
for (int j = 0; j < apds.length; j++) {
PropertyDescriptor apd = apds[j];
if (apd.getReadMethod().getName().equals(readMethod.getName())) {
// 将对象2的属性get方法转换成对象1的get方法(获得的value的类型就是对象1的该属性的类型)
readMethod = apd.getReadMethod();
continue;
}
}
if (readMethod != null) {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// 此处对要赋的值类型进行判断和处理
if (value.getClass().equals(Date.class)) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
value = sdf.format(value);
} else if (value.getClass().equals(BigDecimal.class)) {
BigDecimal b = (BigDecimal) value;
value = b.doubleValue();
}
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
}
}
}
}
}

...全文
评论 6
收藏 已收藏
分享
返回顶部
小可怡情
Java EE 67,535 成员 225,843 内容