Search in sources :

Example 1 with LineColumn

use of com.puppycrawl.tools.checkstyle.api.LineColumn in project checkstyle by checkstyle.

the class RegexpCheck method isIgnore.

/**
 * Detect ignore situation.
 *
 * @param startLine position of line
 * @param text file text
 * @param start line column
 * @return true is that need to be ignored
 */
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private boolean isIgnore(int startLine, FileText text, LineColumn start) {
    final LineColumn end;
    if (matcher.end() == 0) {
        end = text.lineColumn(0);
    } else {
        end = text.lineColumn(matcher.end() - 1);
    }
    boolean ignore = false;
    if (ignoreComments) {
        final FileContents theFileContents = getFileContents();
        final int startColumn = start.getColumn();
        final int endLine = end.getLine();
        final int endColumn = end.getColumn();
        ignore = theFileContents.hasIntersectionWithComment(startLine, startColumn, endLine, endColumn);
    }
    return ignore;
}
Also used : LineColumn(com.puppycrawl.tools.checkstyle.api.LineColumn) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents)

Example 2 with LineColumn

use of com.puppycrawl.tools.checkstyle.api.LineColumn in project checkstyle by checkstyle.

the class RegexpCheck method findMatch.

/**
 * Recursive method that finds the matches.
 */
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private void findMatch() {
    final boolean foundMatch = matcher.find();
    if (foundMatch) {
        final FileText text = getFileContents().getText();
        final LineColumn start = text.lineColumn(matcher.start());
        final int startLine = start.getLine();
        final boolean ignore = isIgnore(startLine, text, start);
        if (!ignore) {
            matchCount++;
            if (illegalPattern || checkForDuplicates && matchCount - 1 > duplicateLimit) {
                errorCount++;
                logMessage(startLine);
            }
        }
        if (canContinueValidation(ignore)) {
            findMatch();
        }
    } else if (!illegalPattern && matchCount == 0) {
        logMessage(0);
    }
}
Also used : LineColumn(com.puppycrawl.tools.checkstyle.api.LineColumn) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 3 with LineColumn

use of com.puppycrawl.tools.checkstyle.api.LineColumn in project checkstyle by checkstyle.

the class MultilineDetector method findMatch.

/**
 * Method that finds the matches.
 */
private void findMatch() {
    try {
        boolean foundMatch = matcher.find();
        while (foundMatch) {
            currentMatches++;
            if (currentMatches > options.getMaximum()) {
                final LineColumn start = text.lineColumn(matcher.start());
                if (options.getMessage().isEmpty()) {
                    options.getReporter().log(start.getLine(), MSG_REGEXP_EXCEEDED, matcher.pattern().toString());
                } else {
                    options.getReporter().log(start.getLine(), options.getMessage());
                }
            }
            foundMatch = matcher.find();
        }
    }// see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993 et al.
     catch (StackOverflowError ignored) {
        // OK http://blog.igorminar.com/2008/05/catching-stackoverflowerror-and-bug-in.html
        // http://programmers.stackexchange.com/questions/
        // 209099/is-it-ever-okay-to-catch-stackoverflowerror-in-java
        options.getReporter().log(1, MSG_STACKOVERFLOW, matcher.pattern().toString());
    }
}
Also used : LineColumn(com.puppycrawl.tools.checkstyle.api.LineColumn)

Example 4 with LineColumn

use of com.puppycrawl.tools.checkstyle.api.LineColumn in project checkstyle by checkstyle.

the class BlockTagUtil method extractBlockTags.

/**
 * Extract the block tags from a Javadoc comment.
 *
 * @param lines The text of the comment, as separate lines.
 * @return The tags extracted from the block.
 */
public static List<TagInfo> extractBlockTags(String... lines) {
    final List<TagInfo> tags = new ArrayList<>();
    for (int i = 0; i < lines.length; i++) {
        // Starting lines of a comment have a different first line pattern.
        final boolean isFirstLine = i == 0;
        final Pattern pattern;
        if (isFirstLine) {
            pattern = BLOCK_TAG_PATTERN_FIRST_LINE;
        } else {
            pattern = BLOCK_TAG_PATTERN;
        }
        final String line = lines[i];
        final Matcher tagMatcher = pattern.matcher(line);
        if (tagMatcher.find()) {
            final String tagName = tagMatcher.group(1);
            // offset of one for the @ character
            final int colNum = tagMatcher.start(1) - 1;
            final int lineNum = i + 1;
            final String remainder = line.substring(tagMatcher.end(1));
            String tagValue = remainder.trim();
            // Handle the case where we're on the last line of a Javadoc comment.
            if (tagValue.endsWith(JAVADOC_CLOSING_TAG)) {
                final int endIndex = tagValue.length() - JAVADOC_CLOSING_TAG.length();
                tagValue = tagValue.substring(0, endIndex).trim();
            }
            final LineColumn position = new LineColumn(lineNum, colNum);
            tags.add(new TagInfo(tagName, tagValue, position));
        }
    }
    return tags;
}
Also used : Pattern(java.util.regex.Pattern) LineColumn(com.puppycrawl.tools.checkstyle.api.LineColumn) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 5 with LineColumn

use of com.puppycrawl.tools.checkstyle.api.LineColumn in project checkstyle by checkstyle.

the class InlineTagUtil method extractInlineTags.

/**
 * Extract inline Javadoc tags from the given comment.
 *
 * @param lines The Javadoc comment (as lines).
 * @return The extracted inline Javadoc tags.
 * @throws IllegalArgumentException when comment lines contain newlines
 */
public static List<TagInfo> extractInlineTags(String... lines) {
    for (String line : lines) {
        if (line.contains(LINE_FEED) || line.contains(CARRIAGE_RETURN)) {
            throw new IllegalArgumentException("comment lines cannot contain newlines");
        }
    }
    final String commentText = convertLinesToString(lines);
    final Matcher inlineTagMatcher = INLINE_TAG_PATTERN.matcher(commentText);
    final List<TagInfo> tags = new ArrayList<>();
    while (inlineTagMatcher.find()) {
        final String tagName = inlineTagMatcher.group(1);
        // Remove the leading asterisks (in case the tag spans a line) and collapse
        // the whitespace.
        String matchedTagValue = inlineTagMatcher.group(2);
        matchedTagValue = removeLeadingJavaDoc(matchedTagValue);
        matchedTagValue = collapseWhitespace(matchedTagValue);
        final String tagValue = matchedTagValue;
        final int startIndex = inlineTagMatcher.start(1);
        final LineColumn position = getLineColumnOfIndex(commentText, // correct start index offset
        startIndex - 1);
        tags.add(new TagInfo(tagName, tagValue, position));
    }
    return tags;
}
Also used : LineColumn(com.puppycrawl.tools.checkstyle.api.LineColumn) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Aggregations

LineColumn (com.puppycrawl.tools.checkstyle.api.LineColumn)6 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 ParseErrorMessage (com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage)1 ParseStatus (com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseStatus)1 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)1 FileText (com.puppycrawl.tools.checkstyle.api.FileText)1 Pattern (java.util.regex.Pattern)1