Skip to content

Commit

Permalink
--taskid=ID20231016
Browse files Browse the repository at this point in the history
add rule:Single line comments in a method should be put above the code to be commented, by using // and multiple lines by using /* */.
  • Loading branch information
xinluke committed Oct 16, 2023
1 parent 8283fd8 commit 0f500b0
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/main/java/com/wangym/lombok/job/impl/CommentJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -22,20 +19,21 @@ public CommentJob() {
public void exec(File file) throws IOException {
//按行读取文件
StringBuffer sb = new StringBuffer();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
boolean changeFlag = false;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"))) {
String line;
while ((line = reader.readLine()) != null) {
//判断是否包含行尾注释,将行尾注释提到上一行
processLine(line, sb);
changeFlag = true && processLine(line, sb);
}
}
//如果文件内容发生变化,重新写入文件
if (!sb.toString().equals(FileCopyUtils.copyToString(new FileReader(file)))) {
if (changeFlag) {
FileCopyUtils.copy(sb.toString().getBytes("utf-8"), file);
}
}

private void processLine(String text, StringBuffer sb) {
private boolean processLine(String text, StringBuffer sb) {
Pattern pattern = Pattern.compile("^(\\s*)(.*?;)\\s*(//.*)$"); // 匹配行尾注释
Matcher matcher = pattern.matcher(text);

Expand All @@ -46,8 +44,10 @@ private void processLine(String text, StringBuffer sb) {

sb.append(commentAlignment + comment + "\n");
sb.append(commentAlignment + code + "\n");
return true;
} else {
sb.append(text + "\n");
return false;
}
}

Expand Down

0 comments on commit 0f500b0

Please sign in to comment.