Search in sources :

Example 1 with ForEachStatement

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);
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 2 with ForEachStatement

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);
}
Also used : ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement) Test(org.junit.Test)

Example 3 with ForEachStatement

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);
}
Also used : ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement) Test(org.junit.Test)

Example 4 with ForEachStatement

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;
    }
}
Also used : DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 5 with ForEachStatement

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));
        }
    }
}
Also used : HashMap(java.util.HashMap) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Aggregations

ForEachStatement (org.sonar.plugins.java.api.tree.ForEachStatement)6 Test (org.junit.Test)2 Symbol (org.sonar.plugins.java.api.semantic.Symbol)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)2 HashMap (java.util.HashMap)1 JavaTree (org.sonar.java.model.JavaTree)1 DoWhileStatementTree (org.sonar.plugins.java.api.tree.DoWhileStatementTree)1 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)1