use of org.sonar.plugins.java.api.tree.ForEachStatement in project sonar-java by SonarSource.
the class KeySetInsteadOfEntrySetCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
ForEachStatement forEachTree = (ForEachStatement) tree;
ExpressionTree expressionTree = forEachTree.expression();
if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodTree = (MethodInvocationTree) expressionTree;
Symbol ownerSymbol = getOwnerSymbol(methodTree);
if (ownerSymbol != null && MAP_KEYSET_METHOD.matches(methodTree)) {
new GetUsageVisitor().isCallingGetWithSymbol(forEachTree, forEachTree.variable().symbol(), ownerSymbol);
}
}
}
}
use of org.sonar.plugins.java.api.tree.ForEachStatement in project sonar-java by SonarSource.
the class JavaTreeModelTest method annotations_in_for_each_statements.
@Test
public void annotations_in_for_each_statements() {
ForEachStatement tree = (ForEachStatement) firstMethodFirstStatement("class C { void foo(Object[] values) { for(@Nullable Object value : values) { } } }");
assertThat(tree.variable().modifiers().annotations()).hasSize(1);
}
use of org.sonar.plugins.java.api.tree.ForEachStatement in project sonar-java by SonarSource.
the class JavaTreeModelTest method enhanced_for_statement.
@Test
public void enhanced_for_statement() {
ForEachStatement tree = (ForEachStatement) firstMethodFirstStatement("class T { void m() { for (Object o : objects) ; } }");
assertThat(tree.is(Tree.Kind.FOR_EACH_STATEMENT)).isTrue();
assertThat(tree.forKeyword().text()).isEqualTo("for");
assertThat(tree.openParenToken().text()).isEqualTo("(");
assertThat(tree.variable()).isNotNull();
assertThat(tree.colonToken().text()).isEqualTo(":");
assertThat(tree.expression()).isNotNull();
assertThat(tree.closeParenToken().text()).isEqualTo(")");
assertThat(tree.statement()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 7);
}
use of org.sonar.plugins.java.api.tree.ForEachStatement in project sonar-java by SonarSource.
the class MissingCurlyBracesCheck method visitNode.
@Override
public void visitNode(Tree tree) {
switch(tree.kind()) {
case WHILE_STATEMENT:
WhileStatementTree whileStatementTree = (WhileStatementTree) tree;
checkStatement(whileStatementTree.whileKeyword(), whileStatementTree.statement());
break;
case DO_STATEMENT:
DoWhileStatementTree doWhileStatementTree = (DoWhileStatementTree) tree;
checkStatement(doWhileStatementTree.doKeyword(), doWhileStatementTree.statement());
break;
case FOR_STATEMENT:
ForStatementTree forStatementTree = (ForStatementTree) tree;
checkStatement(forStatementTree.forKeyword(), forStatementTree.statement());
break;
case FOR_EACH_STATEMENT:
ForEachStatement forEachStatement = (ForEachStatement) tree;
checkStatement(forEachStatement.forKeyword(), forEachStatement.statement());
break;
case IF_STATEMENT:
checkIfStatement((IfStatementTree) tree);
break;
default:
break;
}
}
use of org.sonar.plugins.java.api.tree.ForEachStatement in project sonar-java by SonarSource.
the class LoopsOnSameSetCheck method visitNode.
@Override
public void visitNode(Tree tree) {
Map<Symbol, Integer> forEachSymbols = new HashMap<>();
Tree previousForeachIterable = null;
for (Tree item : ((BlockTree) tree).body()) {
if (item.is(Tree.Kind.FOR_EACH_STATEMENT)) {
ForEachStatement forEachStatement = (ForEachStatement) item;
checkForEach(forEachSymbols, previousForeachIterable, forEachStatement);
previousForeachIterable = forEachStatement.expression();
} else {
previousForeachIterable = null;
item.accept(new InvalidatorVisitor(forEachSymbols));
}
}
}
Aggregations