Proactive code replication tool

Source: Internet
Author: User

Purpose:

When developing a program, if you want to add a module to the system, the existing code file will be copied and some changes will be made. Suppose there is already a user addition, deletion, modification, and query module. To add a similar module for student, we will copy all the Java classes such as the user's action, Dao, and JSP pages, then modify the content.

When other modules are added again for the above steps, they will run again and again:

1. copy each file one by one

2. Replace by keyword (as in the above example, replace user with student)

3. Make changes based on business differences

This tool is used to automatically complete the first two steps in the preceding steps to accelerate code development.

PS: assuming that the Code module is written based on a standard SSH mode, you can use a standard code generation tool to generate a full set of Code. There are many similar tools on the Internet, however, the codes of different companies have different styles, and it is very difficult to have a unified code generation tool to generate a full set of suitable code. Therefore, such a method of copying changes is more suitable.


Tool code implementation:

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


Trial code:

package cn.jerryhouse.util.cc.test;import org.junit.Test;import cn.jerryhouse.util.cc.CodeCopier;public class CodeCopierTest {@Testpublic 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();}}


Compare the copy object with the source object:




Reprinted please indicate the source: http://blog.csdn.net/u014569459/article/details/38327339



Proactive code replication tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.