use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method blocks.
/*
* 14. Blocks and Statements
*/
/**
* 14.2. Blocks
*/
@Test
public void blocks() {
BlockTree tree = ((MethodTree) firstTypeMember("class T { void m() { ; ; } }")).block();
assertThat(tree.is(Tree.Kind.BLOCK)).isTrue();
assertThat(tree.openBraceToken().text()).isEqualTo("{");
assertThat(tree.body()).hasSize(2);
assertThat(tree.closeBraceToken().text()).isEqualTo("}");
assertThatChildrenIteratorHasSize(tree, 4);
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method local_class_declaration.
/**
* 14.3. Local Class Declarations
*/
@Test
public void local_class_declaration() {
BlockTree block = ((MethodTree) firstTypeMember("class T { void m() { abstract class Local { } } }")).block();
ClassTree tree = (ClassTree) block.body().get(0);
assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
assertThat(tree.simpleName().identifierToken().text()).isEqualTo("Local");
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.ABSTRACT);
assertThat(tree).isNotNull();
assertThatChildrenIteratorHasSize(tree, 7);
block = ((MethodTree) firstTypeMember("class T { void m() { static enum Local { ; } } }")).block();
tree = (ClassTree) block.body().get(0);
assertThat(tree.is(Tree.Kind.ENUM)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.STATIC);
assertThat(tree).isNotNull();
assertThatChildrenIteratorHasSize(tree, 8);
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method local_variable_declaration.
/**
* 14.4. Local Variable Declaration Statements
*/
@Test
public void local_variable_declaration() {
BlockTree block = ((MethodTree) firstTypeMember("class T { void m() { int a = 42, b[]; final @Nullable int c = 42; } }")).block();
List<StatementTree> declarations = block.body();
assertThat(declarations).hasSize(3);
VariableTree tree = (VariableTree) declarations.get(0);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).isEmpty();
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("a");
assertThat(tree.initializer()).isNotNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(",");
assertThatChildrenIteratorHasSize(tree, 6);
tree = (VariableTree) declarations.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).isEmpty();
assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("b");
assertThat(tree.initializer()).isNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 4);
// TODO Test annotation
tree = (VariableTree) declarations.get(2);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.FINAL);
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("c");
assertThat(tree.initializer()).isNotNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 6);
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class AssertionInThreadRunCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
BlockTree block = methodTree.block();
if (block != null && isRunMethod(methodTree)) {
block.accept(new AssertionsVisitor());
}
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class LeftCurlyBraceBaseTreeVisitor method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
BlockTree blockTree = tree.block();
if (blockTree != null) {
checkTokens(getLastTokenFromSignature(tree), blockTree.openBraceToken());
}
super.visitMethod(tree);
}
Aggregations