Search in sources :

Example 31 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
 */
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
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();
        // actually may be empty.
        for (int i = openingBrace.getLineNo() - 1; i <= lastLine - 2; i++) {
            if (contents.lineIsBlank(i) || contents.lineIsComment(i)) {
                length--;
            }
        }
    }
    return length;
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents)

Example 32 with FileContents

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

the class EmptyLineSeparatorCheck method hasEmptyLine.

/**
 * Checks, whether there are empty lines within the specified line range. Line numbering is
 * started from 1 for parameter values
 *
 * @param startLine number of the first line in the range
 * @param endLine number of the second line in the range
 * @return {@code true} if found any blank line within the range, {@code false}
 *         otherwise
 */
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private boolean hasEmptyLine(int startLine, int endLine) {
    // Initial value is false - blank line not found
    boolean result = false;
    final FileContents fileContents = getFileContents();
    for (int line = startLine; line <= endLine; line++) {
        // Check, if the line is blank. Lines are numbered from 0, so subtract 1
        if (fileContents.lineIsBlank(line - 1)) {
            result = true;
            break;
        }
    }
    return result;
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents)

Example 33 with FileContents

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

the class TreeWalkerTest method testProcessWithParserThrowable.

@Test
public void testProcessWithParserThrowable() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    treeWalker.configure(createModuleConfig(TypeNameCheck.class));
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    treeWalker.setModuleFactory(factory);
    treeWalker.setupChild(createModuleConfig(TypeNameCheck.class));
    final File file = new File(temporaryFolder, "file.java");
    final List<String> lines = new ArrayList<>();
    lines.add(" classD a {} ");
    final FileText fileText = new FileText(file, lines);
    treeWalker.setFileContents(new FileContents(fileText));
    try {
        treeWalker.processFiltered(file, fileText);
        assertWithMessage("Exception is expected").fail();
    } catch (CheckstyleException exception) {
        assertWithMessage("Error message is unexpected").that(exception.getMessage().contains("occurred while parsing file")).isTrue();
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) ArrayList(java.util.ArrayList) TypeNameCheck(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 34 with FileContents

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

the class TreeWalkerTest method testBehaviourWithOrdinaryAndCommentChecks.

@Test
public void testBehaviourWithOrdinaryAndCommentChecks() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    treeWalker.configure(createModuleConfig(TypeNameCheck.class));
    treeWalker.configure(createModuleConfig(CommentsIndentationCheck.class));
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    treeWalker.setModuleFactory(factory);
    treeWalker.setupChild(createModuleConfig(TypeNameCheck.class));
    treeWalker.setupChild(createModuleConfig(CommentsIndentationCheck.class));
    final File file = new File(temporaryFolder, "file.java");
    final List<String> lines = new ArrayList<>();
    lines.add(" class a%$# {} ");
    final FileText fileText = new FileText(file, lines);
    treeWalker.setFileContents(new FileContents(fileText));
    try {
        treeWalker.processFiltered(file, fileText);
        assertWithMessage("file is not compilable, exception is expected").fail();
    } catch (CheckstyleException exception) {
        final String message = "IllegalStateException occurred while parsing file";
        assertWithMessage("Error message is unexpected").that(exception.getMessage().contains(message)).isTrue();
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) ArrayList(java.util.ArrayList) TypeNameCheck(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck) CommentsIndentationCheck(com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 35 with FileContents

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

the class XpathFileGeneratorAstFilterTest method createTreeWalkerAuditEvent.

private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, Violation violation) throws Exception {
    final File file = new File(getPath(fileName));
    final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
    final FileContents fileContents = new FileContents(fileText);
    final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
    return new TreeWalkerAuditEvent(fileContents, fileName, violation, rootAst);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

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