Java List First Item isFirstAnnotationOfMethod(List lines, int lineNumber)

Here you can find the source of isFirstAnnotationOfMethod(List lines, int lineNumber)

Description

is First Annotation Of Method

License

Open Source License

Declaration

static boolean isFirstAnnotationOfMethod(List<String> lines, int lineNumber) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    static boolean isFirstAnnotationOfMethod(List<String> lines, int lineNumber) {
        String line = lines.get(lineNumber);
        if (isAnnotation(line) && (lineNumber == 0 || !isAnnotation(lines, lineNumber - 1))) {
            int i = lineNumber + 1;
            while (i < lines.size() && isAnnotation(lines.get(i))) {
                i++;// w  ww  .j a  v a  2s  .c  o  m
            }
            return isMethodDeclaration(lines, i);
        }
        return false;
    }

    static boolean isAnnotation(List<String> lines, int lineNumber) {
        return isAnnotation(lines.get(lineNumber));
    }

    static boolean isAnnotation(String line) {
        return line.matches("^\\s*@.*");
    }

    static boolean isMethodDeclaration(String line) {
        line = killComments(line);
        boolean isAbstract = (" " + line).contains(" abstract ");
        line = killOccurrences(line,
                "(public|private|protected|static|final|native|synchronized|abstract|transient|default)");
        line = killOccurrences(line, "\\[\\s*\\]");
        line = killOccurrences(line, "<[^<>]*>");
        line = killOccurrences(line, "\\.\\.\\.");
        line = killStrings(line);
        if (matches(line, ".*\\.[a-z].*\\(.*"))
            return false; // a method call
        if (isAbstract) {
            return matches(line.trim(),
                    "[a-zA-Z][^\\s\\.]*\\s+\\S+\\([^\\(\\)]*\\)\\s*(throws\\s+[^\\{\\;]*)?\\;$");
        } else {
            return matches(line.trim(),
                    "[a-zA-Z][^\\s\\.]*\\s+\\S+\\([^\\(\\)]*\\)\\s*(throws\\s+[^\\{\\;]*)?(\\{.*)?");
        }
    }

    static boolean isMethodDeclaration(List<String> lines, int lineNumber) {
        return isMethodDeclaration(lines.get(lineNumber));
    }

    private static boolean matches(String line, String regex) {
        String optionalStart = "^\\s*(/\\*.*\\*/)?\\s*";
        String optionalEnd = "\\s*(//.*|/\\*.*)?\\s*$";
        return line.matches(optionalStart + regex + optionalEnd);
    }

    private static boolean matches(List<String> lines, int lineNumber, String regex) {
        return matches(lines.get(lineNumber), regex);
    }

    private static String killComments(String lineToSearchIn) {
        String regexSingleLineComment = "//.*";
        lineToSearchIn = lineToSearchIn.replaceAll(regexSingleLineComment, "");
        String regexMultiComment = "/\\*[^\\*/]*\\*/";
        String tmp = killOccurrences(lineToSearchIn, regexMultiComment);
        return tmp.replaceAll("/\\*.*", "");
    }

    private static String killOccurrences(String line, String regex) {
        String tmp = line.replaceFirst(regex, "");
        while (!tmp.equals(tmp.replaceFirst(regex, ""))) {
            tmp = tmp.replaceFirst(regex, "");
        }
        return tmp;
    }

    static String killStrings(String line) {
        return killOccurrences(line.replaceAll("\\\\\"", ""), "\"[^\"]*[^\\\\]\"");
    }
}

Related

  1. getOnlyFirst(List obj)
  2. getPage(List all, int first, int pageSize)
  3. getStringFirstResult(List> llo, String reason)
  4. hasSameElement(List firstList, List secondList)
  5. isFirst(List list, V object)
  6. isFirstPage(List pageLines, List> rawPages)
  7. isInOrder(List src, Object first, Object second)
  8. limit(List list, Long firstResult, Long maxResults)
  9. listWith(Object aFirstObject)