背景:
日常工作中我们经常会遇到在属性值大体相同的情况下要把数据实体类在bo, po, model,vo,dto之间进行相互的转化,需要我们无脑的写一些恶心至极的convert,那我们为什么不写个工具去生成这些无聊的代码,提高我们编码的效率,来好让我们更好的去摸鱼呢,哦错了,去内卷呢QAQ。
注意事项:
仅仅转化字段名和字段类型相同的字段,不同的会在代码前添加“//TODO”的注释,并且暂时不涉及父类字段,后续会加上;
使用方法:
1.在main函数中给source和target赋值;
source:当前对象类型
target:要转化成的对象类型
2.运行main函数就会生成xxxxConvert.java文件到项目目录下。
例如:
source是UserDO, target是UserModel 就是生成将UserDO转化成UserModel的convert;
生成的convert就是这种格式:
className -> UserConvert
public UserMode convertModel(UserDO userDO) {
UserModel userModel = new UserModel();
.........
userModel.setXXX(userDO.getXXX); // 存在字段
........
// TODO SOURCE NO FIELD
userModel.setXXX(userDO.getXXX); // 不匹配的字段
........
return userModel;
}
下面请看代码:
package util;
import model.UserDO;
import model.UserModel;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
/**
* java 系统变量
* https://zhuanlan.zhihu.com/p/441691521
*/
public class ConvertUtil {
private static final String SPACE = " ";
private static final String ENTER = System.getProperty("line.separator");
private static final String ENTER_TABLE = ENTER + "\t";
private static final String EQUAL = " = ";
private static final String POINT = ".";
private static final String GET = "get";
private static final String SET = "set";
private static final String NEW = "new ";
private static final String BRACKETS = "()";
private static final String FILE_PATH = System.getProperty("user.dir") + System.getProperty("file.separator");
private static final String FILE_NAME = "Convert.java";
public static void main(String[] args) throws IOException {
Class source = UserDO.class;
Class target = UserModel.class;
// 方式一: 生成字符串
String s = generateConvertContent(source, target);
System.out.println(s);
// 方式二: 生成文件 默认生成的文件在项目的一级目录下和pom同层次
// parm1:source
// parm2:target
generateConvertFile(source, target);
}
/**
*
* @param source
* @param target
* @throws IOException
*/
private static void generateConvertFile(Class source, Class target) throws IOException {
String targetName = firstCharContrary(target.getSimpleName());
String className = "";
for (char c : targetName.toCharArray()) {
if (Character.isUpperCase(c)) {
int i = targetName.indexOf(c);
className = targetName.substring(0, i);
targetName = targetName.substring(i);
break;
}
}
String converMethodName = "converTo" + targetName;
String convertMethod = "public" + SPACE + target.getSimpleName() + SPACE + converMethodName + SPACE
+ "(" + source.getSimpleName() + SPACE + firstCharContrary(source.getSimpleName()) + ")";
String s = generateConvertContent(source, target);
s = convertMethod + "{" + ENTER_TABLE + s + ENTER +"}";
FileWriter fw = new FileWriter(FILE_PATH + firstCharContrary(className) + FILE_NAME);
fw.write(s.trim());
fw.close();
}
/**
* 默认传入和返回变量名
* @param source
* @param target
* @return
*/
private static String generateConvertContent(Class source, Class target) {
String sourceName = firstCharContrary(source.getSimpleName());
String targetName = firstCharContrary(target.getSimpleName());
return generateConvertContent(source, target, sourceName, targetName);
}
/**
*
* @param source
* @param target
* @param sourceName 传入变量名
* @param targetName 返回变量名
* @return
*/
private static String generateConvertContent(Class source, Class target, String sourceName, String targetName) {
StringBuilder result = new StringBuilder();
Field[] sourceDeclaredFields = source.getDeclaredFields();
Field[] targetDeclaredFields = target.getDeclaredFields();
result.append(getConvertNewObjectLine(target, targetName));
for (Field field : targetDeclaredFields) {
if (!getField(field, sourceDeclaredFields)) {
result.append(getConvertTODOLine());
}
result.append(getConvertFieldLine(sourceName, targetName, field.getName()));
}
return result.toString();
}
private static Boolean getField(Field target, Field[] fields) {
for (Field field : fields) {
if (field.getName().equals(target.getName())
&& field.getType().equals(target.getType())) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
private static String getConvertNewObjectLine(Class target, String targetName) {
return target.getSimpleName() + SPACE + targetName + EQUAL + NEW
+target.getSimpleName() + BRACKETS + ENTER_TABLE;
}
private static String getConvertTODOLine() {
return "//TODO SOURCE NO FIELD" + ENTER_TABLE;
}
private static String getConvertFieldLine(String sourceName, String targetName, String fieldName) {
return targetName + POINT + SET + firstCharContrary(fieldName)
+ "(" + sourceName + POINT + GET + firstCharContrary(fieldName) + BRACKETS + ")"
+ ENTER_TABLE;
}
private static String firstCharContrary(String fieldName) {
char[] chars = fieldName.toCharArray();
chars[0] = toContraryCase(chars[0]);
return String.valueOf(chars);
}
private static char toContraryCase(char c) {
if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
c ^= 32;
}
return c;
}
}