Search in sources :

Example 36 with DetailAST

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

the class FallThroughCheck method checkLoop.

/**
     * Checks if a given loop terminated by return, throw or,
     * if allowed break, continue.
     * @param ast loop to check
     * @return true if loop is terminated.
     */
private boolean checkLoop(final DetailAST ast) {
    final DetailAST loopBody;
    if (ast.getType() == TokenTypes.LITERAL_DO) {
        final DetailAST lparen = ast.findFirstToken(TokenTypes.DO_WHILE);
        loopBody = lparen.getPreviousSibling();
    } else {
        final DetailAST rparen = ast.findFirstToken(TokenTypes.RPAREN);
        loopBody = rparen.getNextSibling();
    }
    return isTerminated(loopBody, false, false);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 37 with DetailAST

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

the class FallThroughCheck method visitToken.

@Override
public void visitToken(DetailAST ast) {
    final DetailAST nextGroup = ast.getNextSibling();
    final boolean isLastGroup = nextGroup.getType() != TokenTypes.CASE_GROUP;
    if (!isLastGroup || checkLastCaseGroup) {
        final DetailAST slist = ast.findFirstToken(TokenTypes.SLIST);
        if (slist != null && !isTerminated(slist, true, true) && !hasFallThroughComment(ast, nextGroup)) {
            if (isLastGroup) {
                log(ast, MSG_FALL_THROUGH_LAST);
            } else {
                log(nextGroup, MSG_FALL_THROUGH);
            }
        }
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 38 with DetailAST

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

the class FallThroughCheck method checkSwitch.

/**
     * Checks if a given switch terminated by return, throw or,
     * if allowed break, continue.
     * @param literalSwitchAst loop to check
     * @param useContinue should we consider continue as terminator.
     * @return true if switch is terminated.
     */
private boolean checkSwitch(final DetailAST literalSwitchAst, boolean useContinue) {
    DetailAST caseGroup = literalSwitchAst.findFirstToken(TokenTypes.CASE_GROUP);
    boolean isTerminated = caseGroup != null;
    while (isTerminated && caseGroup.getType() != TokenTypes.RCURLY) {
        final DetailAST caseBody = caseGroup.findFirstToken(TokenTypes.SLIST);
        isTerminated = caseBody != null && isTerminated(caseBody, false, useContinue);
        caseGroup = caseGroup.getNextSibling();
    }
    return isTerminated;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 39 with DetailAST

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

the class FinalLocalVariableCheck method updateUninitializedVariables.

/**
     * Update current scope data uninitialized variable according to the previous scope data.
     * @param prevScopeUnitializedVariableData variable for previous stack of uninitialized
     *     variables
     */
// -@cs[CyclomaticComplexity] Breaking apart will damage encapsulation.
private void updateUninitializedVariables(Deque<DetailAST> prevScopeUnitializedVariableData) {
    // Check for only previous scope
    for (DetailAST variable : prevScopeUnitializedVariableData) {
        for (ScopeData scopeData : scopeStack) {
            final FinalVariableCandidate candidate = scopeData.scope.get(variable.getText());
            DetailAST storedVariable = null;
            if (candidate != null) {
                storedVariable = candidate.variableIdent;
            }
            if (storedVariable != null && isSameVariables(storedVariable, variable) && !scopeData.uninitializedVariables.contains(storedVariable)) {
                scopeData.uninitializedVariables.push(variable);
            }
        }
    }
    // Check for rest of the scope
    for (Deque<DetailAST> unitializedVariableData : prevScopeUninitializedVariables) {
        for (DetailAST variable : unitializedVariableData) {
            for (ScopeData scopeData : scopeStack) {
                final FinalVariableCandidate candidate = scopeData.scope.get(variable.getText());
                DetailAST storedVariable = null;
                if (candidate != null) {
                    storedVariable = candidate.variableIdent;
                }
                if (storedVariable != null && isSameVariables(storedVariable, variable) && !scopeData.uninitializedVariables.contains(storedVariable)) {
                    scopeData.uninitializedVariables.push(variable);
                }
            }
        }
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 40 with DetailAST

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

the class FinalLocalVariableCheck method isSameVariables.

/**
     * Check if both the Variables are same.
     * @param ast1 Variable to compare
     * @param ast2 Variable to compare
     * @return true if both the variables are same, otherwise false
     */
private static boolean isSameVariables(DetailAST ast1, DetailAST ast2) {
    final DetailAST classOrMethodOfAst1 = findFirstUpperNamedBlock(ast1);
    final DetailAST classOrMethodOfAst2 = findFirstUpperNamedBlock(ast2);
    return classOrMethodOfAst1 == classOrMethodOfAst2;
}
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