use of org.sonar.plugins.java.api.tree.IfStatementTree 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.IfStatementTree 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.IfStatementTree in project sonar-java by SonarSource.
the class SwitchInsteadOfIfSequenceCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.parent().is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree parentIf = (IfStatementTree) tree.parent();
if (tree.equals(parentIf.elseStatement())) {
// don't double count nested ifs
return;
}
}
IfStatementTree ifStatementTree = (IfStatementTree) tree;
int level = 1;
while (ifStatementTree.elseKeyword() != null && ifStatementTree.elseStatement().is(Tree.Kind.IF_STATEMENT)) {
level++;
if (!sameEqualCondition(ifStatementTree.condition(), ((IfStatementTree) ifStatementTree.elseStatement()).condition())) {
level = 0;
break;
}
ifStatementTree = (IfStatementTree) ifStatementTree.elseStatement();
}
if (level > 2) {
reportIssue(((IfStatementTree) tree).condition(), "Convert this \"if/else if\" structure into a \"switch\"." + context.getJavaVersion().java7CompatibilityMessage());
}
}
use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method if_statement.
/**
* 14.9. The if Statement
*/
@Test
public void if_statement() {
IfStatementTree tree = (IfStatementTree) firstMethodFirstStatement("class T { void m() { if (true) { } } }");
assertThat(tree.is(Tree.Kind.IF_STATEMENT)).isTrue();
assertThat(tree.ifKeyword().text()).isEqualTo("if");
assertThat(tree.openParenToken().text()).isEqualTo("(");
assertThat(tree.condition()).isNotNull();
assertThat(tree.closeParenToken().text()).isEqualTo(")");
assertThat(tree.thenStatement()).isNotNull();
assertThat(tree.elseKeyword()).isNull();
assertThat(tree.elseStatement()).isNull();
assertThatChildrenIteratorHasSize(tree, 5);
tree = (IfStatementTree) firstMethodFirstStatement("class T { void m() { if (true) { } else { } } }");
assertThat(tree.is(Tree.Kind.IF_STATEMENT)).isTrue();
assertThat(tree.ifKeyword().text()).isEqualTo("if");
assertThat(tree.openParenToken().text()).isEqualTo("(");
assertThat(tree.condition()).isNotNull();
assertThat(tree.closeParenToken().text()).isEqualTo(")");
assertThat(tree.thenStatement()).isNotNull();
assertThat(tree.elseKeyword().text()).isEqualTo("else");
assertThat(tree.elseStatement()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 7);
}
use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method known_limitation.
@Test
public void known_limitation() throws Exception {
String code = newCode("int foo(boolean test) {", " int a;", " if (test) {", " a = 0;", " } else {", // Should have returned both thenAssignment and elseAssignment. CFG?
" a = 1;", " }", " return a;", "}");
List<StatementTree> statements = methodBody(code);
StatementTree elseAssignment = ((BlockTree) ((IfStatementTree) statements.get(1)).elseStatement()).body().get(0);
ExpressionTree expression = assignementExpressionFromStatement(elseAssignment);
assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, expression);
}
Aggregations