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