Search in sources :

Example 21 with BlockTree

use of org.sonar.plugins.java.api.tree.BlockTree 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);
}
Also used : ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 22 with BlockTree

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

the class RedundantThrowsDeclarationCheck method thrownExceptionsFromBody.

@Nullable
private static Set<Type> thrownExceptionsFromBody(MethodTree methodTree) {
    BlockTree block = methodTree.block();
    if (block != null) {
        MethodInvocationVisitor visitor = new MethodInvocationVisitor();
        block.accept(visitor);
        return visitor.thrownExceptions();
    }
    return null;
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Nullable(javax.annotation.Nullable)

Example 23 with BlockTree

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

the class TooManyStatementsPerLineCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    BlockTree block;
    if (tree.is(Tree.Kind.STATIC_INITIALIZER, Kind.INITIALIZER)) {
        block = (BlockTree) tree;
    } else {
        block = ((MethodTree) tree).block();
    }
    if (block != null) {
        StatementVisitor visitor = new StatementVisitor();
        block.accept(visitor);
        for (Entry<Integer> entry : visitor.statementsPerLine.entrySet()) {
            int count = entry.getCount();
            if (count > 1) {
                addIssue(entry.getElement(), "At most one statement is allowed per line, but " + count + " statements were found on this line.");
            }
        }
    }
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 24 with BlockTree

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

the class ExplodedGraphWalker method visitMethod.

public MethodBehavior visitMethod(MethodTree tree, @Nullable MethodBehavior methodBehavior) {
    Preconditions.checkArgument(methodBehavior == null || !methodBehavior.isComplete() || !methodBehavior.isVisited(), "Trying to execute an already visited methodBehavior");
    this.methodBehavior = methodBehavior;
    BlockTree body = tree.block();
    if (body != null) {
        execute(tree);
    }
    return this.methodBehavior;
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 25 with BlockTree

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

the class LambdaSingleExpressionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree;
    Tree lambdaBody = lambdaExpressionTree.body();
    if (isBlockWithOneStatement(lambdaBody)) {
        String message = "Remove useless curly braces around statement";
        if (singleStatementIsReturn(lambdaExpressionTree)) {
            message += " and then remove useless return keyword";
        }
        reportIssue(((BlockTree) lambdaBody).openBraceToken(), message + context.getJavaVersion().java8CompatibilityMessage());
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree)

Aggregations

BlockTree (org.sonar.plugins.java.api.tree.BlockTree)34 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)14 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)13 Test (org.junit.Test)10 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)9 Tree (org.sonar.plugins.java.api.tree.Tree)5 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 JavaTree (org.sonar.java.model.JavaTree)3 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)3 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)2 ForEachStatement (org.sonar.plugins.java.api.tree.ForEachStatement)2 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2