Search in sources :

Example 6 with ASTReturnStatement

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

Example 7 with ASTReturnStatement

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

the class OnlyOneReturnRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (node.isAbstract()) {
        return data;
    }
    List<ASTReturnStatement> returnNodes = node.findDescendantsOfType(ASTReturnStatement.class);
    if (returnNodes.size() > 1) {
        for (Iterator<ASTReturnStatement> i = returnNodes.iterator(); i.hasNext(); ) {
            Node problem = i.next();
            // skip the last one, it's OK
            if (!i.hasNext()) {
                continue;
            }
            addViolation(data, problem);
        }
    }
    return data;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Aggregations

ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)7 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)4 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)2 ArrayList (java.util.ArrayList)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTSynchronizedStatement (net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement)1 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)1