Search in sources :

Example 11 with FileContents

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

the class EmptyLineSeparatorCheck method getEmptyLines.

/**
     * Get list of empty lines.
     * @param ast the ast to check.
     * @return list of line numbers for empty lines.
     */
private List<Integer> getEmptyLines(DetailAST ast) {
    final DetailAST lastToken = ast.getLastChild().getLastChild();
    int lastTokenLineNo = 0;
    if (lastToken != null) {
        lastTokenLineNo = lastToken.getLineNo();
    }
    final List<Integer> emptyLines = new ArrayList<>();
    final FileContents fileContents = getFileContents();
    for (int lineNo = ast.getLineNo(); lineNo < lastTokenLineNo; lineNo++) {
        if (fileContents.lineIsBlank(lineNo)) {
            emptyLines.add(lineNo);
        }
    }
    return emptyLines;
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) ArrayList(java.util.ArrayList)

Example 12 with FileContents

use of com.puppycrawl.tools.checkstyle.api.FileContents in project commons by twitter.

the class JavadocMethodRegexCheck method isShortEnoughToSkip.

/**
   * Return true iff the method is short enough that we don't require
   * a javadoc.
   */
private boolean isShortEnoughToSkip(final DetailAST aAST) {
    // Based on code from
    // com/puppycrawl/tools/checkstyle/api/MethodLengthCheck.java
    final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST);
    if (openingBrace != null) {
        final DetailAST closingBrace = openingBrace.findFirstToken(TokenTypes.RCURLY);
        int length = closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
        // skip blank lines
        final FileContents contents = getFileContents();
        final int lastLine = closingBrace.getLineNo();
        for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
            if (contents.lineIsBlank(i)) {
                length--;
            }
        }
        if (length <= minLineCount) {
            return true;
        }
    }
    return false;
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 13 with FileContents

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

the class TreeWalker method processFiltered.

@Override
protected void processFiltered(File file, List<String> lines) throws CheckstyleException {
    // check if already checked and passed the file
    if (CommonUtils.matchesFileExtension(file, getFileExtensions())) {
        final String msg = "%s occurred during the analysis of file %s.";
        final String fileName = file.getPath();
        try {
            final FileText text = FileText.fromLines(file, lines);
            final FileContents contents = new FileContents(text);
            final DetailAST rootAST = parse(contents);
            getMessageCollector().reset();
            walk(rootAST, contents, AstState.ORDINARY);
            final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);
            walk(astWithComments, contents, AstState.WITH_COMMENTS);
        } catch (final TokenStreamRecognitionException tre) {
            final String exceptionMsg = String.format(Locale.ROOT, msg, "TokenStreamRecognitionException", fileName);
            throw new CheckstyleException(exceptionMsg, tre);
        } catch (RecognitionException | TokenStreamException ex) {
            final String exceptionMsg = String.format(Locale.ROOT, msg, ex.getClass().getSimpleName(), fileName);
            throw new CheckstyleException(exceptionMsg, ex);
        }
    }
}
Also used : TokenStreamException(antlr.TokenStreamException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) FileText(com.puppycrawl.tools.checkstyle.api.FileText) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) RecognitionException(antlr.RecognitionException)

Example 14 with FileContents

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

the class AstTreeStringPrinter method parseFileText.

/**
     * Parse a text and return the parse tree.
     * @param text the text to parse.
     * @param withComments true to include comment nodes to the tree
     * @return the root node of the parse tree.
     * @throws CheckstyleException if the file is not a Java source.
     */
private static DetailAST parseFileText(FileText text, boolean withComments) throws CheckstyleException {
    final FileContents contents = new FileContents(text);
    final DetailAST result;
    try {
        if (withComments) {
            result = TreeWalker.parseWithComments(contents);
        } else {
            result = TreeWalker.parse(contents);
        }
    } catch (RecognitionException | TokenStreamException ex) {
        final String exceptionMsg = String.format(Locale.ROOT, "%s occurred during the analysis of file %s.", ex.getClass().getSimpleName(), text.getFile().getPath());
        throw new CheckstyleException(exceptionMsg, ex);
    }
    return result;
}
Also used : TokenStreamException(antlr.TokenStreamException) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) RecognitionException(antlr.RecognitionException)

Example 15 with FileContents

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

the class MethodLengthCheck method getLengthOfBlock.

/**
     * Returns length of code only without comments and blank lines.
     * @param openingBrace block opening brace
     * @param closingBrace block closing brace
     * @return number of lines with code for current block
     */
private int getLengthOfBlock(DetailAST openingBrace, DetailAST closingBrace) {
    int length = closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
    if (!countEmpty) {
        final FileContents contents = getFileContents();
        final int lastLine = closingBrace.getLineNo();
        for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
            if (contents.lineIsBlank(i) || contents.lineIsComment(i)) {
                length--;
            }
        }
    }
    return length;
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents)

Aggregations

FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)19 TextBlock (com.puppycrawl.tools.checkstyle.api.TextBlock)6 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)5 FileText (com.puppycrawl.tools.checkstyle.api.FileText)4 ArrayList (java.util.ArrayList)3 RecognitionException (antlr.RecognitionException)2 TokenStreamException (antlr.TokenStreamException)2 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2 List (java.util.List)2 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)1 LineColumn (com.puppycrawl.tools.checkstyle.api.LineColumn)1 LocalizedMessages (com.puppycrawl.tools.checkstyle.api.LocalizedMessages)1 Scope (com.puppycrawl.tools.checkstyle.api.Scope)1 File (java.io.File)1 Test (org.junit.Test)1