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 3476d42 commit 8283fd8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/main/java/com/wangym/lombok/job/impl/CommentJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.wangym.lombok.job.impl;

import com.wangym.lombok.job.AbstractJob;
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.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class CommentJob extends AbstractJob {

public CommentJob() {
super(".java");
}

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

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

if (matcher.find()) {
String commentAlignment = matcher.group(1); // 获取注释对齐部分
String code = matcher.group(2); // 获取代码部分
String comment = matcher.group(3); // 获取行尾注释

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


}
25 changes: 25 additions & 0 deletions src/test/java/com/wangym/lombok/CommentJobTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.wangym.lombok;

import com.wangym.lombok.job.impl.CommentJob;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentJobTest {

@Autowired
private CommentJob job;

@Test
public void handle() throws IOException {
job.exec(new ClassPathResource("LoggerExample.java").getFile());
}

}
2 changes: 1 addition & 1 deletion src/test/resources/LoggerExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LoggerExample {

public void test() {
int a = 5;
double b = 6d;
double b = 6d; //这个是行尾注释,会被提到上一行
double b1 = 6.1;
long c = 7;
long c2 = 8l;
Expand Down

0 comments on commit 8283fd8

Please sign in to comment.