Search in sources :

Example 6 with ASTBlockStatement

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

the class PrematureDeclarationRule method blocksAfter.

/**
 * Returns all the blocks found right after the node supplied within the its
 * current scope.
 *
 * @param block
 *            SimpleJavaNode
 * @param node
 *            SimpleNode
 * @return List
 */
private static List<ASTBlockStatement> blocksAfter(AbstractJavaNode block, AbstractJavaNode node) {
    int count = block.jjtGetNumChildren();
    int start = indexOf(block, node.jjtGetParent()) + 1;
    List<ASTBlockStatement> nextBlocks = new ArrayList<>(count);
    for (int i = start; i < count; i++) {
        Node maybeBlock = block.jjtGetChild(i);
        if (maybeBlock instanceof ASTBlockStatement) {
            nextBlocks.add((ASTBlockStatement) maybeBlock);
        }
    }
    return nextBlocks;
}
Also used : ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ArrayList(java.util.ArrayList)

Example 7 with ASTBlockStatement

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

the class CycloBaseVisitor method visit.

@Override
public Object visit(ASTSwitchStatement node, Object data) {
    int childCount = node.jjtGetNumChildren();
    int lastIndex = childCount - 1;
    for (int n = 0; n < lastIndex; n++) {
        Node childNode = node.jjtGetChild(n);
        if (childNode instanceof ASTSwitchLabel) {
            // default is not considered a decision (same as "else")
            ASTSwitchLabel sl = (ASTSwitchLabel) childNode;
            if (!sl.isDefault()) {
                // check the label is not empty
                childNode = node.jjtGetChild(n + 1);
                if (childNode instanceof ASTBlockStatement) {
                    ((MutableInt) data).increment();
                }
            }
        }
    }
    super.visit(node, data);
    return data;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ASTSwitchLabel(net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)

Example 8 with ASTBlockStatement

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

the class JUnitUseExpectedRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    List<ASTTryStatement> catches = node.findDescendantsOfType(ASTTryStatement.class);
    List<Node> found = new ArrayList<>();
    if (catches.isEmpty()) {
        return found;
    }
    for (ASTTryStatement trySt : catches) {
        ASTCatchStatement cStatement = getCatch(trySt);
        if (cStatement != null) {
            ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1);
            if (block.jjtGetNumChildren() != 0) {
                continue;
            }
            List<ASTBlockStatement> blocks = trySt.jjtGetChild(0).findDescendantsOfType(ASTBlockStatement.class);
            if (blocks.isEmpty()) {
                continue;
            }
            ASTBlockStatement st = blocks.get(blocks.size() - 1);
            ASTName name = st.getFirstDescendantOfType(ASTName.class);
            if (name != null && st.equals(name.getNthParent(5)) && "fail".equals(name.getImage())) {
                found.add(name);
                continue;
            }
            ASTThrowStatement th = st.getFirstDescendantOfType(ASTThrowStatement.class);
            if (th != null && st.equals(th.getNthParent(2))) {
                found.add(th);
                continue;
            }
        }
    }
    return found;
}
Also used : ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTThrowStatement(net.sourceforge.pmd.lang.java.ast.ASTThrowStatement) Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ArrayList(java.util.ArrayList) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)

Example 9 with ASTBlockStatement

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

the class InefficientStringBufferingRule method visit.

@Override
public Object visit(ASTAdditiveExpression node, Object data) {
    ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
    if (bs == null) {
        return data;
    }
    int immediateLiterals = 0;
    int immediateStringLiterals = 0;
    List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
    for (ASTLiteral literal : nodes) {
        if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
            immediateLiterals++;
            if (literal.isStringLiteral()) {
                immediateStringLiterals++;
            }
        }
        if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
            return data;
        }
    }
    if (immediateLiterals > 1) {
        return data;
    }
    // if literal + public static final, return
    List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
    for (ASTName name : nameNodes) {
        if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
            VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
            AccessNode accessNodeParent = vnd.getAccessNodeParent();
            if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
                return data;
            }
        }
    }
    // if literal primitive type and not strings variables, then return
    boolean stringFound = false;
    for (ASTName name : nameNodes) {
        if (!isPrimitiveType(name) && isStringType(name)) {
            stringFound = true;
            break;
        }
    }
    if (!stringFound && immediateStringLiterals == 0) {
        return data;
    }
    if (bs.isAllocation()) {
        for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
            ASTName name = iterator.next();
            if (!name.getImage().endsWith("length")) {
                break;
            } else if (!iterator.hasNext()) {
                // All names end with length
                return data;
            }
        }
        if (isAllocatedStringBuffer(node)) {
            addViolation(data, node);
        }
    } else if (isInStringBufferOperation(node, 6, "append")) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 10 with ASTBlockStatement

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

the class ConsecutiveAppendsShouldReuseRule method getNextBlockStatementSibling.

private ASTBlockStatement getNextBlockStatementSibling(Node node) {
    Node parent = node.jjtGetParent();
    int childIndex = -1;
    for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
        if (parent.jjtGetChild(i) == node) {
            childIndex = i;
            break;
        }
    }
    if (childIndex + 1 < parent.jjtGetNumChildren()) {
        Node nextSibling = parent.jjtGetChild(childIndex + 1);
        if (nextSibling instanceof ASTBlockStatement) {
            return (ASTBlockStatement) nextSibling;
        }
    }
    return null;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)

Aggregations

ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)13 Node (net.sourceforge.pmd.lang.ast.Node)8 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)4 ArrayList (java.util.ArrayList)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 ASTSwitchLabel (net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTCatchStatement (net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1