Search in sources :

Example 1 with ASTIfStatement

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

the class InsufficientStringBufferDeclarationRule method storeBlockStatistics.

/**
 * This rule is concerned with IF and Switch blocks. Process the block into
 * a local Map, from which we can later determine which is the longest block
 * inside
 *
 * @param blocks
 *            The map of blocks in the method being investigated
 * @param thisSize
 *            The size of the current block
 * @param block
 *            The block in question
 */
private void storeBlockStatistics(Map<Node, Map<Node, Integer>> blocks, int thisSize, Node block) {
    Node statement = block.jjtGetParent();
    if (block.jjtGetParent() instanceof ASTIfStatement) {
        // Else Ifs are their own subnode in AST. So we have to
        // look a little farther up the tree to find the IF statement
        Node possibleStatement = statement.getFirstParentOfType(ASTIfStatement.class);
        while (possibleStatement instanceof ASTIfStatement) {
            statement = possibleStatement;
            possibleStatement = possibleStatement.getFirstParentOfType(ASTIfStatement.class);
        }
    }
    Map<Node, Integer> thisBranch = blocks.get(statement);
    if (thisBranch == null) {
        thisBranch = new HashMap<>();
        blocks.put(statement, thisBranch);
    }
    Integer x = thisBranch.get(block);
    if (x != null) {
        thisSize += x;
    }
    thisBranch.put(statement, thisSize);
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement)

Example 2 with ASTIfStatement

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

the class DoubleCheckedLockingRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (node.getResultType().isVoid()) {
        return super.visit(node, data);
    }
    ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0);
    if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) {
        return super.visit(node, data);
    }
    List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
    if (rsl.size() != 1) {
        return super.visit(node, data);
    }
    ASTReturnStatement rs = rsl.get(0);
    List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
    ASTPrimaryExpression ape = pel.get(0);
    Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
    String returnVariableName = null;
    if (lastChild instanceof ASTPrimaryPrefix) {
        returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
    }
    // With Java5 and volatile keyword, DCL is no longer an issue
    if (returnVariableName == null || this.volatileFields.contains(returnVariableName)) {
        return super.visit(node, data);
    }
    // field, then it's ok, too
    if (checkLocalVariableUsage(node, returnVariableName)) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> isl = node.findDescendantsOfType(ASTIfStatement.class);
    if (isl.size() == 2) {
        ASTIfStatement is = isl.get(0);
        if (ifVerify(is, returnVariableName)) {
            // find synchronized
            List<ASTSynchronizedStatement> ssl = is.findDescendantsOfType(ASTSynchronizedStatement.class);
            if (ssl.size() == 1) {
                ASTSynchronizedStatement ss = ssl.get(0);
                isl = ss.findDescendantsOfType(ASTIfStatement.class);
                if (isl.size() == 1) {
                    ASTIfStatement is2 = isl.get(0);
                    if (ifVerify(is2, returnVariableName)) {
                        List<ASTStatementExpression> sel = is2.findDescendantsOfType(ASTStatementExpression.class);
                        if (sel.size() == 1) {
                            ASTStatementExpression se = sel.get(0);
                            if (se.jjtGetNumChildren() == 3) {
                                // primaryExpression, AssignmentOperator, Expression
                                if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                                    ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
                                    if (matchName(pe, returnVariableName)) {
                                        if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
                                            addViolation(data, node);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Example 3 with ASTIfStatement

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

the class GuardLogStatementRule method hasGuard.

private boolean hasGuard(ASTPrimaryExpression node, String methodCall, String logLevel) {
    ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
    if (ifStatement == null) {
        return false;
    }
    // an if statement always has an expression
    ASTExpression expr = ifStatement.getFirstChildOfType(ASTExpression.class);
    List<ASTPrimaryPrefix> guardCalls = expr.findDescendantsOfType(ASTPrimaryPrefix.class);
    if (guardCalls.isEmpty()) {
        return false;
    }
    boolean foundGuard = false;
    // check all conditions in the if expression
    for (ASTPrimaryPrefix guardCall : guardCalls) {
        if (guardCall.jjtGetNumChildren() < 1 || guardCall.jjtGetChild(0).getImage() == null) {
            continue;
        }
        String guardMethodCall = getLastPartOfName(guardCall.jjtGetChild(0).getImage());
        boolean guardMethodCallMatches = guardStmtByLogLevel.get(methodCall).contains(guardMethodCall);
        boolean hasArguments = guardCall.jjtGetParent().hasDescendantOfType(ASTArgumentList.class);
        if (guardMethodCallMatches && !hasArguments) {
            // simple case: guard method without arguments found
            foundGuard = true;
        } else if (guardMethodCallMatches && hasArguments) {
            // java.util.logging: guard method with argument. Verify the log level
            String guardArgLogLevel = getLogLevelName(guardCall.jjtGetParent(), guardMethodCall);
            foundGuard = logLevel.equals(guardArgLogLevel);
        }
        if (foundGuard) {
            break;
        }
    }
    return foundGuard;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 4 with ASTIfStatement

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

the class StatementAndBraceFinder method visit.

// ----------------------------------------------------------------------------
// BRANCH OUT
@Override
public Object visit(ASTStatement node, Object data) {
    if (!(data instanceof Structure)) {
        return data;
    }
    Structure dataFlow = (Structure) data;
    if (node.jjtGetParent() instanceof ASTForStatement) {
        this.addForExpressionNode(node, dataFlow);
        dataFlow.pushOnStack(NodeType.FOR_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
        tryToLog(NodeType.FOR_BEFORE_FIRST_STATEMENT, node);
    } else if (node.jjtGetParent() instanceof ASTDoStatement) {
        dataFlow.pushOnStack(NodeType.DO_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
        dataFlow.createNewNode(node.jjtGetParent());
        tryToLog(NodeType.DO_BEFORE_FIRST_STATEMENT, node);
    }
    super.visit(node, data);
    if (node.jjtGetParent() instanceof ASTIfStatement) {
        ASTIfStatement st = (ASTIfStatement) node.jjtGetParent();
        if (!st.hasElse()) {
            dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast());
            tryToLog(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, node);
        } else if (st.hasElse() && !st.jjtGetChild(1).equals(node)) {
            dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast());
            tryToLog(NodeType.ELSE_LAST_STATEMENT, node);
        } else {
            dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT, dataFlow.getLast());
            tryToLog(NodeType.IF_LAST_STATEMENT, node);
        }
    } else if (node.jjtGetParent() instanceof ASTWhileStatement) {
        dataFlow.pushOnStack(NodeType.WHILE_LAST_STATEMENT, dataFlow.getLast());
        tryToLog(NodeType.WHILE_LAST_STATEMENT, node);
    } else if (node.jjtGetParent() instanceof ASTForStatement) {
        dataFlow.pushOnStack(NodeType.FOR_END, dataFlow.getLast());
        tryToLog(NodeType.FOR_END, node);
    } else if (node.jjtGetParent() instanceof ASTLabeledStatement) {
        dataFlow.pushOnStack(NodeType.LABEL_LAST_STATEMENT, dataFlow.getLast());
        tryToLog(NodeType.LABEL_LAST_STATEMENT, node);
    }
    return data;
}
Also used : ASTWhileStatement(net.sourceforge.pmd.lang.java.ast.ASTWhileStatement) ASTDoStatement(net.sourceforge.pmd.lang.java.ast.ASTDoStatement) ASTLabeledStatement(net.sourceforge.pmd.lang.java.ast.ASTLabeledStatement) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) Structure(net.sourceforge.pmd.lang.dfa.Structure) ASTForStatement(net.sourceforge.pmd.lang.java.ast.ASTForStatement)

Example 5 with ASTIfStatement

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

the class StatementAndBraceFinder method visit.

@Override
public Object visit(ASTExpression node, Object data) {
    if (!(data instanceof Structure)) {
        return data;
    }
    Structure dataFlow = (Structure) data;
    String loggerTag = "parent";
    Node parent = node.jjtGetParent();
    // TODO what about throw stmts?
    if (parent instanceof ASTIfStatement) {
        // START IF
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.IF_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.IF_EXPR, node);
    } else if (parent instanceof ASTWhileStatement) {
        // START WHILE
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.WHILE_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.WHILE_EXPR, node);
    } else if (parent instanceof ASTSwitchStatement) {
        // START SWITCH
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.SWITCH_START, node);
    } else if (parent instanceof ASTForStatement) {
        // FOR EXPR
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.FOR_EXPR, node);
    } else if (parent instanceof ASTDoStatement) {
        // DO EXPR
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.DO_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.DO_EXPR, node);
    } else if (parent instanceof ASTAssertStatement) {
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.ASSERT_STATEMENT, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.ASSERT_STATEMENT, node);
    }
    return super.visit(node, data);
}
Also used : ASTWhileStatement(net.sourceforge.pmd.lang.java.ast.ASTWhileStatement) ASTDoStatement(net.sourceforge.pmd.lang.java.ast.ASTDoStatement) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTSwitchStatement(net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement) ASTAssertStatement(net.sourceforge.pmd.lang.java.ast.ASTAssertStatement) Structure(net.sourceforge.pmd.lang.dfa.Structure) ASTForStatement(net.sourceforge.pmd.lang.java.ast.ASTForStatement)

Aggregations

ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)10 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ASTSwitchStatement (net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement)3 Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTDoStatement (net.sourceforge.pmd.lang.java.ast.ASTDoStatement)2 ASTForStatement (net.sourceforge.pmd.lang.java.ast.ASTForStatement)2 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 ASTSynchronizedStatement (net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement)2 ASTWhileStatement (net.sourceforge.pmd.lang.java.ast.ASTWhileStatement)2 ASTAssertStatement (net.sourceforge.pmd.lang.java.ast.ASTAssertStatement)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTLabeledStatement (net.sourceforge.pmd.lang.java.ast.ASTLabeledStatement)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)1 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)1 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)1 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)1