Search in sources :

Example 21 with StatementTree

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

the class ConstantMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    BlockTree body = methodTree.block();
    if (!methodTree.modifiers().annotations().isEmpty() || ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.DEFAULT)) {
        return;
    }
    if (Boolean.FALSE.equals(methodTree.isOverriding()) && body != null && body.body().size() == 1) {
        StatementTree uniqueStatement = body.body().get(0);
        if (uniqueStatement.is(Kind.RETURN_STATEMENT)) {
            ExpressionTree returnedExpression = ((ReturnStatementTree) uniqueStatement).expression();
            if (isConstant(returnedExpression)) {
                reportIssue(returnedExpression, "Remove this method and declare a constant for this value.");
            }
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree)

Example 22 with StatementTree

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

the class SyntaxTreeNameFinderTest method testMemberSelectOnMethodInvocation.

@Test
public void testMemberSelectOnMethodInvocation() {
    MethodTree tree = buildSyntaxTree("public void test() {int i = checkForNullMethod().length;}");
    BlockTree block = tree.block();
    StatementTree statementTree = block.body().get(0);
    assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("checkForNullMethod");
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 23 with StatementTree

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

the class SyntaxTreeNameFinderTest method testSwitch.

@Test
public void testSwitch() {
    MethodTree tree = buildSyntaxTree("public void test() {int i; switch (i) { case 0: break;}}");
    BlockTree block = tree.block();
    StatementTree statementTree = block.body().get(1);
    assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("i");
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 24 with StatementTree

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

the class ReturnOfBooleanExpressionsCheck method getStatementTree.

private static StatementTree getStatementTree(IfStatementTree ifStatementTree) {
    StatementTree elseStatementOrNextStatement = ifStatementTree.elseStatement();
    if (elseStatementOrNextStatement == null) {
        JavaTree parent = (JavaTree) ifStatementTree.parent();
        List<Tree> children = parent.getChildren();
        int indexOfIf = children.indexOf(ifStatementTree);
        if (indexOfIf < children.size() - 1) {
            // Defensive, this condition should always be true as if necessarily followed by a statement or a token.
            Tree next = children.get(indexOfIf + 1);
            if (!next.is(Kind.TOKEN)) {
                elseStatementOrNextStatement = (StatementTree) next;
            }
        }
    }
    return elseStatementOrNextStatement;
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) JavaTree(org.sonar.java.model.JavaTree)

Example 25 with StatementTree

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

the class SwitchWithLabelsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    CaseGroupTree cgt = (CaseGroupTree) tree;
    for (StatementTree statementTree : cgt.body()) {
        if (statementTree.is(LABELED_STATEMENT)) {
            IdentifierTree label = ((LabeledStatementTree) statementTree).label();
            reportIssue(label, "Remove this misleading \"" + label.name() + "\" label.");
        }
    }
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree)

Aggregations

StatementTree (org.sonar.plugins.java.api.tree.StatementTree)53 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)27 Test (org.junit.Test)26 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)26 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)21 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)17 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)15 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)13 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)9 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)8 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)7 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)6 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)6 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)6 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)6 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)5 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)5