Search in sources :

Example 6 with ASTIfStatement

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

the class InsufficientStringBufferDeclarationRule method getFirstParentBlock.

/**
 * Locate the block that the given node is in, if any
 *
 * @param node
 *            The node we're looking for a parent of
 * @return Node - The node that corresponds to any block that may be a
 *         parent of this object
 */
private Node getFirstParentBlock(Node node) {
    Node parentNode = node.jjtGetParent();
    Node lastNode = node;
    while (parentNode != null && !BLOCK_PARENTS.contains(parentNode.getClass())) {
        lastNode = parentNode;
        parentNode = parentNode.jjtGetParent();
    }
    if (parentNode instanceof ASTIfStatement) {
        parentNode = lastNode;
    } else if (parentNode instanceof ASTSwitchStatement) {
        parentNode = getSwitchParent(parentNode, lastNode);
    }
    return parentNode;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTSwitchStatement(net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement)

Example 7 with ASTIfStatement

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

the class NonThreadSafeSingletonRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class);
    for (ASTIfStatement ifStatement : ifStatements) {
        if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
            if (!ifStatement.hasDescendantOfType(ASTNullLiteral.class)) {
                continue;
            }
            ASTName n = ifStatement.getFirstDescendantOfType(ASTName.class);
            if (n == null || !fieldDecls.containsKey(n.getImage())) {
                continue;
            }
            List<ASTAssignmentOperator> assigmnents = ifStatement.findDescendantsOfType(ASTAssignmentOperator.class);
            boolean violation = false;
            for (int ix = 0; ix < assigmnents.size(); ix++) {
                ASTAssignmentOperator oper = assigmnents.get(ix);
                if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
                    continue;
                }
                ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
                if (expr.jjtGetChild(0) instanceof ASTPrimaryExpression && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetNumChildren() == 1 && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix) {
                    ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
                    String name = null;
                    if (pp.usesThisModifier()) {
                        ASTPrimarySuffix priSuf = expr.getFirstDescendantOfType(ASTPrimarySuffix.class);
                        name = priSuf.getImage();
                    } else {
                        ASTName astName = (ASTName) pp.jjtGetChild(0);
                        name = astName.getImage();
                    }
                    if (fieldDecls.containsKey(name)) {
                        violation = true;
                    }
                }
            }
            if (violation) {
                addViolation(data, ifStatement);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 8 with ASTIfStatement

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

the class ConsecutiveLiteralAppendsRule method getFirstParentBlock.

/**
 * Get the first parent. Keep track of the last node though. For If
 * statements it's the only way we can differentiate between if's and else's
 * For switches it's the only way we can differentiate between switches
 *
 * @param node
 *            The node to check
 * @return The first parent block
 */
private Node getFirstParentBlock(Node node) {
    Node parentNode = node.jjtGetParent();
    Node lastNode = node;
    while (parentNode != null && !BLOCK_PARENTS.contains(parentNode.getClass())) {
        lastNode = parentNode;
        parentNode = parentNode.jjtGetParent();
    }
    if (parentNode instanceof ASTIfStatement) {
        parentNode = lastNode;
    } else if (parentNode instanceof ASTSwitchStatement) {
        parentNode = getSwitchParent(parentNode, lastNode);
    }
    return parentNode;
}
Also used : TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTSwitchStatement(net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement)

Example 9 with ASTIfStatement

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

the class CloseResourceRule method findIfStatement.

private ASTIfStatement findIfStatement(ASTBlock enclosingBlock, Node node) {
    ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
    List<ASTIfStatement> allIfStatements = enclosingBlock.findDescendantsOfType(ASTIfStatement.class);
    if (ifStatement != null && allIfStatements.contains(ifStatement)) {
        return ifStatement;
    }
    return null;
}
Also used : ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement)

Example 10 with ASTIfStatement

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

the class ScopeCreationVisitorTest method testScopesAreCreated.

@Test
public void testScopesAreCreated() {
    parseCode(TEST1);
    ASTIfStatement n = acu.findDescendantsOfType(ASTIfStatement.class).get(0);
    assertTrue(n.getScope() instanceof LocalScope);
}
Also used : ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) Test(org.junit.Test)

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