两个对象拥有相同的字段,这两个可以通过拷贝互相给另一个对象进行赋值。通过使用下面两个工具类可以实现该功能。
使用方法 如上,DbzAecopdVoEntity和DbzAecopd1Entity具有相同的属性,通过BeanUtils.copyProperties(aecopd1Entity,dbzAecopd),可以将DbzAecopdVoEntity相同属性的值赋值给DbzAecopd1Entity。
以下是工具类代码
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
public class BeanUtils {
public static void copyProperties(Object to, Object from) {
Collection getters = IntrospectorUtil.getGetters(from.getClass());
for (Iterator iterator = getters.iterator(); iterator.hasNext();) {
Method getter = (Method) iterator.next();
String propertyName = IntrospectorUtil.getPropertyName(getter.getName());
try {
//On r閏up鑢e le setter s'il existe
Method setter = to.getClass().getDeclaredMethod(IntrospectorUtil.getSetterMethodName(propertyName),
new Class[]{getter.getReturnType()});
Object getterResult = getter.invoke(from, new Object[0]);
setter.invoke(to, new Object[]{getterResult});
} catch (NoSuchMethodException ex) {
//Pas grave on passe au suivant
continue;
} catch (IllegalAccessException ex) {
continue;
} catch (InvocationTargetException ex) {
continue;
}
}
}
}
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IntrospectorUtil {
public static Collection getGetters(Class classToAnalyze) {
ArrayList result = new ArrayList();
Method[] methods = classToAnalyze.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (isGetter(method)) result.add(method);
}
return result;
}
public static boolean isGetter(Method method) {
String methodName = method.getName();
if (!methodName.startsWith("get") &&
!methodName.startsWith("is")) {
return false;
}
if (methodName.startsWith("is") && (!method.getReturnType().equals(boolean.class)))
return false;
if (method.getReturnType().equals(void.class)) return false;
if (method.getParameterTypes().length != 0) return false;
return true;
}
public static Collection getSetters(Class classToAnalyze) {
ArrayList result = new ArrayList();
Method[] methods = classToAnalyze.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (isSetter(method)) result.add(method);
}
return result;
}
public static boolean isSetter(Method method) {
String methodName = method.getName();
if (!methodName.startsWith("set")) return false;
if (method.getParameterTypes().length != 1) return false;
return true;
}
public static String getPropertyName(String getterOrSetterName) {
if (getterOrSetterName.startsWith("set") ||
getterOrSetterName.startsWith("get"))
return decapitalize(getterOrSetterName.substring(3));
if (getterOrSetterName.startsWith("is"))
return decapitalize(getterOrSetterName.substring(2));
throw new IllegalArgumentException(getterOrSetterName + " is not a correct getter or setter"
+ "name");
}
public static String decapitalize(String name) {
if (name == null || name.length() == 0) {
return name;
}
if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))) {
return name;
}
char chars[] = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
return new String(chars);
}
public static String getSetterMethodName(String propertyName) {
return "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
}
public static String getBooleanGetterMethodName(String propertyName) {
return "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
}
public static String getGetterMethodName(String propertyName) {
return "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
}
public static void copyFields(Object from, Object to) {
Collection getters = IntrospectorUtil.getGetters(from.getClass());
for (Iterator iterator = getters.iterator(); iterator.hasNext();) {
Method getter = (Method) iterator.next();
String propertyName = IntrospectorUtil.getPropertyName(getter.getName());
try {
Method setter = to.getClass().getDeclaredMethod(IntrospectorUtil.getSetterMethodName(propertyName),
new Class[]{getter.getReturnType()});
Object getterResult = getter.invoke(from, new Object[0]);
setter.invoke(to, new Object[]{getterResult});
} catch (NoSuchMethodException ex) {
continue;
} catch (IllegalAccessException ex) {
continue;
} catch (InvocationTargetException ex) {
continue;
}
}
}
}