Commit bb64430b authored by 王飞's avatar 王飞

Merge branch 'wangfei' into 'dev'

1、新增copyBean的工具类。

See merge request !145
parents ff4e5d3e 8e315bab
package com.ruoyi.common.utils.bean; 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.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -107,4 +110,30 @@ public class BeanUtils extends org.springframework.beans.BeanUtils ...@@ -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)); 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