实现不同属性名称的属性拷贝
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BeanUtils {
private static void copyProperties(Object source, Object target, @Nullable Class<?> editable,
@Nullable Map<String, String> map, @Nullable String... ignoreProperties) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = source.getClass();
if (editable != null) {
if (!editable.isInstance(source)) {
throw new IllegalArgumentException("Source class [" + source.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] sourcePds = BeanUtils.getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
for (PropertyDescriptor sourcePd : sourcePds) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null && (ignoreList == null || !ignoreList.contains(sourcePd.getName()))) {
PropertyDescriptor targetPd = null;
if (map != null && map.get(sourcePd.getName()) != null && !"".equals(map.get(sourcePd.getName()))) {
targetPd = BeanUtils.getPropertyDescriptor(target.getClass(), map.get(sourcePd.getName()));
} else {
targetPd = BeanUtils.getPropertyDescriptor(target.getClass(), sourcePd.getName());
}
if (targetPd != null) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
copyProperties(source, target, null,null, ignoreProperties);
}
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null,null, (String[]) null);
}
public static void copyProperties(Object source, Object target,Map<String,String> map) throws BeansException {
copyProperties(source, target, null,map, (String[]) null);
}
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("a", "b");
A a = new A();
B b = new B();
a.setA("a");
BeanUtils.copyProperties(a, b, map);
System.out.println(b.toString());
}
}
class B {
private String b;
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
@Override
public String toString() {
return "B{" +
"b='" + b + '\'' +
'}';
}
}
class A {
private String a;
private String c;
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}