工具用途:
平时开发程序的时候,假设要在系统里新增一个模块,一般会复制已有的代码文件,然后再做一些改动。假设已有一个User的增删改查模块,此时要新增一个对Student的类似模块,我们就会复制User的Action/Dao/jsp页面等全部java类,然后对里面的内容进行改动。
对于上面的步骤,当再次新增其它模块时,还会再次反复运行:
1.逐个复制各个文件
2.按keyword进行替换(如上面例子中,须要将User替换为Student)
3.依据业务差异,进行改动
本工具用于自己主动完毕上面步骤中前两个步骤,达到加快代码开发的目的。
PS:假设代码模块是基于一种标准的SSH模式来编写的,自然能够採用标准的代码生成工具来生成全套代码,网上也有非常多类似的工具,可是纵观各个不同公司的代码,风格各异,非常难有一个统一的代码生成工具来生成合适的全套代码,所以这样的复制改动的方法反而更适用一些。
工具代码实现:
package cn.jerryhouse.util.cc; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.LinkedList; import java.util.List; public class CodeCopier { private List<String> srcFileList = new LinkedList<String>(); private String srcBaseDir; private String destBaseDir; private List<ReplacePair> replacePairList = new LinkedList<ReplacePair>(); private final String NEW_LINE = System.getProperty("line.separator"); public void process() { for(String filePath: srcFileList) { String destFilePath = getDestFilePath(filePath); copyAndModifyFile(filePath,destFilePath); } } private String getDestFilePath(String srcFilePath) { File srcFile = new File(srcFilePath); String srcFileName = srcFile.getName(); String srcFileDir = srcFile.getParent(); String destFileName = getDestFileName(srcFileName); String destFilePath = this.destBaseDir+srcFileDir.substring(srcBaseDir.length())+File.separator+destFileName; return destFilePath; } private String getDestFileName(String srcFilePath) { return updateContentByPattern(srcFilePath); } private void copyAndModifyFile(String srcFilePath,String destFilePath) { try { mkdirsForFilePath(destFilePath); BufferedReader br = new BufferedReader(new FileReader(srcFilePath)); BufferedWriter bw = new BufferedWriter(new FileWriter(destFilePath)); String line; while((line=br.readLine())!=null) { String updatedLine = updateContentByPattern(line); bw.write(updatedLine+NEW_LINE); } br.close(); bw.close(); } catch (Exception e) { e.printStackTrace(); } } private void mkdirsForFilePath(String filePath) { File file = new File(filePath); file.getParentFile().mkdirs(); } private String updateContentByPattern(String content) { for(ReplacePair replacePair: replacePairList) { content = content.replaceAll(replacePair.getToReplacePattern(), replacePair.getReplacement()); } return content; } public void addReplacePair(String toReplacePattern,String replacement) { replacePairList.add(new ReplacePair(toReplacePattern,replacement)); } public void addSrcFile(String srcFilePath) { srcFileList.add(srcFilePath); } public void setSrcBaseDir(String srcBaseDir) { this.srcBaseDir = srcBaseDir; } public void setDestBaseDir(String destBaseDir) { this.destBaseDir = destBaseDir; } }
package cn.jerryhouse.util.cc; public class ReplacePair { private String toReplacePattern; private String replacement; public ReplacePair(String toReplacePattern,String replacement) { this.toReplacePattern = toReplacePattern; this.replacement = replacement; } public String getToReplacePattern() { return toReplacePattern; } public void setToReplacePattern(String toReplacePattern) { this.toReplacePattern = toReplacePattern; } public String getReplacement() { return replacement; } public void setReplacement(String replacement) { this.replacement = replacement; } }
測试代码:
package cn.jerryhouse.util.cc.test; import org.junit.Test; import cn.jerryhouse.util.cc.CodeCopier; public class CodeCopierTest { @Test public void testProcess() { CodeCopier codeCopier = new CodeCopier(); codeCopier.setSrcBaseDir("D:/tmp/sshExample2"); codeCopier.setDestBaseDir("D:/tmp1/sshExample2"); codeCopier.addSrcFile("D:/tmp/sshExample2/src/cn/jerry/ssh/mdv/action/UserAction.java"); codeCopier.addSrcFile("D:/tmp/sshExample2/src/cn/jerry/ssh/mdv/dao/UserDaoImp.java"); codeCopier.addReplacePair("User", "Student"); codeCopier.addReplacePair("user", "student"); codeCopier.process(); } }
拷贝文件和源文件对照:
转载请注明出处:http://blog.csdn.net/u014569459/article/details/38327339
时间: 2024-09-30 19:14:31