package ormRex;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;public classTest
{private final static String dir = "/Users/caicai/work/space/qingread-api-entity/src/main/java/com/qingread/serial/entity";public static voidmain(String[] args) throws IOException
{
File dirFile= newFile(dir);
String[] allFileName=dirFile.list();for(String fileName : allFileName)
{if (fileName.contains("java") == false) continue;
Path path= Paths.get(dir, fileName);
List alines =Files.readAllLines(path);
List allLine = new ArrayList<>();
String json= "@JSONField(name=\"%s\")";
String dateJson= "@JSONField(name=\"%s\",format=\"yyyy-MM-dd HH:mm:ss\")";//import com.alibaba.fastjson.annotation.JSONField;
for(String line : alines)
{if (line.contains("class"))
{
allLine.add("import com.alibaba.fastjson.annotation.JSONField;");
allLine.add("import java.io.Serializable;");
line= line.substring(0, line.length() - 1);
line+= "implements Serializable {";
allLine.add(line);
allLine.add("private static final long serialVersionUID = 1L;");continue;
}if (line.contains("private"))
{//关键字转下划线
String key = line.substring(line.lastIndexOf(" "), line.length() - 1);
key=key.trim();
String nKey= "";for (int i = 0; i < key.length(); i++)
{char c =key.charAt(i);if(Character.isUpperCase(c))
{
nKey+= "_";
nKey+=Character.toLowerCase(c);continue;
}
nKey+=c;
}//添加json修饰
if (line.contains("Date")) nKey =String.format(dateJson, nKey);elsenKey=String.format(json, nKey);
allLine.add(nKey);
}
allLine.add(line);
}
PrintWriter pw= newPrintWriter(path.toFile());for(String str : allLine)
{
pw.println(str);
}
pw.flush();
pw.close();
}
}
}