diff --git a/src/main/java/com/wangym/lombok/job/impl/CommentJob.java b/src/main/java/com/wangym/lombok/job/impl/CommentJob.java index 49171a9..e74ff0d 100644 --- a/src/main/java/com/wangym/lombok/job/impl/CommentJob.java +++ b/src/main/java/com/wangym/lombok/job/impl/CommentJob.java @@ -7,6 +7,8 @@ import org.springframework.util.FileCopyUtils; import java.io.*; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -21,24 +23,28 @@ public CommentJob() { @Override public void exec(File file) throws IOException { //按行读取文件 - StringBuffer sb = new StringBuffer(); + List stringList = new ArrayList<>(); boolean changeFlag = false; try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"))) { String line; while ((line = reader.readLine()) != null) { //判断是否包含行尾注释,将行尾注释提到上一行 - if (processLine(line, sb)) { + if (processLine(line, stringList)) { changeFlag = true; } } } //如果文件内容发生变化,重新写入文件 if (changeFlag) { + StringBuffer sb = new StringBuffer(); + for (String s : stringList) { + sb.append(s).append("\n"); + } FileCopyUtils.copy(sb.toString().getBytes("utf-8"), file); } } - private boolean processLine(String text, StringBuffer sb) { + private boolean processLine(String text, List lines) { Pattern pattern = Pattern.compile("^(\\s*)(.*?;)\\s*(//.*)$"); // 匹配行尾注释 Matcher matcher = pattern.matcher(text); @@ -47,14 +53,40 @@ private boolean processLine(String text, StringBuffer sb) { String code = matcher.group(2); // 获取代码部分 String comment = matcher.group(3); // 获取行尾注释 - sb.append(commentAlignment + comment + "\n"); - sb.append(commentAlignment + code + "\n"); + addToHead(lines, commentAlignment + comment); + lines.add(commentAlignment + code); return true; } else { - sb.append(text + "\n"); + lines.add(text); return false; } } + private void addToHead(List lines, String text) { + //for倒序循环遍历,找到第一个不是注解的行 + int index = 0; + for (int i = lines.size() - 1; i >= 0; i--) { + if (!isAnnotaion(lines.get(i))) { + break; + } else { + //记录最后一个注解的位置 + index = i; + } + + } + if (index != 0) { + lines.add(index, text); + } else { + lines.add(text); + } + } + + private boolean isAnnotaion(String text) { + //判断是否是注解 + Pattern pattern = Pattern.compile("^\\s*@\\D*$"); + Matcher matcher = pattern.matcher(text); + return matcher.find(); + } + } diff --git a/src/test/java/com/wangym/lombok/CommentJobTest.java b/src/test/java/com/wangym/lombok/CommentJobTest.java index 51e8a8a..13d3cf1 100644 --- a/src/test/java/com/wangym/lombok/CommentJobTest.java +++ b/src/test/java/com/wangym/lombok/CommentJobTest.java @@ -22,4 +22,9 @@ public void handle() throws IOException { job.exec(new ClassPathResource("LoggerExample.java").getFile()); } + @Test + public void handle1() throws IOException { + job.exec(new ClassPathResource("SystemOutPrintExample.java").getFile()); + } + } diff --git a/src/test/resources/SystemOutPrintExample.java b/src/test/resources/SystemOutPrintExample.java index 482412b..e5b76d4 100644 --- a/src/test/resources/SystemOutPrintExample.java +++ b/src/test/resources/SystemOutPrintExample.java @@ -1,6 +1,9 @@ package com.wangym.lombok.job.impl; public class SystemOutPrintExample { + @JsonProperty(value = "open_id") + private String name;//这是一个单行注释2 + public void test() { System.out.println("hello world");