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