use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class RightCurlyBraceToNextBlockAbstractVisitor method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree ifStatementTree = (IfStatementTree) tree;
StatementTree thenStatement = ifStatementTree.thenStatement();
if (ifStatementTree.elseKeyword() != null && thenStatement.is(Tree.Kind.BLOCK)) {
checkTokenPosition(ifStatementTree.elseKeyword(), (BlockTree) thenStatement);
}
} else {
TryStatementTree tryStatementTree = (TryStatementTree) tree;
BlockTree block = tryStatementTree.block();
for (CatchTree catchTree : tryStatementTree.catches()) {
checkTokenPosition(catchTree.catchKeyword(), block);
block = catchTree.block();
}
SyntaxToken finallyKeyword = tryStatementTree.finallyKeyword();
if (finallyKeyword != null) {
checkTokenPosition(finallyKeyword, block);
}
}
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class RedundantThrowsDeclarationCheck method onlyReturnLiteralsOrThrowException.
private static boolean onlyReturnLiteralsOrThrowException(MethodTree methodTree) {
BlockTree block = methodTree.block();
if (block == null) {
return false;
}
List<StatementTree> body = block.body();
if (body.size() != 1) {
return false;
}
StatementTree singleStatement = body.get(0);
return singleStatement.is(Tree.Kind.THROW_STATEMENT) || returnStatementWithLiteral(singleStatement);
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class ReturnOfBooleanExpressionsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
IfStatementTree ifStatementTree = (IfStatementTree) tree;
StatementTree elseStatementOrNextStatement = getStatementTree(ifStatementTree);
if (hasOneReturnBoolean(elseStatementOrNextStatement) && hasOneReturnBoolean(ifStatementTree.thenStatement())) {
reportIssue(ifStatementTree.ifKeyword(), "Replace this if-then-else statement by a single return statement.");
}
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_extract_fields_read.
@Test
public void should_extract_fields_read() {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { bar(p -> { System.out.println(a + field1); foo(this.field2); }); }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), true);
statementTree.accept(extractor);
// local variable "a" and fields "field1" and "field2"
assertThat(extractor.usedVariables()).hasSize(3);
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_not_extract_local_vars_written.
@Test
public void should_not_extract_local_vars_written() throws Exception {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { new A().field1 = 0; a = false;} }; }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false);
statementTree.accept(extractor);
assertThat(extractor.usedVariables()).isEmpty();
methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { a = !a;} }; }");
statementTree = methodTree.block().body().get(0);
extractor = new VariableReadExtractor(methodTree.symbol(), false);
statementTree.accept(extractor);
assertThat(extractor.usedVariables()).hasSize(1);
}
Aggregations