Search in sources :

Example 1 with ASTConditionalOrExpression

use of net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression in project pmd by pmd.

the class CycloMetric method booleanExpressionComplexity.

/**
 * Evaluates the number of paths through a boolean expression. This is the total number of {@code &&} and {@code ||}
 * operators appearing in the expression. This is used in the calculation of cyclomatic and n-path complexity.
 *
 * @param expr Expression to analyse
 *
 * @return The number of paths through the expression
 */
public static int booleanExpressionComplexity(ASTExpression expr) {
    if (expr == null) {
        return 0;
    }
    List<ASTConditionalAndExpression> andNodes = expr.findDescendantsOfType(ASTConditionalAndExpression.class);
    List<ASTConditionalOrExpression> orNodes = expr.findDescendantsOfType(ASTConditionalOrExpression.class);
    int complexity = 0;
    for (ASTConditionalOrExpression element : orNodes) {
        complexity += element.jjtGetNumChildren() - 1;
    }
    for (ASTConditionalAndExpression element : andNodes) {
        complexity += element.jjtGetNumChildren() - 1;
    }
    return complexity;
}
Also used : ASTConditionalOrExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression) ASTConditionalAndExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression)

Example 2 with ASTConditionalOrExpression

use of net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression in project pmd by pmd.

the class BrokenNullCheckRule method checkForViolations.

private void checkForViolations(ASTIfStatement node, Object data, Node conditionalExpression) {
    ASTEqualityExpression equalityExpression = conditionalExpression.getFirstChildOfType(ASTEqualityExpression.class);
    if (equalityExpression == null) {
        return;
    }
    if (conditionalExpression instanceof ASTConditionalAndExpression && !"==".equals(equalityExpression.getImage())) {
        return;
    }
    if (conditionalExpression instanceof ASTConditionalOrExpression && !"!=".equals(equalityExpression.getImage())) {
        return;
    }
    ASTNullLiteral nullLiteral = equalityExpression.getFirstDescendantOfType(ASTNullLiteral.class);
    if (nullLiteral == null) {
        // No null check
        return;
    }
    // because things get too complex
    if (conditionalExpression.hasDescendantOfType(ASTAssignmentOperator.class)) {
        return;
    }
    // Find the expression used in the null compare
    ASTPrimaryExpression nullCompareExpression = findNullCompareExpression(equalityExpression);
    if (nullCompareExpression == null) {
        // No good null check
        return;
    }
    // Now we find the expression to compare to and do the comparison
    for (int i = 0; i < conditionalExpression.jjtGetNumChildren(); i++) {
        Node conditionalSubnode = conditionalExpression.jjtGetChild(i);
        // We skip the null compare branch
        ASTEqualityExpression nullEqualityExpression = nullLiteral.getFirstParentOfType(ASTEqualityExpression.class);
        if (conditionalSubnode.equals(nullEqualityExpression)) {
            continue;
        }
        ASTPrimaryExpression conditionalPrimaryExpression;
        if (conditionalSubnode instanceof ASTPrimaryExpression) {
            conditionalPrimaryExpression = (ASTPrimaryExpression) conditionalSubnode;
        } else {
            // The ASTPrimaryExpression is hidden (in a negation, braces or
            // EqualityExpression)
            conditionalPrimaryExpression = conditionalSubnode.getFirstDescendantOfType(ASTPrimaryExpression.class);
        }
        if (primaryExpressionsAreEqual(nullCompareExpression, conditionalPrimaryExpression)) {
            // We have a match
            addViolation(data, node);
        }
    }
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTConditionalOrExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTConditionalAndExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression)

Example 3 with ASTConditionalOrExpression

use of net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression in project pmd by pmd.

the class BrokenNullCheckRule method visit.

@Override
public Object visit(ASTIfStatement node, Object data) {
    ASTExpression expression = (ASTExpression) node.jjtGetChild(0);
    ASTConditionalAndExpression conditionalAndExpression = expression.getFirstDescendantOfType(ASTConditionalAndExpression.class);
    if (conditionalAndExpression != null) {
        checkForViolations(node, data, conditionalAndExpression);
    }
    ASTConditionalOrExpression conditionalOrExpression = expression.getFirstDescendantOfType(ASTConditionalOrExpression.class);
    if (conditionalOrExpression != null) {
        checkForViolations(node, data, conditionalOrExpression);
    }
    return super.visit(node, data);
}
Also used : ASTConditionalOrExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression) ASTConditionalAndExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Aggregations

ASTConditionalAndExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression)3 ASTConditionalOrExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression)3 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1