public class CopyAttributesUtils {
public static void copyAttributes(Object src, Object dest) {
Map<String, Object> srcMap = new HashMap<String, Object>();
Field[] srcFields = src.getClass().getDeclaredFields();
for (Field fd : srcFields) {
try {
fd.setAccessible(true);
if (fd.get(src) != null) {
srcMap.put(fd.getName(), fd.get(src));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Field[] destFields = dest.getClass().getDeclaredFields();
for (Field fd : destFields) {
if (srcMap.get(fd.getName()) == null) {
continue;
}
try {
BeanUtils.setProperty(dest, fd.getName(), srcMap.get(fd.getName()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}