Search in sources :

Example 6 with IfStatementTree

use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.

the class RightCurlyBraceToNextBlockAbstractVisitor method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatementTree = (IfStatementTree) tree;
        StatementTree thenStatement = ifStatementTree.thenStatement();
        if (ifStatementTree.elseKeyword() != null && thenStatement.is(Tree.Kind.BLOCK)) {
            checkTokenPosition(ifStatementTree.elseKeyword(), (BlockTree) thenStatement);
        }
    } else {
        TryStatementTree tryStatementTree = (TryStatementTree) tree;
        BlockTree block = tryStatementTree.block();
        for (CatchTree catchTree : tryStatementTree.catches()) {
            checkTokenPosition(catchTree.catchKeyword(), block);
            block = catchTree.block();
        }
        SyntaxToken finallyKeyword = tryStatementTree.finallyKeyword();
        if (finallyKeyword != null) {
            checkTokenPosition(finallyKeyword, block);
        }
    }
}
Also used : TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 7 with IfStatementTree

use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.

the class ReturnOfBooleanExpressionsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    IfStatementTree ifStatementTree = (IfStatementTree) tree;
    StatementTree elseStatementOrNextStatement = getStatementTree(ifStatementTree);
    if (hasOneReturnBoolean(elseStatementOrNextStatement) && hasOneReturnBoolean(ifStatementTree.thenStatement())) {
        reportIssue(ifStatementTree.ifKeyword(), "Replace this if-then-else statement by a single return statement.");
    }
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 8 with IfStatementTree

use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.

the class SwitchInsteadOfIfSequenceCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.parent().is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree parentIf = (IfStatementTree) tree.parent();
        if (tree.equals(parentIf.elseStatement())) {
            // don't double count nested ifs
            return;
        }
    }
    IfStatementTree ifStatementTree = (IfStatementTree) tree;
    int level = 1;
    while (ifStatementTree.elseKeyword() != null && ifStatementTree.elseStatement().is(Tree.Kind.IF_STATEMENT)) {
        level++;
        if (!sameEqualCondition(ifStatementTree.condition(), ((IfStatementTree) ifStatementTree.elseStatement()).condition())) {
            level = 0;
            break;
        }
        ifStatementTree = (IfStatementTree) ifStatementTree.elseStatement();
    }
    if (level > 2) {
        reportIssue(((IfStatementTree) tree).condition(), "Convert this \"if/else if\" structure into a \"switch\"." + context.getJavaVersion().java7CompatibilityMessage());
    }
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 9 with IfStatementTree

use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method if_statement.

/**
 * 14.9. The if Statement
 */
@Test
public void if_statement() {
    IfStatementTree tree = (IfStatementTree) firstMethodFirstStatement("class T { void m() { if (true) { } } }");
    assertThat(tree.is(Tree.Kind.IF_STATEMENT)).isTrue();
    assertThat(tree.ifKeyword().text()).isEqualTo("if");
    assertThat(tree.openParenToken().text()).isEqualTo("(");
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.closeParenToken().text()).isEqualTo(")");
    assertThat(tree.thenStatement()).isNotNull();
    assertThat(tree.elseKeyword()).isNull();
    assertThat(tree.elseStatement()).isNull();
    assertThatChildrenIteratorHasSize(tree, 5);
    tree = (IfStatementTree) firstMethodFirstStatement("class T { void m() { if (true) { } else { } } }");
    assertThat(tree.is(Tree.Kind.IF_STATEMENT)).isTrue();
    assertThat(tree.ifKeyword().text()).isEqualTo("if");
    assertThat(tree.openParenToken().text()).isEqualTo("(");
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.closeParenToken().text()).isEqualTo(")");
    assertThat(tree.thenStatement()).isNotNull();
    assertThat(tree.elseKeyword().text()).isEqualTo("else");
    assertThat(tree.elseStatement()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 7);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) Test(org.junit.Test)

Example 10 with IfStatementTree

use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.

the class ReassignmentFinderTest method known_limitation.

@Test
public void known_limitation() throws Exception {
    String code = newCode("int foo(boolean test) {", "  int a;", "  if (test) {", "    a = 0;", "  } else {", // Should have returned both thenAssignment and elseAssignment. CFG?
    "    a = 1;", "  }", "  return a;", "}");
    List<StatementTree> statements = methodBody(code);
    StatementTree elseAssignment = ((BlockTree) ((IfStatementTree) statements.get(1)).elseStatement()).body().get(0);
    ExpressionTree expression = assignementExpressionFromStatement(elseAssignment);
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, expression);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) Test(org.junit.Test)

Aggregations

IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)19 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)11 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)5 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)5 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)5 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)5 DoWhileStatementTree (org.sonar.plugins.java.api.tree.DoWhileStatementTree)4 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)4 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 Test (org.junit.Test)2 JavaTree (org.sonar.java.model.JavaTree)2 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)2 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)2 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2