Search in sources :

Example 1 with StatementTree

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

the class ImmediatelyReturnedVariableCheck method visitBlock.

@Override
public void visitBlock(BlockTree tree) {
    super.visitBlock(tree);
    List<StatementTree> statements = tree.body();
    int size = statements.size();
    if (size < 2) {
        return;
    }
    StatementTree butLastStatement = statements.get(size - 2);
    if (butLastStatement.is(Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) butLastStatement;
        if (!variableTree.modifiers().annotations().isEmpty()) {
            return;
        }
        StatementTree lastStatement = statements.get(size - 1);
        String lastStatementIdentifier = getReturnOrThrowIdentifier(lastStatement);
        if (lastStatementIdentifier != null) {
            String identifier = variableTree.simpleName().name();
            if (StringUtils.equals(lastStatementIdentifier, identifier)) {
                context.reportIssue(this, variableTree.initializer(), "Immediately " + lastTypeForMessage + " this expression instead of assigning it to the temporary variable \"" + identifier + "\".");
            }
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 2 with StatementTree

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

the class CFG method buildIfStatement.

private void buildIfStatement(IfStatementTree ifStatementTree) {
    Block next = currentBlock;
    // process else-branch
    Block elseBlock = next;
    StatementTree elseStatement = ifStatementTree.elseStatement();
    if (elseStatement != null) {
        // if statement will create the required block.
        if (!elseStatement.is(Tree.Kind.IF_STATEMENT)) {
            currentBlock = createBlock(next);
        }
        build(elseStatement);
        elseBlock = currentBlock;
    }
    // process then-branch
    currentBlock = createBlock(next);
    build(ifStatementTree.thenStatement());
    Block thenBlock = currentBlock;
    // process condition
    currentBlock = createBranch(ifStatementTree, thenBlock, elseBlock);
    buildCondition(ifStatementTree.condition(), thenBlock, elseBlock);
}
Also used : SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree)

Example 3 with StatementTree

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

the class JavaPropertiesHelperTest method firstExpression.

private ExpressionTree firstExpression(String code) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse("class A { " + code + "}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    ClassTree firstType = (ClassTree) compilationUnitTree.types().get(0);
    StatementTree firstStatement = ((MethodTree) firstType.members().get(0)).block().body().get(0);
    return ((ExpressionStatementTree) firstStatement).expression();
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 4 with StatementTree

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

the class ReassignmentFinderTest method outside_method.

@Test
public void outside_method() throws Exception {
    String code = newCode("int b;", "int foo() {", "  return b;", "}");
    ClassTree classTree = classTree(code);
    List<StatementTree> statements = ((MethodTree) classTree.members().get(1)).block().body();
    ExpressionTree variableDeclaration = ((VariableTree) (classTree.members().get(0))).initializer();
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, variableDeclaration);
}
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) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

Example 5 with StatementTree

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

the class ReassignmentFinderTest method parameter.

@Test
public void parameter() throws Exception {
    String code = newCode("int foo(int a) {", "  return a;", "}");
    MethodTree method = methodTree(code);
    List<StatementTree> statements = method.block().body();
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, null);
}
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) 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