1 package java9; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Paths; 6 import java.util.List; 7 import java.util.regex.Pattern; 8 9 public class TTT { 10 11 public static void main(String args[]) throws IOException { 12 13 List<String> readAllLines = Files.readAllLines(Paths 14 .get("/Users/hongtaowang/git/spring-boot-jpa-demo/src/main/java/com/my/app/web/IndexController.java")); 15 16 int issueLine = 29; 17 18 String preMethodSignNumber = getPreMethodSignNumber(issueLine, readAllLines); 19 20 System.out.println(preMethodSignNumber); 21 22 String preSignEndChar = getPreSignEndChar(readAllLines, issueLine); 23 24 System.out.println(preSignEndChar); 25 26 } 27 28 private static String getPreSignEndChar(List<String> readAllLines, int issueLine) { 29 int nextMethodSignLine = getNextMethodSignNumber(issueLine, readAllLines); 30 31 for (int endLineNumber = nextMethodSignLine - 1; endLineNumber > 0; endLineNumber--) { 32 String endChar = readAllLines.get(endLineNumber - 1); 33 34 if (endChar.contains("}")) { 35 return (endLineNumber) + " end line " + endChar; 36 } 37 38 } 39 40 return ""; 41 } 42 43 private static int getNextMethodSignNumber(int i, List<String> readAllLines) { 44 int j = i; 45 for (; j < readAllLines.size(); j++) { 46 String p = readAllLines.get(j - 1); 47 boolean extracted = matchMethodSign(p); 48 if (extracted) { 49 break; 50 } 51 52 } 53 return j; 54 } 55 56 private static String getPreMethodSignNumber(int currentLine, List<String> readAllLines) { 57 for (int preLine = currentLine - 1; preLine > 0; preLine--) { 58 String preLineSource = readAllLines.get(preLine - 1); 59 60 if (matchMethodSign(preLineSource)) { 61 return (preLine) + " end line " + preLineSource; 62 } 63 64 } 65 66 return ""; 67 } 68 69 private static boolean matchMethodSign(String d) { 70 String pattern = ".*[A-z].* .*[A-z1-9]\(.*"; 71 boolean isMatch = Pattern.matches(pattern, d); 72 if (!isMatch) { 73 return false; 74 } 75 76 if (d.contains("return")) { 77 return false; 78 } 79 80 if (d.contains("=")) { 81 return false; 82 } 83 84 return true; 85 } 86 87 }