Search in sources :

Example 6 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 7 with FileContents

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

the class RegexpCheck method isIgnore.

/**
     * Detect ignore situation.
     * @param startLine position of line
     * @param text file text
     * @param start line column
     * @return true is that need to be ignored
     */
private boolean isIgnore(int startLine, FileText text, LineColumn start) {
    final LineColumn end;
    if (matcher.end() == 0) {
        end = text.lineColumn(0);
    } else {
        end = text.lineColumn(matcher.end() - 1);
    }
    final int startColumn = start.getColumn();
    final int endLine = end.getLine();
    final int endColumn = end.getColumn();
    boolean ignore = false;
    if (ignoreComments) {
        final FileContents theFileContents = getFileContents();
        ignore = theFileContents.hasIntersectionWithComment(startLine, startColumn, endLine, endColumn);
    }
    return ignore;
}
Also used : LineColumn(com.puppycrawl.tools.checkstyle.api.LineColumn) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents)

Example 8 with FileContents

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

the class SuppressWithNearbyCommentFilter method tagSuppressions.

/**
     * Collects all the suppression tags for all comments into a list and
     * sorts the list.
     */
private void tagSuppressions() {
    tags.clear();
    final FileContents contents = getFileContents();
    if (checkCPP) {
        tagSuppressions(contents.getSingleLineComments().values());
    }
    if (checkC) {
        final Collection<List<TextBlock>> cComments = contents.getBlockComments().values();
        cComments.forEach(this::tagSuppressions);
    }
    Collections.sort(tags);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 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 10 with FileContents

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

the class SuppressionCommentFilter method tagSuppressions.

/**
     * Collects all the suppression tags for all comments into a list and
     * sorts the list.
     */
private void tagSuppressions() {
    tags.clear();
    final FileContents contents = getFileContents();
    if (checkCPP) {
        tagSuppressions(contents.getSingleLineComments().values());
    }
    if (checkC) {
        final Collection<List<TextBlock>> cComments = contents.getBlockComments().values();
        cComments.forEach(this::tagSuppressions);
    }
    Collections.sort(tags);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) ArrayList(java.util.ArrayList) List(java.util.List)

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