use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class NpathBaseVisitor method visit.
@Override
public Object visit(ASTReturnStatement node, Object data) {
// return statements are valued at 1, or the value of the boolean expression
ASTExpression expr = node.getFirstChildOfType(ASTExpression.class);
if (expr == null) {
return 1;
}
int boolCompReturn = CycloMetric.booleanExpressionComplexity(expr);
int conditionalExpressionComplexity = multiplyChildrenComplexities(expr, data);
if (conditionalExpressionComplexity > 1) {
boolCompReturn += conditionalExpressionComplexity;
}
return boolCompReturn > 0 ? boolCompReturn : 1;
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class ForLoopCanBeForeachRule method visit.
@Override
public Object visit(ASTForStatement node, Object data) {
final ASTForInit init = node.getFirstChildOfType(ASTForInit.class);
final ASTForUpdate update = node.getFirstChildOfType(ASTForUpdate.class);
final ASTExpression guardCondition = node.getFirstChildOfType(ASTExpression.class);
if (init == null && update == null || guardCondition == null) {
return data;
}
Entry<VariableNameDeclaration, List<NameOccurrence>> indexDecl = getIndexVarDeclaration(init, update);
if (indexDecl == null) {
return data;
}
List<NameOccurrence> occurrences = indexDecl.getValue();
VariableNameDeclaration index = indexDecl.getKey();
if (TypeHelper.isA(index, Iterator.class)) {
Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = getIterableDeclOfIteratorLoop(index, node.getScope());
if (iterableInfo != null && isReplaceableIteratorLoop(indexDecl, guardCondition, iterableInfo, node)) {
addViolation(data, node);
}
return data;
}
if (occurrences == null || !"int".equals(index.getTypeImage()) || !indexStartsAtZero(index)) {
return data;
}
String itName = index.getName();
String iterableName = getIterableNameOrNullToAbort(guardCondition, itName);
if (!isForUpdateSimpleEnough(update, itName) || iterableName == null) {
return data;
}
Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = findDeclaration(iterableName, node.getScope());
VariableNameDeclaration iterableDeclaration = iterableInfo == null ? null : iterableInfo.getKey();
if (iterableDeclaration == null) {
return data;
}
if (iterableDeclaration.isArray() && isReplaceableArrayLoop(node, occurrences, iterableDeclaration)) {
addViolation(data, node);
} else if (iterableDeclaration.getTypeImage() != null && iterableDeclaration.getTypeImage().matches("List|ArrayList|LinkedList") && isReplaceableListLoop(node, occurrences, iterableDeclaration)) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression 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);
}
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class NpathBaseVisitor method visit.
@Override
public Object visit(ASTConditionalExpression node, Object data) {
if (node.isTernary()) {
ASTExpression wrapper = new ASTExpression(Integer.MAX_VALUE);
wrapper.jjtAddChild(node.jjtGetChild(0), 0);
int boolCompTernary = CycloMetric.booleanExpressionComplexity(wrapper);
return boolCompTernary + sumChildrenComplexities(node, data) - 1;
}
return 1;
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class MethodTypeResolution method selectMethodsSecondPhase.
/**
* Look for methods be method conversion.
* https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.3
*/
public static List<MethodType> selectMethodsSecondPhase(List<MethodType> methodsToSearch, ASTArgumentList argList) {
// TODO: check if explicit type arguments are applicable to the type parameter bounds
List<MethodType> selectedMethods = new ArrayList<>();
final int argCount = argList == null ? 0 : argList.jjtGetNumChildren();
for (int methodIndex = 0; methodIndex < methodsToSearch.size(); ++methodIndex) {
MethodType methodType = methodsToSearch.get(methodIndex);
if (!methodType.isParameterized()) {
throw new ResolutionFailedException();
}
// vararg methods are considered fixed arity here, see 3rd phase
if (getArity(methodType.getMethod()) == argCount) {
// check method convertability of each argument to the corresponding parameter
boolean methodIsApplicable = true;
// try each arguments if it's method convertible
for (int argIndex = 0; argIndex < argCount; ++argIndex) {
if (!isMethodConvertible(methodType.getParameterTypes().get(argIndex), (ASTExpression) argList.jjtGetChild(argIndex))) {
methodIsApplicable = false;
break;
}
// TODO: add unchecked conversion in an else if branch
}
if (methodIsApplicable) {
selectedMethods.add(methodType);
}
}
}
return selectedMethods;
}
Aggregations