[update] ObjUtil.copy方法优化(即使copy不成功,也不报错,出警告日志)。PR:copy源和目标对象中的属性类型不一致,copy不成功

This commit is contained in:
yexuejc007 2023-06-15 14:24:05 +08:00
parent a260b41fae
commit 667ad26971

View File

@ -8,6 +8,8 @@ import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
@ -20,6 +22,8 @@ import java.util.stream.Collectors;
* @date 2018/12/28 15:54
*/
public class ObjUtil {
private static Logger log = Logger.getLogger(ObjUtil.class.getName());
private ObjUtil() {
}
@ -299,7 +303,9 @@ public class ObjUtil {
} catch (NoSuchFieldException e) {
}
} catch (Exception e) {
throw new RuntimeException(e);
log.warning(lowerCaseFirstChar(f.getName()) + " field copy failed. " + e);
log.log(Level.FINER, lowerCaseFirstChar(f.getName()) +
" field copy failed. The exception information is as follows:", e);
}
});
return o;
@ -321,8 +327,9 @@ public class ObjUtil {
List<Method> getterMethods = getAllGetterMethods(source.getClass(), "get");
O o = targetClass.getDeclaredConstructor().newInstance();
getterMethods.forEach(method -> {
String fieldName = method.getName().replace("get", "");
try {
String fieldName = method.getName().replace("get", "");
Object v = method.invoke(source);
if (invokeSetter) {
try {
@ -344,7 +351,9 @@ public class ObjUtil {
}
}
} catch (Exception e) {
throw new RuntimeException(e);
log.warning(lowerCaseFirstChar(fieldName) + " field copy failed. " + e);
log.log(Level.FINER, lowerCaseFirstChar(fieldName) +
" field copy failed. The exception information is as follows:\n", e);
}
});
return o;