Java
对象转换工具类
package com.xxx.xxxxxx.xxxx;
public class BeanUtil<T> {
private static Logger logger = LoggerFactory.getLogger(BeanUtil.class);
public static <T> T convertMapToObj(T des, Map<String, Object> params) {
if (null == des || MapUtils.isEmpty(params)) {
throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
}
try {
Optional.ofNullable(params).ifPresent(ps -> {
ps.forEach((k, v) -> {
if (exist(k, des)) {
try {
Field field = des.getClass().getDeclaredField(k);
field.setAccessible(true);
field.set(des, v);
} catch (Exception e) {
logger.error("field set fail :{}", e);
throw new BusinessException(ErrorCode.SERVER_ERROR, ErrorCode.SERVER_ERROR.getMessage());
}
}
});
});
} catch (Exception e) {
logger.error("convertMapToObj fail :{}", e);
throw new BusinessException(ErrorCode.SERVER_ERROR, "convertMapToObj fail");
}
return des;
}
public static <T> T convertObjToObj(T des, T ori) {
if (null == des || null == ori) {
throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
}
Optional.ofNullable(ori.getClass().getDeclaredFields()).ifPresent(fields -> {
try {
for (Field f : fields) {
f.setAccessible(true);
String fieldName = f.getName();
if (!fieldName.equals("serialVersionUID")) {
Optional<Object> value = Optional.ofNullable(f.get(ori));
if (value.isPresent()) {
if (exist(fieldName, des)){
Field field = des.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(des, value.get());
}
}
}
}
} catch (Exception e) {
logger.error("convertObjToObj fail :{}", e);
throw new BusinessException(ErrorCode.SERVER_ERROR, "convertObjToObj fail");
}
});
return des;
}
public static <T> Map<String, Object> convertObjToMap(Map<String, Object> des, T ori) {
if (MapUtils.isEmpty(des) || null == ori) {
throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
}
Optional.ofNullable(ori.getClass().getDeclaredFields()).ifPresent(fields -> {
try {
for (Field f : fields) {
f.setAccessible(true);
String fieldName = f.getName();
if (!fieldName.equals("serialVersionUID")) {
Optional<Object> value = Optional.ofNullable(f.get(ori));
if (value.isPresent()) {
des.put(fieldName, value.get());
}
}
}
} catch (Exception e) {
logger.error("convertObjToMap fail :{}", e);
throw new BusinessException(ErrorCode.SERVER_ERROR, "convertObjToMap fail");
}
});
return des;
}
public static <T, R> T convertObjectToObject(T des, R ori) {
if (null == des || null == ori) {
throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
}
Optional.ofNullable(ori.getClass().getDeclaredFields()).ifPresent(fields -> {
try {
for (Field f : fields) {
f.setAccessible(true);
String fieldName = f.getName();
if (!fieldName.equals("serialVersionUID")) {
Optional<Object> value = Optional.ofNullable(f.get(ori));
if (value.isPresent()) {
if (exist(fieldName, des)) {
Field field = des.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(des, value.get());
}
}
}
}
} catch (Exception e) {
logger.error("convertParamToObject fail :{}", e);
throw new BusinessException(ErrorCode.SERVER_ERROR, "convertParamToObject fail");
}
});
return des;
}
public static Boolean exist(String fieldName, Object obj) {
if (obj == null || StringUtils.isEmpty(fieldName)) {
return null;
}
AtomicBoolean flag = new AtomicBoolean(false);
Optional.ofNullable(obj.getClass().getDeclaredFields()).ifPresent(
fields -> {
for (Field f : fields) {
f.setAccessible(true);
if (f.getName().equals(fieldName)) {
flag.set(true);
break;
}
}
}
);
return flag.get();
}
}