mybatis-generator經過JavaParser工具實現Java代碼合併

首先附上我擴展後項目的github地址 點擊打開連接 用過mybatis-generator的人應該都清楚,它默認是不支持Java代碼合併的,只支持XML合併,這給咱們的開發工做帶來了一些麻煩。查看源代碼 它默認的代碼是這樣的:java

(https://my.oschina.net/weimingwei)git

org.mybatis.generator.api.ShellCallback#mergeJavaFile(java.lang.String, java.lang.String, java.lang.String[], java.lang.String) 
 */  
public String mergeJavaFile(String newFileSource,  
        String existingFileFullPath, String[] javadocTags, String fileEncoding)  
        throws ShellException {  
    throw new UnsupportedOperationException();  
}

僅僅是拋出了一個不支持的異常。如是我試着去擴展了這個接口,中間藉助了JavaParser這個用於解析Java文件的工具,附上它的github地址:<a href="https://github.com/javaparser/javaparser" target="_blank">點擊打開連接</a>
在引用它的maven包的時候我發現,原來mybatis-generator中也對它有使用,只是版本比較低,我將其改爲了高版本的,修改後以下:github

<!-- TODO: Raising above 2.4.0 required jdk 8 usage --> api

<dependency>  
            <groupId>com.github.javaparser</groupId>  
            <artifactId>javaparser-core</artifactId>  
            <version>3.2.10</version>  
            <!--<scope>test</scope>-->  
        </dependency>

最後我重寫了mergeJavaFile方法,這個代碼以下:mybatis

public String mergeJavaFile(String newFileSource,
String existingFileFullPath)
throws ShellException, FileNotFoundException {
return new JavaFileMergerJaxp().getNewJavaFile(newFileSource,existingFileFullPath);
}app

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);
}maven

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();  
        List<FieldDeclaration> newFields = new ArrayList<>();  
        HashSet<FieldDeclaration> fieldDeclarations = new HashSet<>();  
        fieldDeclarations.addAll(fields);  
        fieldDeclarations.addAll(oldFields);  
        newFields.addAll(fieldDeclarations);  
        for (FieldDeclaration f: newFields){  
            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());  
            }  
        }  

    }  

    return sb.append(System.getProperty("line.separator")+"}").toString();  
}

但願能給你們帶來一些參考ui

相關文章
相關標籤/搜索