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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations