Search in sources :

Example 1 with ASTSwitchLabel

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

the class StdCyclomaticComplexityRule method visit.

@Override
public Object visit(ASTSwitchStatement node, Object data) {
    Entry entry = entryStack.peek();
    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 generally not considered a decision (same as
            // "else")
            ASTSwitchLabel sl = (ASTSwitchLabel) childNode;
            if (!sl.isDefault()) {
                childNode = node.jjtGetChild(n + 1);
                if (childNode instanceof ASTBlockStatement) {
                    entry.bumpDecisionPoints();
                }
            }
        }
    }
    super.visit(node, data);
    return data;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTSwitchLabel(net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)

Example 2 with ASTSwitchLabel

use of net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel 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 3 with ASTSwitchLabel

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

the class NpathBaseVisitor method visit.

@Override
public Object visit(ASTSwitchStatement node, Object data) {
    // bool_comp of switch + sum(npath(case_range))
    int boolCompSwitch = CycloMetric.booleanExpressionComplexity(node.getFirstChildOfType(ASTExpression.class));
    int npath = 0;
    int caseRange = 0;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        JavaNode n = (JavaNode) node.jjtGetChild(i);
        // Fall-through labels count as 1 for complexity
        if (n instanceof ASTSwitchLabel) {
            npath += caseRange;
            caseRange = 1;
        } else {
            Integer complexity = (Integer) n.jjtAccept(this, data);
            caseRange *= complexity;
        }
    }
    // add in npath of last label
    npath += caseRange;
    return boolCompSwitch + npath;
}
Also used : ASTSwitchLabel(net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 4 with ASTSwitchLabel

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

the class InsufficientStringBufferDeclarationRule method getSwitchParent.

/**
 * Determine which SwitchLabel we belong to inside a switch
 *
 * @param parentNode
 *            The parent node we're looking at
 * @param lastNode
 *            The last node processed
 * @return The parent node for the switch statement
 */
private static Node getSwitchParent(Node parentNode, Node lastNode) {
    int allChildren = parentNode.jjtGetNumChildren();
    ASTSwitchLabel label = null;
    for (int ix = 0; ix < allChildren; ix++) {
        Node n = parentNode.jjtGetChild(ix);
        if (n instanceof ASTSwitchLabel) {
            label = (ASTSwitchLabel) n;
        } else if (n.equals(lastNode)) {
            parentNode = label;
            break;
        }
    }
    return parentNode;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTSwitchLabel(net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)

Example 5 with ASTSwitchLabel

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

the class ConsecutiveLiteralAppendsRule method getSwitchParent.

/**
 * Determine which SwitchLabel we belong to inside a switch
 *
 * @param parentNode
 *            The parent node we're looking at
 * @param lastNode
 *            The last node processed
 * @return The parent node for the switch statement
 */
private Node getSwitchParent(Node parentNode, Node lastNode) {
    int allChildren = parentNode.jjtGetNumChildren();
    Node result = parentNode;
    ASTSwitchLabel label = null;
    for (int ix = 0; ix < allChildren; ix++) {
        Node n = result.jjtGetChild(ix);
        if (n instanceof ASTSwitchLabel) {
            label = (ASTSwitchLabel) n;
        } else if (n.equals(lastNode)) {
            result = label;
            break;
        }
    }
    return result;
}
Also used : TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTSwitchLabel(net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)

Aggregations

ASTSwitchLabel (net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel)5 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1