Search in sources :

Example 1 with ASTNullLiteral

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

the class RedundantFieldInitializerRule method visit.

public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) {
    // Finals can only be initialized once.
    if (fieldDeclaration.isFinal()) {
        return data;
    }
    // VariableDeclarator/VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal
    for (ASTVariableDeclarator variableDeclarator : fieldDeclaration.findChildrenOfType(ASTVariableDeclarator.class)) {
        if (variableDeclarator.jjtGetNumChildren() > 1) {
            final Node variableInitializer = variableDeclarator.jjtGetChild(1);
            if (variableInitializer.jjtGetChild(0) instanceof ASTExpression) {
                final Node expression = variableInitializer.jjtGetChild(0);
                final Node primaryExpression;
                if (expression.jjtGetNumChildren() == 1) {
                    if (expression.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                        primaryExpression = expression.jjtGetChild(0);
                    } else if (expression.jjtGetChild(0) instanceof ASTCastExpression && expression.jjtGetChild(0).jjtGetChild(1) instanceof ASTPrimaryExpression) {
                        primaryExpression = expression.jjtGetChild(0).jjtGetChild(1);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                final Node primaryPrefix = primaryExpression.jjtGetChild(0);
                if (primaryPrefix.jjtGetNumChildren() == 1 && primaryPrefix.jjtGetChild(0) instanceof ASTLiteral) {
                    final ASTLiteral literal = (ASTLiteral) primaryPrefix.jjtGetChild(0);
                    if (isRef(fieldDeclaration, variableDeclarator)) {
                        // Reference type
                        if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTNullLiteral) {
                            addViolation(data, variableDeclarator);
                        }
                    } else {
                        // Primitive type
                        if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                            // boolean type
                            ASTBooleanLiteral booleanLiteral = (ASTBooleanLiteral) literal.jjtGetChild(0);
                            if (!booleanLiteral.isTrue()) {
                                addViolation(data, variableDeclarator);
                            }
                        } else if (literal.jjtGetNumChildren() == 0) {
                            // numeric type
                            // Note: Not catching NumberFormatException, as
                            // it shouldn't be happening on valid source
                            // code.
                            Number value = -1;
                            if (literal.isIntLiteral()) {
                                value = literal.getValueAsInt();
                            } else if (literal.isLongLiteral()) {
                                value = literal.getValueAsLong();
                            } else if (literal.isFloatLiteral()) {
                                value = literal.getValueAsFloat();
                            } else if (literal.isDoubleLiteral()) {
                                value = literal.getValueAsDouble();
                            } else if (literal.isCharLiteral()) {
                                value = (int) literal.getImage().charAt(1);
                            }
                            if (value.doubleValue() == 0) {
                                addViolation(data, variableDeclarator);
                            }
                        }
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 2 with ASTNullLiteral

use of net.sourceforge.pmd.lang.java.ast.ASTNullLiteral 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)

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)2 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)2 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)2 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)1 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)1 ASTConditionalAndExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression)1 ASTConditionalOrExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression)1 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)1 ASTVariableDeclarator (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator)1