工具类
public void copy(Object source,Object target) throws Exception {
Field[] SdeclaredFields = source.getClass().getDeclaredFields();
Field[] TdeclaredFields = target.getClass().getDeclaredFields();
List<Field> sfields = Arrays.asList(SdeclaredFields);
List<Field> tfields = Arrays.asList(TdeclaredFields);
sfields.forEach(a->{
loop:
for (Field tfield : tfields) {
if(a.getName().equals(tfield.getName())){
String methodName = "set" + a.getName().substring(0, 1).toUpperCase() + a.getName().substring(1);
List<String> methodList = Arrays.asList(target.getClass().getMethods()).stream().map(Method::getName).collect(Collectors.toList());
String methodGetName = "get" + a.getName().substring(0, 1).toUpperCase() + a.getName().substring(1);
if (!methodList.contains(methodName)){
continue loop;
}
try {
Method getmethod = source.getClass().getDeclaredMethod(methodGetName);
Object resultGet = getmethod.invoke(source);
if (resultGet != null){
Method method = target.getClass().getDeclaredMethod(methodName, resultGet.getClass());
Object result = method.invoke(target,resultGet);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}