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;
}
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);
}
}
}
}
Aggregations