当前位置: 首页 > news >正文

天津seo排名收费360优化大师安卓版下载

天津seo排名收费,360优化大师安卓版下载,建站工具 比较,氪星人 wordpress需求 应公司质量部要求,需要对代码做静态检查。质量部要求,源码文件必须在起始行起设置一些注释,然而项目已经开发了一年之久,且没有维护这个注释。 此时,面对好几千个源码文件,我们如何快速添加相应的注…

需求

应公司质量部要求,需要对代码做静态检查。质量部要求,源码文件必须在起始行起设置一些注释,然而项目已经开发了一年之久,且没有维护这个注释。

此时,面对好几千个源码文件,我们如何快速添加相应的注释呢?

对,自己写一个程序来实现。

分析

假设注释模板为

/** 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 SimpleFileVisitor<Path>() {@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);List<String> 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);List<String> 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 如下:

		<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.16</version></dependency>

另外,程序也使用了 git log 查看文件的日志信息,以获取文件正确的作者,和完成时间。该 git 命令详解,参考文章【Git】任何位置查看git日志

http://www.hkea.cn/news/932487/

相关文章:

  • 合肥庐阳区疫情最新消息seo优化首页
  • h5网站制作接单最新中高风险地区名单
  • 北京市住房城乡建设委网站公司怎么在网上推广
  • 网站建设首页怎样插入视频百度指数在线查询小程序
  • 青州网站制作哪家好aso优化哪家好
  • wordpress做网站优点郑州网站seo优化
  • 宝安做棋牌网站建设找哪家公司好湖南长沙疫情最新消息
  • 四川专业网站建设中国十大企业培训机构排名
  • 怎么切页面做网站灰色词首页排名接单
  • 网站右侧浮动广告代码百度推广代理公司广州
  • 固原建站公司旺道seo推广系统怎么收费
  • 适合做外链的网站海外广告联盟平台推广
  • 建筑模板规格型号郑州厉害的seo顾问
  • ppt做书模板下载网站有哪些内容国际婚恋网站排名
  • 上海网站建设内容更新网络营销策划目的
  • 重庆市建设信息网站关键词查询网
  • 做哪种网站流量大怎么打广告宣传自己的产品
  • 免费表白网站制作seo网络优化推广
  • 网站建设中可能升级中国科技新闻网
  • 网站制作内容文案网站如何快速被百度收录
  • 淘宝淘宝网页版登录入口免费seo公司
  • 竹溪县县建设局网站短视频营销
  • 好的网站有哪些搜索引擎seo是什么意思
  • 做音乐网站赚钱吗做小程序的公司
  • 坪地网站建设域名流量查询工具
  • 网站建设部署万能推广app
  • 网站的重要性怎么做个网站
  • 做网站的经验百度旗下有哪些app
  • 化工网站开发推广点击器
  • 怎么访问日本竹中建设网站外贸seo推广