Search in sources :

Example 26 with FileContents

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

the class MainFrameModel method parseFileWithComments.

/**
     * Parse a file and return the parse tree with comment nodes.
     * @param file the file to parse.
     * @return the root node of the parse tree.
     * @throws IOException if the file could not be read.
     * @throws ANTLRException if the file is not a Java source.
     */
public DetailAST parseFileWithComments(File file) throws IOException, ANTLRException {
    final FileText fileText = getFileText(file);
    final FileContents contents = new FileContents(fileText);
    return TreeWalker.parseWithComments(contents);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 27 with FileContents

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

the class MainFrameModel method parseFile.

/**
     * Parse a file and return the parse tree.
     * @param file the file to parse.
     * @return the root node of the parse tree.
     * @throws IOException if the file could not be read.
     * @throws ANTLRException if the file is not a Java source.
     */
public DetailAST parseFile(File file) throws IOException, ANTLRException {
    final FileText fileText = getFileText(file);
    final FileContents contents = new FileContents(fileText);
    return TreeWalker.parse(contents);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 28 with FileContents

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

the class SuppressWithNearbyCommentFilter method accept.

@Override
public boolean accept(AuditEvent event) {
    boolean accepted = true;
    if (event.getLocalizedMessage() != null) {
        // Lazy update. If the first event for the current file, update file
        // contents and tag suppressions
        final FileContents currentContents = FileContentsHolder.getCurrentFileContents();
        if (getFileContents() != currentContents) {
            setFileContents(currentContents);
            tagSuppressions();
        }
        if (matchesTag(event)) {
            accepted = false;
        }
    }
    return accepted;
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents)

Example 29 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 30 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, FileText fileText) throws CheckstyleException {
    // check if already checked and passed the file
    if (!ordinaryChecks.isEmpty() || !commentChecks.isEmpty()) {
        final FileContents contents = getFileContents();
        final DetailAST rootAST = JavaParser.parse(contents);
        if (!ordinaryChecks.isEmpty()) {
            walk(rootAST, contents, AstState.ORDINARY);
        }
        if (!commentChecks.isEmpty()) {
            final DetailAST astWithComments = JavaParser.appendHiddenCommentNodes(rootAST);
            walk(astWithComments, contents, AstState.WITH_COMMENTS);
        }
        if (filters.isEmpty()) {
            addViolations(violations);
        } else {
            final SortedSet<Violation> filteredViolations = getFilteredViolations(file.getAbsolutePath(), contents, rootAST);
            addViolations(filteredViolations);
        }
        violations.clear();
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Aggregations

FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)44 FileText (com.puppycrawl.tools.checkstyle.api.FileText)18 File (java.io.File)15 Test (org.junit.jupiter.api.Test)11 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)10 TextBlock (com.puppycrawl.tools.checkstyle.api.TextBlock)9 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)8 Violation (com.puppycrawl.tools.checkstyle.api.Violation)7 ArrayList (java.util.ArrayList)7 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)6 TypeNameCheck (com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck)3 RecognitionException (antlr.RecognitionException)2 TokenStreamException (antlr.TokenStreamException)2 List (java.util.List)2 BeforeEach (org.junit.jupiter.api.BeforeEach)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 CommentsIndentationCheck (com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck)1