use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class IndentationCheck method checkCaseGroup.
private void checkCaseGroup(CaseGroupTree tree) {
List<CaseLabelTree> labels = tree.labels();
if (labels.size() >= 2) {
CaseLabelTree previousCaseLabelTree = labels.get(labels.size() - 2);
excludeIssueAtLine = previousCaseLabelTree.lastToken().line();
}
List<StatementTree> body = tree.body();
List<StatementTree> newBody = body;
int bodySize = body.size();
if (bodySize > 0 && body.get(0).is(Kind.BLOCK)) {
expectedLevel -= indentationLevel;
checkIndentation(body.get(0), Iterables.getLast(labels).colonToken().column() + 2);
newBody = body.subList(1, bodySize);
}
checkIndentation(newBody);
if (bodySize > 0 && body.get(0).is(Kind.BLOCK)) {
expectedLevel += indentationLevel;
}
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class MultilineBlocksCurlyBracesCheck method getIfStatementLastBlock.
private static StatementTree getIfStatementLastBlock(StatementTree statementTree) {
StatementTree block = statementTree;
while (block.is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree ifStatementTree = (IfStatementTree) block;
StatementTree elseStatement = ifStatementTree.elseStatement();
block = elseStatement == null ? ifStatementTree.thenStatement() : elseStatement;
}
return block;
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class MultilineBlocksCurlyBracesCheck method check.
private void check(StatementTree current, StatementTree previous) {
StatementTree block = null;
boolean condition = false;
if (previous.is(Tree.Kind.FOR_EACH_STATEMENT)) {
block = ((ForEachStatement) previous).statement();
} else if (previous.is(Tree.Kind.FOR_STATEMENT)) {
block = ((ForStatementTree) previous).statement();
} else if (previous.is(Tree.Kind.WHILE_STATEMENT)) {
block = ((WhileStatementTree) previous).statement();
} else if (previous.is(Tree.Kind.IF_STATEMENT)) {
block = getIfStatementLastBlock(previous);
condition = true;
}
if (block != null && !block.is(Tree.Kind.BLOCK)) {
SyntaxToken previousToken = block.firstToken();
int previousColumn = previousToken.column();
int previousLine = previousToken.line();
SyntaxToken currentToken = current.firstToken();
int currentColumn = currentToken.column();
int currentLine = currentToken.line();
if ((previousColumn == currentColumn && previousLine + 1 == currentLine) || (previousLine == previous.firstToken().line() && previous.firstToken().column() < currentColumn)) {
int lines = 1 + currentLine - previousLine;
context.reportIssue(this, current, getMessage(condition, lines));
}
}
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class CFGTest method build_partial_cfg.
private void build_partial_cfg(String breakOrContinue) {
String methodCode = "void meth(){ try {fun(); } catch ( Exception e) {e.printStackTrace(); " + breakOrContinue + "; } }";
CompilationUnitTree cut = (CompilationUnitTree) parser.parse("class A {" + methodCode + "}");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
List<StatementTree> body = methodTree.block().body();
CFG cfg = CFG.buildCFG(body, true);
cfg.setMethodSymbol(methodTree.symbol());
assertThat(cfg.blocks()).hasSize(5);
assertThat(cfg.methodSymbol()).isSameAs(methodTree.symbol());
try {
CFG.buildCFG(body, false);
fail("IllegalStateException should have been thrown");
} catch (IllegalStateException iae) {
assertThat(iae).hasMessage("'" + breakOrContinue + "' statement not in loop or switch statement");
}
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class SyntaxTreeDebug method blockString.
private static String blockString(BlockTree syntaxNode) {
StringBuilder buffer = new StringBuilder();
boolean first = true;
for (StatementTree tree : syntaxNode.body()) {
if (first) {
first = false;
} else {
buffer.append(", ");
}
buffer.append(tree);
}
return buffer.toString();
}
Aggregations