Search in sources :

Example 6 with BlockTree

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);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 7 with BlockTree

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);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 8 with BlockTree

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);
}
Also used : SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) EmptyStatementTree(org.sonar.plugins.java.api.tree.EmptyStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 9 with BlockTree

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());
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 10 with BlockTree

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);
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

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