Commit 100c6ea9 authored by wdy's avatar wdy

Merge branch 'dev' of http://gitlab.91isoft.com:90/wangfei/vehicle-quality-review into wangdingyi

parents 63a53733 bb64430b
package com.ruoyi.common.utils.bean;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
......@@ -107,4 +110,30 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
{
return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
}
public static <M> void mergeObject(M target, M source) {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(target.getClass());
// Iterate over all the attributes
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
// Only copy writable attributes
if (descriptor.getWriteMethod() != null) {
Object originalValue = descriptor.getReadMethod()
.invoke(target);
// Only copy values where the destination values is null
if (originalValue == null) {
Object defaultValue = descriptor.getReadMethod().invoke(
source);
descriptor.getWriteMethod().invoke(target, defaultValue);
}
}
}
} catch (Exception e) {
throw new RuntimeException("merge objects error", e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment