Search in sources :

Example 11 with ExpressionStatementTree

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);
}
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 12 with ExpressionStatementTree

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);
}
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 13 with ExpressionStatementTree

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());
}
Also used : 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) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 14 with ExpressionStatementTree

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());
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree)

Example 15 with ExpressionStatementTree

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();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree)

Aggregations

ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)21 Test (org.junit.Test)11 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)11 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)11 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)10 Symbol (org.sonar.plugins.java.api.semantic.Symbol)7 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)7 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)6 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)6 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)5 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)5 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)4 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)3 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 CheckForNull (javax.annotation.CheckForNull)2 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)2