mybatis-generator通过JavaParser工具实现Java代码合并
来源:互联网 发布:阿里云大厦有什么公司 编辑:程序博客网 时间:2024/06/08 10:46
首先附上我扩展后项目的github地址
点击打开链接
用过mybatis-generator的人应该都清楚,它默认是不支持Java代码合并的,只支持XML合并,这给我们的开发工作带来了一些麻烦。查看源代码
它默认的代码是这样的:/* (non-Javadoc) * @see 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地址:点击打开链接在引用它的maven包的时候我发现,原来mybatis-generator中也对它有使用,只是版本比较低,我将其改成了高版本的,修改后如下:<!-- TODO: Raising above 2.4.0 required jdk 8 usage --> <dependency> <groupId>com.github.javaparser</groupId> <artifactId>javaparser-core</artifactId> <version>3.2.10</version> <!--<scope>test</scope>--> </dependency>最后我重写了mergeJavaFile方法,这个代码如下:
public String mergeJavaFile(String newFileSource, String existingFileFullPath) throws ShellException, FileNotFoundException { return new JavaFileMergerJaxp().getNewJavaFile(newFileSource,existingFileFullPath); }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(); 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(); }
希望能给大家带来一些参考
阅读全文
1 0
- mybatis-generator通过JavaParser工具实现Java代码合并
- MyBatis-generator 代码生成工具
- mybatis代码生成工具 mybatis-generator
- mybatis mybatis-generator 代码自动生成工具
- mybatis mybatis-generator 代码自动生成工具
- mybatis-generator xml,java合并问题
- mybatis-generator 代码自动生成工具
- MyBatis Generator Tool工具生成代码
- mybatis-generator 代码自动生成工具
- mybatis-generator 代码自动生成工具
- mybatis-generator 代码自动生成工具
- mybatis-generator 代码自动生成工具
- mybatis-generator 代码自动生成工具
- mybatis-generator 代码自动生成工具
- mybatis-generator 代码自动生成工具
- 利用Mybatis-generator工具自动生成代码
- mybatis-generator 代码自动生成工具
- mybatis-generator 代码自动生成工具
- 二叉搜索树
- [总结]操作系统真是博大精深(一)
- concurrent常用类(二)
- 北京二手房成交继续低迷 有豪宅每周降价300万
- PDF阅读编辑器中压缩PDF文档的能力杠杠的
- mybatis-generator通过JavaParser工具实现Java代码合并
- 为什么不能建立引用的数组?
- 杭电5248—序列变换,最基础的二分
- Android常见的上滑渐变显示标题栏
- python Crypto模块的使用
- linux 常用命令(附录git 使用)
- GuavaCache简介
- logstash5.5.2部署-03
- angularjs概述