public static void copyFieldsTo(Object from, Object to) {
Set<String> getFields = getGetFields(from.getClass());
Set<String> fields = new HashSet();
for (Method method : to.getClass().getMethods()) {
String methodName = method.getName();
if (methodName.startsWith("set")) {
String name = getFieldName(method.getName());
if (getFields.contains(name)) {
fields.add(name);
}
}
}
try {
for (String expression : fields) {
Object value = Ognl.getValue(expression, from);
if (value != null && !"".equals(value)) {
Ognl.setValue(expression, to, value);
}
}
} catch (Exception ex) {
logger.error("异常",ex);
}
}
前提 是 字段名相同