use of net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression in project pmd by pmd.
the class CycloMetric method booleanExpressionComplexity.
/**
* Evaluates the number of paths through a boolean expression. This is the total number of {@code &&} and {@code ||}
* operators appearing in the expression. This is used in the calculation of cyclomatic and n-path complexity.
*
* @param expr Expression to analyse
*
* @return The number of paths through the expression
*/
public static int booleanExpressionComplexity(ASTExpression expr) {
if (expr == null) {
return 0;
}
List<ASTConditionalAndExpression> andNodes = expr.findDescendantsOfType(ASTConditionalAndExpression.class);
List<ASTConditionalOrExpression> orNodes = expr.findDescendantsOfType(ASTConditionalOrExpression.class);
int complexity = 0;
for (ASTConditionalOrExpression element : orNodes) {
complexity += element.jjtGetNumChildren() - 1;
}
for (ASTConditionalAndExpression element : andNodes) {
complexity += element.jjtGetNumChildren() - 1;
}
return complexity;
}
use of net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression in project pmd by pmd.
the class BrokenNullCheckRule method checkForViolations.
private void checkForViolations(ASTIfStatement node, Object data, Node conditionalExpression) {
ASTEqualityExpression equalityExpression = conditionalExpression.getFirstChildOfType(ASTEqualityExpression.class);
if (equalityExpression == null) {
return;
}
if (conditionalExpression instanceof ASTConditionalAndExpression && !"==".equals(equalityExpression.getImage())) {
return;
}
if (conditionalExpression instanceof ASTConditionalOrExpression && !"!=".equals(equalityExpression.getImage())) {
return;
}
ASTNullLiteral nullLiteral = equalityExpression.getFirstDescendantOfType(ASTNullLiteral.class);
if (nullLiteral == null) {
// No null check
return;
}
// because things get too complex
if (conditionalExpression.hasDescendantOfType(ASTAssignmentOperator.class)) {
return;
}
// Find the expression used in the null compare
ASTPrimaryExpression nullCompareExpression = findNullCompareExpression(equalityExpression);
if (nullCompareExpression == null) {
// No good null check
return;
}
// Now we find the expression to compare to and do the comparison
for (int i = 0; i < conditionalExpression.jjtGetNumChildren(); i++) {
Node conditionalSubnode = conditionalExpression.jjtGetChild(i);
// We skip the null compare branch
ASTEqualityExpression nullEqualityExpression = nullLiteral.getFirstParentOfType(ASTEqualityExpression.class);
if (conditionalSubnode.equals(nullEqualityExpression)) {
continue;
}
ASTPrimaryExpression conditionalPrimaryExpression;
if (conditionalSubnode instanceof ASTPrimaryExpression) {
conditionalPrimaryExpression = (ASTPrimaryExpression) conditionalSubnode;
} else {
// The ASTPrimaryExpression is hidden (in a negation, braces or
// EqualityExpression)
conditionalPrimaryExpression = conditionalSubnode.getFirstDescendantOfType(ASTPrimaryExpression.class);
}
if (primaryExpressionsAreEqual(nullCompareExpression, conditionalPrimaryExpression)) {
// We have a match
addViolation(data, node);
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression in project pmd by pmd.
the class BrokenNullCheckRule method visit.
@Override
public Object visit(ASTIfStatement node, Object data) {
ASTExpression expression = (ASTExpression) node.jjtGetChild(0);
ASTConditionalAndExpression conditionalAndExpression = expression.getFirstDescendantOfType(ASTConditionalAndExpression.class);
if (conditionalAndExpression != null) {
checkForViolations(node, data, conditionalAndExpression);
}
ASTConditionalOrExpression conditionalOrExpression = expression.getFirstDescendantOfType(ASTConditionalOrExpression.class);
if (conditionalOrExpression != null) {
checkForViolations(node, data, conditionalOrExpression);
}
return super.visit(node, data);
}
Aggregations