package com.hainabo.mgcmall.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class BeanUtils {
public static <M> void merge(M target, M destination) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());
// 遍历所有属性
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
// 允许读写
if (descriptor.getWriteMethod() != null) {
//Object originalValue = descriptor.getReadMethod().invoke(target);//target中(descriptor)的值
// Only copy values values where the destination values is null
//if (originalValue == null) {
Object defaultValue = descriptor.getReadMethod().invoke(destination);//destination中(descriptor)的值
if(defaultValue!=null && !"".equals(defaultValue)){
descriptor.getWriteMethod().invoke(target, defaultValue);//用defaultValue覆盖到target
}
//}
}
}
}
}
此方法会将 destination中!""&!null 的值覆盖到 target中