|
|
|
@ -0,0 +1,75 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.file.FileReader;
|
|
|
|
|
import cn.hutool.core.io.file.FileWriter;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class ModifyJavaFileToReadWrite {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
List<String> update = Arrays.asList("Db.update", "Db.save", "Db.batchSave");
|
|
|
|
|
List<String> updateNew = Arrays.asList("Db.use(MASTER).update", "Db.use(MASTER).save", "Db.use(MASTER).batchSave");
|
|
|
|
|
|
|
|
|
|
List<String> find = Arrays.asList("Db.find", "Db.findAll", "Db.findFirst", "Db.paginate");
|
|
|
|
|
List<String> findNew = Arrays.asList("Db.use(SLAVE).find", "Db.use(SLAVE).findAll", "Db.use(SLAVE).findFirst", "Db.use(SLAVE).paginate");
|
|
|
|
|
String head1 = "import static com.dsideal.FengHuang.Const.DbConst.MASTER;";
|
|
|
|
|
String head2 = "import static com.dsideal.FengHuang.Const.DbConst.SLAVE;";
|
|
|
|
|
|
|
|
|
|
String path = "D:\\dsWork\\ccDangJianExamForOpenGauss\\src";
|
|
|
|
|
List<File> fileList = cn.hutool.core.io.FileUtil.loopFiles(new File(path), pathname -> {
|
|
|
|
|
if (pathname.getName().endsWith(".java") && pathname.getName().indexOf("ModifyJavaFileToReadWrite.java") < 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (File file : fileList) {
|
|
|
|
|
//读入文本内容
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
|
|
|
|
|
FileReader fileReader = new FileReader(file, "UTF-8");
|
|
|
|
|
List<String> data = new ArrayList<>();
|
|
|
|
|
List<String> res = new ArrayList<>();
|
|
|
|
|
fileReader.readLines(data);
|
|
|
|
|
for (String str : data) {
|
|
|
|
|
if (update.stream().anyMatch(str::contains)) {
|
|
|
|
|
flag = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (find.stream().anyMatch(str::contains)) {
|
|
|
|
|
flag = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (flag) {
|
|
|
|
|
boolean h1 = false, h2 = false;
|
|
|
|
|
|
|
|
|
|
for (String str : data) {
|
|
|
|
|
for (int i = 0; i < update.size(); i++) {
|
|
|
|
|
str = str.replace(update.get(i), updateNew.get(i));
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < find.size(); i++) {
|
|
|
|
|
str = str.replace(find.get(i), findNew.get(i));
|
|
|
|
|
}
|
|
|
|
|
if (str.indexOf(head1) >= 0) h1 = true;
|
|
|
|
|
if (str.indexOf(head2) >= 0) h2 = true;
|
|
|
|
|
res.add(str);
|
|
|
|
|
}
|
|
|
|
|
if (!h1) {
|
|
|
|
|
res.add(1, head1);
|
|
|
|
|
}
|
|
|
|
|
if (!h2) {
|
|
|
|
|
res.add(2, head2);
|
|
|
|
|
}
|
|
|
|
|
//回写文件
|
|
|
|
|
FileWriter fileWriter = new FileWriter(file, "UTF-8");
|
|
|
|
|
fileWriter.writeLines(res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println("恭喜,所有Java文件修改完毕!");
|
|
|
|
|
}
|
|
|
|
|
}
|