Search in sources :

Example 21 with FileContents

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

the class XpathFileGeneratorAuditListenerTest 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)

Example 22 with FileContents

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

the class JavaAstVisitorTest method testNoStackOverflowOnDeepStringConcat.

/**
 * This test exists to kill surviving mutation from pitest removing expression AST building
 * optimization in {@link JavaAstVisitor#visitBinOp(JavaLanguageParser.BinOpContext)}.
 * We do not use {@link JavaParser#parse(FileContents)} here due to DFA clearing hack.
 *
 * <p>
 * Reason: we have iterative expression AST building to avoid stackoverflow
 * in {@link JavaAstVisitor#visitBinOp(JavaLanguageParser.BinOpContext)}. In actual
 * generated parser, we avoid stackoverflow thanks to the left recursive expression
 * rule (eliminating unnecessary recursive calls to hierarchical expression production rules).
 * However, ANTLR's ParserATNSimulator has no such optimization. So, the number of recursive
 * calls to ParserATNSimulator#closure when calling ParserATNSimulator#clearDFA causes a
 * StackOverflow error. We avoid this by using the single argument constructor (thus not
 * forcing DFA clearing) in this test.
 * </p>
 *
 * @throws Exception if input file does not exist
 */
@Test
public void testNoStackOverflowOnDeepStringConcat() throws Exception {
    final File file = new File(getPath("InputJavaAstVisitorNoStackOverflowOnDeepStringConcat.java"));
    final FileText fileText = new FileText(file, StandardCharsets.UTF_8.name());
    final FileContents contents = new FileContents(fileText);
    final String fullText = contents.getText().getFullText().toString();
    final CharStream codePointCharStream = CharStreams.fromString(fullText);
    final JavaLanguageLexer lexer = new JavaLanguageLexer(codePointCharStream, true);
    lexer.setCommentListener(contents);
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final JavaLanguageParser parser = new JavaLanguageParser(tokenStream);
    final JavaLanguageParser.CompilationUnitContext compilationUnit = parser.compilationUnit();
    // We restrict execution to use limited resources here, so that we can
    // kill surviving pitest mutation from removal of nested binary operation
    // optimization in JavaAstVisitor#visitBinOp. Limited resources (small stack size)
    // ensure that we throw a StackOverflowError if optimization is removed.
    final DetailAST root = TestUtil.getResultWithLimitedResources(() -> new JavaAstVisitor(tokenStream).visit(compilationUnit));
    assertWithMessage("File parsing and AST building should complete successfully.").that(root).isNotNull();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavaLanguageParser(com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) JavaLanguageLexer(com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageLexer) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.jupiter.api.Test)

Example 23 with FileContents

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

the class SuppressWithNearbyCommentFilterTest method getTagsAfterExecution.

/**
 * Calls the filter with a minimal set of inputs and returns a list of
 * {@link SuppressWithNearbyCommentFilter} internal type {@code Tag}.
 * Our goal is 100% test coverage, for this we use white-box testing.
 * So we need access to the implementation details. For this reason,
 * it is necessary to use reflection to gain access to the inner field here.
 *
 * @return {@code Tag} list
 */
private static List<?> getTagsAfterExecution(SuppressWithNearbyCommentFilter filter, String filename, String... lines) {
    final FileContents contents = new FileContents(new FileText(new File(filename), Arrays.asList(lines)));
    contents.reportSingleLineComment(1, 0);
    final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, filename, new Violation(1, null, null, null, null, Object.class, null), null);
    filter.accept(dummyEvent);
    return TestUtil.getInternalState(filter, "tags");
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

Example 24 with FileContents

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

the class SuppressionCommentFilterTest method getTagsAfterExecution.

/**
 * Calls the filter with a minimal set of inputs and returns a list of
 * {@link SuppressionCommentFilter} internal type {@code Tag}.
 * Our goal is 100% test coverage, for this we use white-box testing.
 * So we need access to the implementation details. For this reason,
 * it is necessary to use reflection to gain access to the inner field here.
 *
 * @return {@code Tag} list
 */
private static List<Comparable<Object>> getTagsAfterExecution(SuppressionCommentFilter filter, String filename, String... lines) {
    final FileContents contents = new FileContents(new FileText(new File(filename), Arrays.asList(lines)));
    for (int lineNo = 0; lineNo < lines.length; lineNo++) {
        final int colNo = lines[lineNo].indexOf("//");
        if (colNo >= 0) {
            contents.reportSingleLineComment(lineNo + 1, colNo);
        }
    }
    final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, filename, new Violation(1, null, null, null, null, Object.class, ""), null);
    filter.accept(dummyEvent);
    return TestUtil.getInternalState(filter, "tags");
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

Example 25 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)

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