Search in sources :

Example 1 with FileContents

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

the class IllegalInstantiationCheckTest method testNullClassLoader.

@Test
public void testNullClassLoader() throws Exception {
    final DetailAST exprAst = new DetailAST();
    exprAst.setType(TokenTypes.EXPR);
    final DetailAST newAst = new DetailAST();
    newAst.setType(TokenTypes.LITERAL_NEW);
    newAst.setLineNo(1);
    newAst.setColumnNo(1);
    final DetailAST identAst = new DetailAST();
    identAst.setType(TokenTypes.IDENT);
    identAst.setText("Boolean");
    final DetailAST lparenAst = new DetailAST();
    lparenAst.setType(TokenTypes.LPAREN);
    final DetailAST elistAst = new DetailAST();
    elistAst.setType(TokenTypes.ELIST);
    final DetailAST rparenAst = new DetailAST();
    rparenAst.setType(TokenTypes.RPAREN);
    exprAst.addChild(newAst);
    newAst.addChild(identAst);
    identAst.setNextSibling(lparenAst);
    lparenAst.setNextSibling(elistAst);
    elistAst.setNextSibling(rparenAst);
    final IllegalInstantiationCheck check = new IllegalInstantiationCheck();
    final File inputFile = new File(getNonCompilablePath("InputIllegalInstantiationLang.java"));
    check.setFileContents(new FileContents(new FileText(inputFile, "UTF-8")));
    check.configure(createCheckConfig(IllegalInstantiationCheck.class));
    check.setMessages(new LocalizedMessages());
    check.setClasses("java.lang.Boolean");
    check.visitToken(newAst);
    check.finishTree(newAst);
}
Also used : LocalizedMessages(com.puppycrawl.tools.checkstyle.api.LocalizedMessages) 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) Test(org.junit.Test)

Example 2 with FileContents

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

the class SuppressionCommentFilter 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();
        }
        final Tag matchTag = findNearestMatch(event);
        accepted = matchTag == null || matchTag.isReportingOn();
    }
    return accepted;
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents)

Example 3 with FileContents

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

the class UnusedImports method processJavaDocLinkParameters.

protected void processJavaDocLinkParameters(DetailAST aAST) {
    final FileContents contents = getFileContents();
    final int lineNo = aAST.getLineNo();
    final TextBlock cmt = contents.getJavadocBefore(lineNo);
    if (cmt != null) {
        final JavadocTags tags = JavaDocUtil.getJavadocTags(cmt, JavadocUtils.JavadocTagType.ALL);
        for (final JavadocTag tag : tags.getValidTags()) {
            processJavaDocTag(tag);
        }
        for (final InvalidJavadocTag tag : tags.getInvalidTags()) {
            log(tag.getLine(), tag.getCol(), "import.invalidJavaDocTag", tag.getName());
        }
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) TextBlock(com.puppycrawl.tools.checkstyle.api.TextBlock) JavadocTags(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags)

Example 4 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 5 with FileContents

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

the class JavaParser method parseFileText.

/**
 * Parse a text and return the parse tree.
 *
 * @param text the text to parse
 * @param options {@link Options} to control inclusion of comment nodes
 * @return the root node of the parse tree
 * @throws CheckstyleException if the text is not a valid Java source
 */
public static DetailAST parseFileText(FileText text, Options options) throws CheckstyleException {
    final FileContents contents = new FileContents(text);
    final DetailAST ast = parse(contents);
    if (options == Options.WITH_COMMENTS) {
        appendHiddenCommentNodes(ast);
    }
    return ast;
}
Also used : 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