Search in sources :

Example 1 with ExpressionStatementTree

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

the class IgnoredReturnValueCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree expr = ((ExpressionStatementTree) tree).expression();
    if (expr.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expr;
        if (isExcluded(mit)) {
            return;
        }
        Symbol methodSymbol = mit.symbol();
        if (!isVoidOrUnknown(mit.symbolType()) && isCheckedType(methodSymbol.owner().type()) && methodSymbol.isPublic() && !isConstructor(methodSymbol)) {
            IdentifierTree methodName = ExpressionUtils.methodName(mit);
            reportIssue(methodName, "The return value of \"" + methodName.name() + "\" must be used.");
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 2 with ExpressionStatementTree

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

the class SymbolTableTest method symbolNotFound.

@Test
public void symbolNotFound() throws Exception {
    Result result = Result.createFor("SymbolsNotFound");
    MethodTree methodDeclaration = (MethodTree) result.symbol("method").declaration();
    ExpressionStatementTree expression = (ExpressionStatementTree) methodDeclaration.block().body().get(0);
    MethodInvocationTree methodInvocation = (MethodInvocationTree) expression.expression();
    Symbol symbolNotFound = methodInvocation.symbol();
    assertThat(symbolNotFound).isNotNull();
    assertThat(symbolNotFound.name()).isNull();
    assertThat(symbolNotFound.owner()).isSameAs(Symbols.unknownSymbol);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Example 3 with ExpressionStatementTree

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

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

the class JavaTreeModelTest method unary_operators.

/**
 * 15.15. Unary Operators
 */
@Test
public void unary_operators() {
    UnaryExpressionTree tree;
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { ++i; } }"))).expression();
    assertThat(tree.is(Tree.Kind.PREFIX_INCREMENT)).isTrue();
    assertThat(tree.operatorToken().text()).isEqualTo("++");
    assertThat(tree.expression()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { --i; } }"))).expression();
    assertThat(tree.is(Tree.Kind.PREFIX_DECREMENT)).isTrue();
    assertThat(tree.operatorToken().text()).isEqualTo("--");
    assertThat(tree.expression()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Example 5 with ExpressionStatementTree

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

the class MethodInvocationTreeImplTest method symbol_should_be_set.

@Test
public void symbol_should_be_set() {
    CompilationUnitTree cut = createTree("class A { void foo(){} void bar(){foo();} }");
    ClassTree classTree = (ClassTree) cut.types().get(0);
    Symbol.MethodSymbol declaration = ((MethodTree) classTree.members().get(0)).symbol();
    StatementTree statementTree = ((MethodTree) classTree.members().get(1)).block().body().get(0);
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).expression();
    assertThat(mit.symbol()).isSameAs(declaration);
    assertThat(mit.arguments()).isNotNull();
    assertThat(mit.arguments().openParenToken()).isNotNull();
    assertThat(mit.arguments().closeParenToken()).isNotNull();
}
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) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

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