总体思路:
继承DefaultShellCallback.java,Overide 这个类里面isMergeSupported和mergeJavaFile方法,具体实现mergeJavaFile。
其中实现mergeJavaFile,需要引入javaparser相关包。
private static class MyCallBack extends DefaultShellCallback {
public MyCallBack() {
super(true);
}
@Override
public boolean isMergeSupported() {
return true;
}
@Override
public String mergeJavaFile(String newFileSource, File existingFile, String[] javadocTags, String fileEncoding)
throws ShellException{
try {
String ret = new JavaFileMerger().getNewJavaFile(newFileSource, existingFile.getPath());
return ret;
} catch (FileNotFoundException e) {
}
return null;
}
}
public static class JavaFileMerger{
public String getNewJavaFile(String newFileSource, String existingFileFullPath) throws FileNotFoundException {
CompilationUnit newCompilationUnit = JavaParser.parse(newFileSource);
CompilationUnit existingCompilationUnit = JavaParser.parse(new File(existingFileFullPath));
return mergerFile(newCompilationUnit,existingCompilationUnit);
}
public String mergerFile(CompilationUnit newCompilationUnit,CompilationUnit existingCompilationUnit){
StringBuilder sb = new StringBuilder(newCompilationUnit.getPackageDeclaration().get().toString());
newCompilationUnit.removePackageDeclaration();
//合并imports
NodeList<ImportDeclaration> imports = newCompilationUnit.getImports();
imports.addAll(existingCompilationUnit.getImports());
Set importSet = new HashSet<ImportDeclaration>();
importSet.addAll(imports);
NodeList<ImportDeclaration> newImports = new NodeList<>();
newImports.addAll(importSet);
newCompilationUnit.setImports(newImports);
for (ImportDeclaration i:newCompilationUnit.getImports()) {
sb.append(i.toString());
}
newLine(sb);
NodeList<TypeDeclaration<?>> types = newCompilationUnit.getTypes();
NodeList<TypeDeclaration<?>> oldTypes = existingCompilationUnit.getTypes();
for (int i = 0;i<types.size();i++) {
//截取Class
String classNameInfo = types.get(i).toString().substring(0, types.get(i).toString().indexOf("{")+1);
sb.append(classNameInfo);
newLine(sb);
newLine(sb);
//合并fields
List<FieldDeclaration> fields = types.get(i).getFields();
List<FieldDeclaration> oldFields = oldTypes.get(i).getFields();
for (FieldDeclaration f: fields){
sb.append(f.toString());
newLine(sb);
newLine(sb);
}
for (FieldDeclaration f: oldFields){
boolean flag = true;
for (String tag : MergeConstants.OLD_ELEMENT_TAGS) {
if (f.toString().contains(tag)) {
flag = false;
break;
}
}
if (flag) {
sb.append(f.toString());
newLine(sb);
newLine(sb);
}
}
//合并methods
List<MethodDeclaration> methods = types.get(i).getMethods();
List<MethodDeclaration> existingMethods = oldTypes.get(i).getMethods();
for (MethodDeclaration f: methods){
sb.append(f.toString());
newLine(sb);
newLine(sb);
}
for (MethodDeclaration m:existingMethods){
boolean flag = true;
for (String tag : MergeConstants.OLD_ELEMENT_TAGS) {
if (m.toString().contains(tag)) {
flag = false;
break;
}
}
if (flag){
sb.append(m.toString());
newLine(sb);
newLine(sb);
}
}
//判断是否有内部类
types.get(i).getChildNodes();
for (Node n:types.get(i).getChildNodes()){
if (n.toString().contains("static class")){
sb.append(n.toString());
}
if (n.toString().contains("enum")) {
sb.append(n.toString());
}
}
}
return sb.append(System.getProperty("line.separator")+"}").toString();
}
}
参考链接https://blog.youkuaiyun.com/w980994974/article/details/76904587
代码稍有改动,
1. 继承类相关代码更新
2.自动生成的java model文件成员变量附近merge,参考代码新老全部合并,出现两份同样代码,改为老的java file 有了就不合并了。