public BaseVO copy2VO() {
String voSuffix = "VO";
String modelSuffix = "Model";
int min=2;
if (this == null) {
return null;
}
BaseVO baseVO = null;
try {
String className = this.getClass().getName();
String vo = "." + voSuffix.toLowerCase() + ".";
String voClassName = Optional.ofNullable(className).map(s -> {
String[] split = s.split("\\.");
int length = split.length;
if (length >= min) {
String modelName = split[length - 1];
String model = split[length - 2];
String prefix = Arrays.stream(Arrays.copyOfRange(split, 0, length - 2))
.collect(Collectors.joining("."));
String voName = modelName.replace(modelSuffix, voSuffix);
return prefix + vo + voName;
}
return null;
}).orElseThrow((Supplier<Throwable>) () -> new ClassNotFoundException());
Class<?> clazz = Class.forName(voClassName);
Constructor<?> constructor = clazz.getConstructor();
constructor.setAccessible(true);
baseVO = (BaseVO) constructor.newInstance();
BeanUtils.copyProperties(this, baseVO);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return baseVO;
}
Java 基类实现Model转Vo
最新推荐文章于 2024-03-13 11:03:14 发布