Search in sources :

Example 1 with ASTUnaryExpressionNotPlusMinus

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

the class SimplifyBooleanReturnsRule method isNodesEqualWithUnaryExpression.

private boolean isNodesEqualWithUnaryExpression(Node n1, Node n2) {
    Node node1;
    Node node2;
    if (n1 instanceof ASTUnaryExpressionNotPlusMinus) {
        node1 = n1.jjtGetChild(0);
    } else {
        node1 = n1;
    }
    if (n2 instanceof ASTUnaryExpressionNotPlusMinus) {
        node2 = n2.jjtGetChild(0);
    } else {
        node2 = n2;
    }
    return isNodesEquals(node1, node2);
}
Also used : ASTUnaryExpressionNotPlusMinus(net.sourceforge.pmd.lang.java.ast.ASTUnaryExpressionNotPlusMinus) Node(net.sourceforge.pmd.lang.ast.Node)

Example 2 with ASTUnaryExpressionNotPlusMinus

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

the class SimplifyBooleanReturnsRule method visit.

public Object visit(ASTIfStatement node, Object data) {
    // that's the case: if..then..return; return;
    if (!node.hasElse() && isIfJustReturnsBoolean(node) && isJustReturnsBooleanAfter(node)) {
        addViolation(data, node);
        return super.visit(node, data);
    }
    // only deal with if..then..else stmts
    if (node.jjtGetNumChildren() != 3) {
        return super.visit(node, data);
    }
    // don't bother if either the if or the else block is empty
    if (node.jjtGetChild(1).jjtGetNumChildren() == 0 || node.jjtGetChild(2).jjtGetNumChildren() == 0) {
        return super.visit(node, data);
    }
    Node returnStatement1 = node.jjtGetChild(1).jjtGetChild(0);
    Node returnStatement2 = node.jjtGetChild(2).jjtGetChild(0);
    if (returnStatement1 instanceof ASTReturnStatement && returnStatement2 instanceof ASTReturnStatement) {
        Node expression1 = returnStatement1.jjtGetChild(0).jjtGetChild(0);
        Node expression2 = returnStatement2.jjtGetChild(0).jjtGetChild(0);
        if (terminatesInBooleanLiteral(returnStatement1) && terminatesInBooleanLiteral(returnStatement2)) {
            addViolation(data, node);
        } else if (expression1 instanceof ASTUnaryExpressionNotPlusMinus ^ expression2 instanceof ASTUnaryExpressionNotPlusMinus) {
            // If they are the same => error
            if (isNodesEqualWithUnaryExpression(expression1, expression2)) {
                // second case:
                // If
                // Expr
                // Statement
                // ReturnStatement
                // UnaryExpressionNotPlusMinus '!'
                // Expression E
                // Statement
                // ReturnStatement
                // Expression E
                // i.e.,
                // if (foo)
                // return !a;
                // else
                // return a;
                addViolation(data, node);
            }
        }
    } else if (hasOneBlockStmt(node.jjtGetChild(1)) && hasOneBlockStmt(node.jjtGetChild(2))) {
        // We have blocks so we must go down three levels (BlockStatement,
        // Statement, ReturnStatement)
        returnStatement1 = returnStatement1.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
        returnStatement2 = returnStatement2.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
        // if we have 2 return;
        if (isSimpleReturn(returnStatement1) && isSimpleReturn(returnStatement2)) {
            // third case
            // If
            // Expr
            // Statement
            // Block
            // BlockStatement
            // Statement
            // ReturnStatement
            // Statement
            // Block
            // BlockStatement
            // Statement
            // ReturnStatement
            // i.e.,
            // if (foo) {
            // return true;
            // } else {
            // return false;
            // }
            addViolation(data, node);
        } else {
            Node expression1 = getDescendant(returnStatement1, 4);
            Node expression2 = getDescendant(returnStatement2, 4);
            if (terminatesInBooleanLiteral(node.jjtGetChild(1).jjtGetChild(0)) && terminatesInBooleanLiteral(node.jjtGetChild(2).jjtGetChild(0))) {
                addViolation(data, node);
            } else if (expression1 instanceof ASTUnaryExpressionNotPlusMinus ^ expression2 instanceof ASTUnaryExpressionNotPlusMinus) {
                // If they are the same => error
                if (isNodesEqualWithUnaryExpression(expression1, expression2)) {
                    // forth case
                    // If
                    // Expr
                    // Statement
                    // Block
                    // BlockStatement
                    // Statement
                    // ReturnStatement
                    // UnaryExpressionNotPlusMinus '!'
                    // Expression E
                    // Statement
                    // Block
                    // BlockStatement
                    // Statement
                    // ReturnStatement
                    // Expression E
                    // i.e.,
                    // if (foo) {
                    // return !a;
                    // } else {
                    // return a;
                    // }
                    addViolation(data, node);
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTUnaryExpressionNotPlusMinus(net.sourceforge.pmd.lang.java.ast.ASTUnaryExpressionNotPlusMinus) Node(net.sourceforge.pmd.lang.ast.Node) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)2 ASTUnaryExpressionNotPlusMinus (net.sourceforge.pmd.lang.java.ast.ASTUnaryExpressionNotPlusMinus)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)1