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