Search in sources :

Example 31 with StatementTree

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

the class ReassignmentFinderTest method ignore_assignation_after_starting_point.

@Test
public void ignore_assignation_after_starting_point() throws Exception {
    String code = newCode("int foo() {", "  int b = 0;", "  doSomething(b);", "  b = 1;", "}");
    List<StatementTree> statements = methodBody(code);
    Tree expectedVariableDeclaration = initializerFromVariableDeclarationStatement(statements.get(0));
    MethodInvocationTree startingPoint = (MethodInvocationTree) ((ExpressionStatementTree) statements.get(1)).expression();
    Symbol searchedVariable = ((IdentifierTree) startingPoint.arguments().get(0)).symbol();
    assertThatLastReassignmentsOfVariableIsEqualTo(searchedVariable, startingPoint, expectedVariableDeclaration);
}
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) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 32 with StatementTree

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

the class ReassignmentFinderTest method ignore_assignation_after_starting_point_same_line.

@Test
public void ignore_assignation_after_starting_point_same_line() throws Exception {
    String code = newCode("int foo() {", "  int b = 0;", "  doSomething(b); b = 1;", "}");
    List<StatementTree> statements = methodBody(code);
    Tree expectedVariableDeclaration = initializerFromVariableDeclarationStatement(statements.get(0));
    MethodInvocationTree startingPoint = (MethodInvocationTree) ((ExpressionStatementTree) statements.get(1)).expression();
    Symbol searchedVariable = ((IdentifierTree) startingPoint.arguments().get(0)).symbol();
    assertThatLastReassignmentsOfVariableIsEqualTo(searchedVariable, startingPoint, expectedVariableDeclaration);
}
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) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 33 with StatementTree

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

Example 34 with StatementTree

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

the class ReassignmentFinderTest method assignement.

@Test
public void assignement() throws Exception {
    String code = newCode("int foo() {", "  int a;", "  a = 0;", "  return a;", "}");
    List<StatementTree> statements = methodBody(code);
    ExpressionTree aAssignmentExpression = assignementExpressionFromStatement(statements.get(1));
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, aAssignmentExpression);
}
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) Test(org.junit.Test)

Example 35 with StatementTree

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

the class ReassignmentFinderTest method array_declaration.

@Test
public void array_declaration() throws Exception {
    String code = newCode("int foo() {", "  int a[] = new int[42];", "  a[0] = 42;", "  return a;", "}");
    List<StatementTree> statements = methodBody(code);
    ExpressionTree arrayAssignmentExpression = initializerFromVariableDeclarationStatement(statements.get(0));
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, arrayAssignmentExpression);
}
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) 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