Search in sources :

Example 26 with DetailAST

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

the class ModifiedControlVariableCheck method leaveToken.

@Override
public void leaveToken(DetailAST ast) {
    switch(ast.getType()) {
        case TokenTypes.FOR_ITERATOR:
            leaveForIter(ast.getParent());
            break;
        case TokenTypes.FOR_EACH_CLAUSE:
            if (!skipEnhancedForLoopVariable) {
                final DetailAST paramDef = ast.findFirstToken(TokenTypes.VARIABLE_DEF);
                leaveForEach(paramDef);
            }
            break;
        case TokenTypes.LITERAL_FOR:
            if (!getCurrentVariables().isEmpty()) {
                leaveForDef(ast);
            }
            break;
        case TokenTypes.OBJBLOCK:
            exitBlock();
            break;
        case TokenTypes.ASSIGN:
        case TokenTypes.PLUS_ASSIGN:
        case TokenTypes.MINUS_ASSIGN:
        case TokenTypes.STAR_ASSIGN:
        case TokenTypes.DIV_ASSIGN:
        case TokenTypes.MOD_ASSIGN:
        case TokenTypes.SR_ASSIGN:
        case TokenTypes.BSR_ASSIGN:
        case TokenTypes.SL_ASSIGN:
        case TokenTypes.BAND_ASSIGN:
        case TokenTypes.BXOR_ASSIGN:
        case TokenTypes.BOR_ASSIGN:
        case TokenTypes.INC:
        case TokenTypes.POST_INC:
        case TokenTypes.DEC:
        case TokenTypes.POST_DEC:
            //we need that Tokens only at visitToken()
            break;
        default:
            throw new IllegalStateException(ILLEGAL_TYPE_OF_TOKEN + ast);
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 27 with DetailAST

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

the class ModifiedControlVariableCheck method getForIteratorVariables.

/**
     * Get all variables which for loop iterating part change in every loop.
     * @param ast for loop literal(TokenTypes.LITERAL_FOR)
     * @return names of variables change in iterating part of for
     */
private static Set<String> getForIteratorVariables(DetailAST ast) {
    final Set<String> iteratorVariables = new HashSet<>();
    final DetailAST forIteratorAST = ast.findFirstToken(TokenTypes.FOR_ITERATOR);
    final DetailAST forUpdateListAST = forIteratorAST.findFirstToken(TokenTypes.ELIST);
    findChildrenOfExpressionType(forUpdateListAST).stream().filter(iteratingExpressionAST -> {
        return MUTATION_OPERATIONS.contains(iteratingExpressionAST.getType());
    }).forEach(iteratingExpressionAST -> {
        final DetailAST oneVariableOperatorChild = iteratingExpressionAST.getFirstChild();
        if (oneVariableOperatorChild.getType() == TokenTypes.IDENT) {
            iteratorVariables.add(oneVariableOperatorChild.getText());
        }
    });
    return iteratorVariables;
}
Also used : HashSet(java.util.HashSet) TokenTypes(com.puppycrawl.tools.checkstyle.api.TokenTypes) Arrays(java.util.Arrays) List(java.util.List) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) Set(java.util.Set) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) ArrayDeque(java.util.ArrayDeque) Deque(java.util.Deque) LinkedList(java.util.LinkedList) Collectors(java.util.stream.Collectors) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) HashSet(java.util.HashSet)

Example 28 with DetailAST

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

the class MultipleVariableDeclarationsCheck method visitToken.

@Override
public void visitToken(DetailAST ast) {
    DetailAST nextNode = ast.getNextSibling();
    if (nextNode != null) {
        final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA;
        if (isCommaSeparated || nextNode.getType() == TokenTypes.SEMI) {
            nextNode = nextNode.getNextSibling();
        }
        if (nextNode != null && nextNode.getType() == TokenTypes.VARIABLE_DEF) {
            final DetailAST firstNode = CheckUtils.getFirstNode(ast);
            if (isCommaSeparated) {
                // for more details
                if (ast.getParent().getType() != TokenTypes.FOR_INIT) {
                    log(firstNode, MSG_MULTIPLE_COMMA);
                }
            } else {
                final DetailAST lastNode = getLastNode(ast);
                final DetailAST firstNextNode = CheckUtils.getFirstNode(nextNode);
                if (firstNextNode.getLineNo() == lastNode.getLineNo()) {
                    log(firstNode, MSG_MULTIPLE);
                }
            }
        }
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 29 with DetailAST

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

the class RightCurlyCheck method isAloneOnLine.

/**
     * Checks whether right curly is alone on a line.
     * @param details for validation.
     * @return true if right curly is alone on a line.
     */
private static boolean isAloneOnLine(Details details) {
    final DetailAST rcurly = details.rcurly;
    final DetailAST lcurly = details.lcurly;
    final DetailAST nextToken = details.nextToken;
    return rcurly.getLineNo() != lcurly.getLineNo() && rcurly.getLineNo() != nextToken.getLineNo();
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 30 with DetailAST

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

the class RightCurlyCheck method getDetails.

/**
     * Collects validation details.
     * @param ast detail ast.
     * @return object that contain all details to make a validation.
     */
// -@cs[JavaNCSS] getDetails() method is a huge SWITCH, it has to be monolithic
// -@cs[ExecutableStatementCount] getDetails() method is a huge SWITCH, it has to be monolithic
// -@cs[NPathComplexity] getDetails() method is a huge SWITCH, it has to be monolithic
private static Details getDetails(DetailAST ast) {
    // Attempt to locate the tokens to do the check
    boolean shouldCheckLastRcurly = false;
    DetailAST rcurly = null;
    final DetailAST lcurly;
    DetailAST nextToken;
    switch(ast.getType()) {
        case TokenTypes.LITERAL_TRY:
            if (ast.getFirstChild().getType() == TokenTypes.RESOURCE_SPECIFICATION) {
                lcurly = ast.getFirstChild().getNextSibling();
            } else {
                lcurly = ast.getFirstChild();
            }
            nextToken = lcurly.getNextSibling();
            rcurly = lcurly.getLastChild();
            if (nextToken == null) {
                shouldCheckLastRcurly = true;
                nextToken = getNextToken(ast);
            }
            break;
        case TokenTypes.LITERAL_CATCH:
            nextToken = ast.getNextSibling();
            lcurly = ast.getLastChild();
            rcurly = lcurly.getLastChild();
            if (nextToken == null) {
                shouldCheckLastRcurly = true;
                nextToken = getNextToken(ast);
            }
            break;
        case TokenTypes.LITERAL_IF:
            nextToken = ast.findFirstToken(TokenTypes.LITERAL_ELSE);
            if (nextToken == null) {
                shouldCheckLastRcurly = true;
                nextToken = getNextToken(ast);
                lcurly = ast.getLastChild();
            } else {
                lcurly = nextToken.getPreviousSibling();
            }
            if (lcurly.getType() == TokenTypes.SLIST) {
                rcurly = lcurly.getLastChild();
            }
            break;
        case TokenTypes.LITERAL_ELSE:
        case TokenTypes.LITERAL_FINALLY:
            shouldCheckLastRcurly = true;
            nextToken = getNextToken(ast);
            lcurly = ast.getFirstChild();
            if (lcurly.getType() == TokenTypes.SLIST) {
                rcurly = lcurly.getLastChild();
            }
            break;
        case TokenTypes.CLASS_DEF:
            final DetailAST child = ast.getLastChild();
            lcurly = child.getFirstChild();
            rcurly = child.getLastChild();
            nextToken = ast;
            break;
        case TokenTypes.CTOR_DEF:
        case TokenTypes.STATIC_INIT:
        case TokenTypes.INSTANCE_INIT:
            lcurly = ast.findFirstToken(TokenTypes.SLIST);
            rcurly = lcurly.getLastChild();
            nextToken = getNextToken(ast);
            break;
        case TokenTypes.LITERAL_DO:
            nextToken = ast.findFirstToken(TokenTypes.DO_WHILE);
            lcurly = ast.findFirstToken(TokenTypes.SLIST);
            if (lcurly != null) {
                rcurly = lcurly.getLastChild();
            }
            break;
        default:
            // ATTENTION! We have default here, but we expect case TokenTypes.METHOD_DEF,
            // TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_WHILE only.
            // It has been done to improve coverage to 100%. I couldn't replace it with
            // if-else-if block because code was ugly and didn't pass pmd check.
            lcurly = ast.findFirstToken(TokenTypes.SLIST);
            if (lcurly != null) {
                // SLIST could be absent if method is abstract,
                // and code like "while(true);"
                rcurly = lcurly.getLastChild();
            }
            nextToken = getNextToken(ast);
            break;
    }
    final Details details = new Details();
    details.rcurly = rcurly;
    details.lcurly = lcurly;
    details.nextToken = nextToken;
    details.shouldCheckLastRcurly = shouldCheckLastRcurly;
    return details;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Aggregations

DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)397 Test (org.junit.Test)74 CommonHiddenStreamToken (antlr.CommonHiddenStreamToken)14 FullIdent (com.puppycrawl.tools.checkstyle.api.FullIdent)14 ArrayList (java.util.ArrayList)13 AST (antlr.collections.AST)8 Method (java.lang.reflect.Method)7 LinkedList (java.util.LinkedList)7 Scope (com.puppycrawl.tools.checkstyle.api.Scope)6 HashSet (java.util.HashSet)6 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)5 ArrayDeque (java.util.ArrayDeque)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)3 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 HashMap (java.util.HashMap)3