Map<String, Method> setterMapping = new TreeMap();
fillSettersAndGetters(obj.getClass(), setterMapping, getterMapping);
getterMapping.keySet().retainAll(setterMapping.keySet());
for (Entry<String, Method> getter : getterMapping.entrySet()) {
Method setter = setterMapping.get(getter.getKey());
// getter object should be castable to setter type:
if (!setter.getParameterTypes()[0].isAssignableFrom(
getter.getValue().getReturnType()))
continue;
try {
Object param = getter.getValue().invoke(obj);
if (getter.getValue().getReturnType().isArray()) {
param = ((Object[]) param).clone();
} else if (Collection.class.isAssignableFrom(getter.getValue().getReturnType())) {
param = new HashSet((Collection) param);
} else if (Map.class.isAssignableFrom(getter.getValue().getReturnType())) {
param = new HashMap((Map) param);
}
setter.invoke(newObj, param);
} catch (Exception ex) {
logger.fatal("Cannot set:" + getter.getKey(), ex);
}
}
}