网页浏览器英文缩写,seo查询什么意思,建网站哪个好,西双版纳网站制作公司需求
应公司质量部要求#xff0c;需要对代码做静态检查。质量部要求#xff0c;源码文件必须在起始行起设置一些注释#xff0c;然而项目已经开发了一年之久#xff0c;且没有维护这个注释。
此时#xff0c;面对好几千个源码文件#xff0c;我们如何快速添加相应的注…需求
应公司质量部要求需要对代码做静态检查。质量部要求源码文件必须在起始行起设置一些注释然而项目已经开发了一年之久且没有维护这个注释。
此时面对好几千个源码文件我们如何快速添加相应的注释呢
对自己写一个程序来实现。
分析
假设注释模板为
/** Model: 模块* Description: 描述 * Author: 作者* Finished: 时间*/只要获得 模块 、描述、作者、时间 的值既可以通过文档读写完成给源码添加注释的需求。
代码
根据分析实现代码如下
package com.xzbd.jrx;import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;public class AddFileHeaderComment {private static String projectPath D:\\workspace\\builder_backend;public static void main(String[] args) {addFileHeaderComments(projectPath);}public static void addFileHeaderComments(String projectPath) {try {Files.walkFileTree(Paths.get(projectPath), new SimpleFileVisitorPath() {Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {String filePath file.toString();// 仅对Java文件进行if (filePath.endsWith(.java)) {addCommentToFile(file.toFile());}return FileVisitResult.CONTINUE;}});} catch (IOException e) {e.printStackTrace();}}public static void addCommentToFile(File file) {try {String packageName getPackageName(file);String className getClassName(file);String author getAuthor(file);String date getFileModifiedDate(file);String comment String.format(/*%n * Model: %s%n * Description: %s%n * Author: %s%n * Finished: %s%n */%n%n, packageName, className, author, date);String originalContent Files.readString(file.toPath());String newContent comment originalContent;Files.write(file.toPath(), newContent.getBytes(utf-8));} catch (IOException e) {e.printStackTrace();}}public static String getPackageName(File file) throws IOException {String absolutePath file.getAbsolutePath();String sep com\\xzbd\\;String packageName StrUtil.subAfter(absolutePath, sep, false);packageName StrUtil.subBefore(packageName, File.separator, false);// 可以使用 file.getParent().getFileName(); 根据需要调整return packageName;}public static String getClassName(File file) {return file.getName().replaceFirst([.][^.]$, );}public static String getAuthor(File file) {String absolutePath file.getAbsolutePath();String cmd String.format(git --git-dir%s\\.git --work-tree%s log --reverse %s, projectPath, projectPath,absolutePath);ListString execForLines RuntimeUtil.execForLines(Charset.forName(utf-8), cmd);for (String line : execForLines) {String sep Author:;if (StrUtil.startWith(line, sep)) {String useranme StrUtil.subAfter(line, sep, false);return useranme;}}return Your Name;}public static String getFileModifiedDate(File file) {String absolutePath file.getAbsolutePath();String cmd String.format(git --git-dir%s\\.git --work-tree%s log --reverse %s, projectPath, projectPath,absolutePath);ListString execForLines RuntimeUtil.execForLines(Charset.forName(utf-8), cmd);for (String line : execForLines) {String sep Date:;if (StrUtil.startWith(line, sep)) {String dateStr StrUtil.subAfter(line, sep, false);Date data new Date(dateStr);DateTimeFormatter dateFmt DateTimeFormatter.ofPattern(yyyy年MM月dd日);String date DateUtil.format(data, dateFmt);return date;}}DateTimeFormatter dateFmt DateTimeFormatter.ofPattern(yyyy年MM月dd日);String date DateUtil.format(new Date(), dateFmt);return date;}
}
执行后的效果
总结
文中代码程序实现了对java文件添加注释的功能其中用到了 hutool-all 工具其 pom 如下 dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.16/version/dependency另外程序也使用了 git log 查看文件的日志信息以获取文件正确的作者和完成时间。该 git 命令详解参考文章【Git】任何位置查看git日志 文章转载自: http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.qglqb.cn.gov.cn.qglqb.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.zcqbx.cn.gov.cn.zcqbx.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.gtylt.cn.gov.cn.gtylt.cn http://www.morning.mznqz.cn.gov.cn.mznqz.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.rkdnm.cn.gov.cn.rkdnm.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.lwdzt.cn.gov.cn.lwdzt.cn http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.pljxz.cn.gov.cn.pljxz.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.qcymf.cn.gov.cn.qcymf.cn http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.ftntr.cn.gov.cn.ftntr.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.rmdsd.cn.gov.cn.rmdsd.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn