Search in sources :

Example 1 with ASTStatement

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

the class NpathBaseVisitor method visit.

@Override
public Object visit(ASTIfStatement node, Object data) {
    // (npath of if + npath of else (or 1) + bool_comp of if) * npath of next
    List<ASTStatement> statementChildren = node.findChildrenOfType(ASTStatement.class);
    // add path for not taking if
    int complexity = node.hasElse() ? 0 : 1;
    for (ASTStatement element : statementChildren) {
        complexity += (Integer) element.jjtAccept(this, data);
    }
    int boolCompIf = CycloMetric.booleanExpressionComplexity(node.getFirstChildOfType(ASTExpression.class));
    return boolCompIf + complexity;
}
Also used : ASTStatement(net.sourceforge.pmd.lang.java.ast.ASTStatement) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 2 with ASTStatement

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

the class StatementAndBraceFinder method addForExpressionNode.

/*
     * The method handles the special "for" loop. It creates always an
     * expression node even if the loop looks like for(;;).
     */
private void addForExpressionNode(Node node, Structure dataFlow) {
    ASTForStatement parent = (ASTForStatement) node.jjtGetParent();
    boolean hasExpressionChild = false;
    boolean hasForInitNode = false;
    boolean hasForUpdateNode = false;
    for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
        if (parent.jjtGetChild(i) instanceof ASTExpression) {
            hasExpressionChild = true;
        } else if (parent.jjtGetChild(i) instanceof ASTForUpdate) {
            hasForUpdateNode = true;
        } else if (parent.jjtGetChild(i) instanceof ASTForInit) {
            hasForInitNode = true;
        }
    }
    if (!hasExpressionChild) {
        if (node instanceof ASTForInit) {
            dataFlow.createNewNode(node);
            dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
            tryToLog(NodeType.FOR_EXPR, node);
        } else if (node instanceof ASTForUpdate) {
            if (!hasForInitNode) {
                dataFlow.createNewNode(node);
                dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
                tryToLog(NodeType.FOR_EXPR, node);
            }
        } else if (node instanceof ASTStatement) {
            if (!hasForInitNode && !hasForUpdateNode) {
                dataFlow.createNewNode(node);
                dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
                tryToLog(NodeType.FOR_EXPR, node);
            }
        }
    }
}
Also used : ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit) ASTForStatement(net.sourceforge.pmd.lang.java.ast.ASTForStatement) ASTForUpdate(net.sourceforge.pmd.lang.java.ast.ASTForUpdate) ASTStatement(net.sourceforge.pmd.lang.java.ast.ASTStatement) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Aggregations

ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTStatement (net.sourceforge.pmd.lang.java.ast.ASTStatement)2 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)1 ASTForStatement (net.sourceforge.pmd.lang.java.ast.ASTForStatement)1 ASTForUpdate (net.sourceforge.pmd.lang.java.ast.ASTForUpdate)1