use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class IndentationCheck method visitLambdaExpression.
@Override
public void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree) {
// doesn't scan lambda parameters because there's no indentation check on types and identifiers
Tree body = lambdaExpressionTree.body();
if (body.is(Kind.BLOCK)) {
BlockTree block = (BlockTree) body;
excludeIssueAtLine = block.openBraceToken().line();
int previousLevel = expectedLevel;
expectedLevel = block.closeBraceToken().column();
scan(block);
expectedLevel = previousLevel;
} else {
scan(body);
}
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class HiddenFieldCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (isClassTree(tree)) {
ClassTree classTree = (ClassTree) tree;
ImmutableMap.Builder<String, VariableTree> builder = ImmutableMap.builder();
for (Tree member : classTree.members()) {
if (member.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) member;
builder.put(variableTree.simpleName().name(), variableTree);
}
}
fields.push(builder.build());
excludedVariables.push(Lists.<VariableTree>newArrayList());
} else if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
isVariableHidingField(variableTree);
} else if (tree.is(Tree.Kind.STATIC_INITIALIZER)) {
excludeVariablesFromBlock((BlockTree) tree);
} else {
MethodTree methodTree = (MethodTree) tree;
excludedVariables.peek().addAll(methodTree.parameters());
flattenExcludedVariables.addAll(methodTree.parameters());
if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.STATIC)) {
excludeVariablesFromBlock(methodTree.block());
}
}
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class CFG method buildTryStatement.
private void buildTryStatement(TryStatementTree tryStatementTree) {
// FIXME only path with no failure constructed for now, (not taking try with resources into consideration).
currentBlock = createBlock(currentBlock);
BlockTree finallyBlockTree = tryStatementTree.finallyBlock();
if (finallyBlockTree != null) {
currentBlock.isFinallyBlock = true;
Block finallyBlock = currentBlock;
build(finallyBlockTree);
finallyBlock.addExitSuccessor(exitBlock());
exitBlocks.push(currentBlock);
addContinueTarget(currentBlock);
currentBlock.isFinallyBlock = true;
breakTargets.addLast(currentBlock);
}
Block finallyOrEndBlock = currentBlock;
Block beforeFinally = createBlock(currentBlock);
TryStatement tryStatement = new TryStatement();
tryStatement.successorBlock = finallyOrEndBlock;
enclosingTry.push(tryStatement);
enclosedByCatch.push(false);
for (CatchTree catchTree : Lists.reverse(tryStatementTree.catches())) {
currentBlock = createBlock(finallyOrEndBlock);
enclosedByCatch.push(true);
build(catchTree.block());
buildVariable(catchTree.parameter());
currentBlock.isCatchBlock = true;
enclosedByCatch.pop();
tryStatement.addCatch(catchTree.parameter().type().symbolType(), currentBlock);
}
currentBlock = beforeFinally;
build(tryStatementTree.block());
build((List<? extends Tree>) tryStatementTree.resourceList());
enclosingTry.pop();
enclosedByCatch.pop();
currentBlock = createBlock(currentBlock);
currentBlock.elements.add(tryStatementTree);
if (finallyBlockTree != null) {
exitBlocks.pop();
continueTargets.removeLast();
breakTargets.removeLast();
}
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class CFG method build.
public static CFG build(MethodTree tree) {
BlockTree block = tree.block();
Preconditions.checkArgument(block != null, "Cannot build CFG for method with no body.");
return new CFG(block.body(), tree.symbol(), false);
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method class_init_declaration.
@Test
public void class_init_declaration() {
BlockTree tree = (BlockTree) firstTypeMember("class T { { ; ; } }");
assertThat(tree.is(Tree.Kind.INITIALIZER)).isTrue();
assertThat(tree.body()).hasSize(2);
assertThat(tree.openBraceToken().text()).isEqualTo("{");
assertThat(tree.closeBraceToken().text()).isEqualTo("}");
assertThatChildrenIteratorHasSize(tree, 4);
tree = (BlockTree) firstTypeMember("class T { static { ; ; } }");
assertThat(tree.is(Tree.Kind.STATIC_INITIALIZER)).isTrue();
StaticInitializerTree staticInitializerTree = (StaticInitializerTree) tree;
assertThat(staticInitializerTree.body()).hasSize(2);
assertThat(staticInitializerTree.staticKeyword().text()).isEqualTo("static");
assertThat(staticInitializerTree.openBraceToken().text()).isEqualTo("{");
assertThat(staticInitializerTree.closeBraceToken().text()).isEqualTo("}");
assertThatChildrenIteratorHasSize(tree, 5);
}
Aggregations