Search in sources :

Example 26 with StatementTree

use of org.sonar.plugins.java.api.tree.StatementTree 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 27 with StatementTree

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

the class RedundantThrowsDeclarationCheck method onlyReturnLiteralsOrThrowException.

private static boolean onlyReturnLiteralsOrThrowException(MethodTree methodTree) {
    BlockTree block = methodTree.block();
    if (block == null) {
        return false;
    }
    List<StatementTree> body = block.body();
    if (body.size() != 1) {
        return false;
    }
    StatementTree singleStatement = body.get(0);
    return singleStatement.is(Tree.Kind.THROW_STATEMENT) || returnStatementWithLiteral(singleStatement);
}
Also used : ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 28 with StatementTree

use of org.sonar.plugins.java.api.tree.StatementTree 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 29 with StatementTree

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

the class VariableReadExtractorTest method should_extract_fields_read.

@Test
public void should_extract_fields_read() {
    MethodTree methodTree = buildMethodTree("void foo(boolean a) { bar(p -> { System.out.println(a + field1); foo(this.field2); }); }");
    StatementTree statementTree = methodTree.block().body().get(0);
    VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), true);
    statementTree.accept(extractor);
    // local variable "a" and fields "field1" and "field2"
    assertThat(extractor.usedVariables()).hasSize(3);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 30 with StatementTree

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

the class VariableReadExtractorTest method should_not_extract_local_vars_written.

@Test
public void should_not_extract_local_vars_written() throws Exception {
    MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { new A().field1 = 0; a = false;} };  }");
    StatementTree statementTree = methodTree.block().body().get(0);
    VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false);
    statementTree.accept(extractor);
    assertThat(extractor.usedVariables()).isEmpty();
    methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { a = !a;} };  }");
    statementTree = methodTree.block().body().get(0);
    extractor = new VariableReadExtractor(methodTree.symbol(), false);
    statementTree.accept(extractor);
    assertThat(extractor.usedVariables()).hasSize(1);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Aggregations

StatementTree (org.sonar.plugins.java.api.tree.StatementTree)53 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)27 Test (org.junit.Test)26 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)26 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)21 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)17 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)15 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)13 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)9 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)8 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)7 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)6 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)6 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)6 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)6 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)5 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)5