帮我写个java工具类,能把A对象的属性拷贝到B对象,考虑对象中的属性包含对象和对象集合的情况,属性中可能包含List<Object> 考虑这种情况
时间: 2023-06-03 16:02:05 浏览: 177
好的,我可以帮您编写这个工具类。以下是示例代码:
```
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ObjectCopyUtil {
public static void copyProperties(Object source, Object target) throws IllegalAccessException {
List<String> excludeProperties = new ArrayList<>(); // 需要排除的属性名列表
excludeProperties.add("id"); // 示例中排除了名为id的属性
Class<?> sourceClass = source.getClass();
Class<?> targetClass = target.getClass();
// 获取源对象和目标对象中所有的属性名和属性值
Map<String, Object> sourcePropertyMap = getPropertyMap(sourceClass, source);
Map<String, Object> targetPropertyMap = getPropertyMap(targetClass, target);
for (Map.Entry<String, Object> entry : sourcePropertyMap.entrySet()) {
String propertyName = entry.getKey();
Object propertyValue = entry.getValue();
if (excludeProperties.contains(propertyName)) { // 如果是需要排除的属性,则直接跳过
continue;
}
Object targetPropertyValue = targetPropertyMap.get(propertyName);
if (targetPropertyValue != null && !isAssignable(targetPropertyValue.getClass(), propertyValue.getClass())) { // 如果目标对象中的属性类型不能被赋值为源对象中的属性类型,则忽略该属性
continue;
}
if (propertyValue instanceof List) { // 如果属性是集合类型,则需要递归调用该方法进行子元素的拷贝
List<Object> sourceList = (List<Object>) propertyValue;
List<Object> targetList = (List<Object>) targetPropertyValue;
if (targetList == null) {
targetList = new ArrayList<>();
targetPropertyMap.put(propertyName, targetList);
}
for (Object sourceListItem : sourceList) {
Object targetListItem = createObjectInstance(sourceListItem.getClass()); // 创建目标对象子元素的实例
copyProperties(sourceListItem, targetListItem);
targetList.add(targetListItem);
}
} else if (propertyValue != null && !isPrimitiveOrWrapper(propertyValue.getClass())) { // 如果属性是对象类型,则需要递归调用该方法进行对象的拷贝
Object targetPropertyObject = createObjectInstance(propertyValue.getClass()); // 创建目标对象属性的实例
targetPropertyMap.put(propertyName, targetPropertyObject);
copyProperties(propertyValue, targetPropertyObject);
} else {
targetPropertyMap.put(propertyName, propertyValue);
}
}
updateObject(target, targetPropertyMap); // 将目标对象中属性的值更新为拷贝后的属性值
}
private static Map<String, Object> getPropertyMap(Class<?> clazz, Object object) throws IllegalAccessException {
Map<String, Object> propertyMap = new HashMap<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) { // 如果属性是静态或者瞬态的,则忽略该属性
continue;
}
field.setAccessible(true);
String propertyName = field.getName();
Object propertyValue = field.get(object);
propertyMap.put(propertyName, propertyValue);
}
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && !superclass.getName().equals(Object.class.getName())) {
Map<String, Object> superPropertyMap = getPropertyMap(superclass, object);
propertyMap.putAll(superPropertyMap);
}
return propertyMap;
}
private static void updateObject(Object object, Map<String, Object> propertyMap) throws IllegalAccessException {
Class<?> clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) { // 如果属性是静态或者瞬态的,则忽略该属性
continue;
}
field.setAccessible(true);
String propertyName = field.getName();
Object propertyValue = propertyMap.get(propertyName);
if (propertyValue != null) {
field.set(object, propertyValue);
}
}
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && !superclass.getName().equals(Object.class.getName())) {
updateObject(object, propertyMap);
}
}
private static boolean isAssignable(Class<?> targetType, Class<?> sourceType) {
return targetType.isAssignableFrom(sourceType);
}
private static boolean isPrimitiveOrWrapper(Class<?> clazz) {
return clazz.isPrimitive() || Number.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
}
private static Object createObjectInstance(Class<?> clazz) {
Object object = null;
try {
object = clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
}
```
您可以调用该工具类中的copyProperties方法将A对象的属性拷贝到B对象中。如果A对象的属性包含对象和集合属性,则也可以正确地进行拷贝。注:以上代码仅供参考,请根据实际情况进行调整和优化。
阅读全文