use of org.sonar.plugins.java.api.tree.ExpressionStatementTree 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.ExpressionStatementTree 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.ExpressionStatementTree in project sonar-java by SonarSource.
the class NonNullSetToNullCheck method callsThisConstructor.
private static boolean callsThisConstructor(MethodTree constructor) {
List<StatementTree> body = constructor.block().body();
if (body.isEmpty()) {
return false;
}
StatementTree firstStatement = body.get(0);
if (!firstStatement.is(Tree.Kind.EXPRESSION_STATEMENT)) {
return false;
}
ExpressionTree expression = ((ExpressionStatementTree) firstStatement).expression();
if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
return false;
}
ExpressionTree methodSelect = ((MethodInvocationTree) expression).methodSelect();
return methodSelect.is(Tree.Kind.IDENTIFIER) && "this".equals(((IdentifierTree) methodSelect).name());
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class MethodOnlyCallsSuperCheck method isUselessSuperCall.
private static boolean isUselessSuperCall(MethodTree methodTree) {
ExpressionTree callToSuper = null;
StatementTree statementTree = methodTree.block().body().get(0);
if (returnsVoid(methodTree) && statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
callToSuper = ((ExpressionStatementTree) statementTree).expression();
} else if (statementTree.is(Tree.Kind.RETURN_STATEMENT)) {
callToSuper = ((ReturnStatementTree) statementTree).expression();
}
return callToSuper != null && isCallToSuper(methodTree, callToSuper) && sameVisibility(methodTree.symbol(), ((MethodInvocationTree) callToSuper).symbol());
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method returnTypeOf.
private Type returnTypeOf(String method, String invocation) {
CompilationUnitTree compilationUnit = treeOf("class A { " + method + " void test() { " + invocation + " } }");
MethodTree testMethod = (MethodTree) ((ClassTreeImpl) compilationUnit.types().get(0)).members().get(1);
ExpressionTree methodInvocation = ((ExpressionStatementTree) testMethod.block().body().get(0)).expression();
return methodInvocation.symbolType();
}
Aggregations