package org.yeshi.utils.bean;
|
|
import java.lang.reflect.Field;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* @author Administrator
|
* @title: BeanUtil
|
* @description: TODO
|
* @date 2021/10/11 14:01
|
*/
|
public class BeanUtil {
|
|
//复制属性
|
public static Object copyProperties(Object source, Object target) throws IllegalAccessException {
|
Map<String, Field> targetFieldMap = new HashMap<>();
|
Field[] fs = target.getClass().getDeclaredFields();
|
for (Field field : fs) {
|
targetFieldMap.put(field.getName(), field);
|
}
|
fs = source.getClass().getDeclaredFields();
|
for (Field field : fs) {
|
if (targetFieldMap.get(field.getName()) == null)
|
continue;
|
//获取对象的属性值
|
field.setAccessible(true);
|
Object resultValue = field.get(source);
|
targetFieldMap.get(field.getName()).setAccessible(true);
|
targetFieldMap.get(field.getName()).set(target, resultValue);
|
}
|
return target;
|
}
|
|
|
}
|