use of org.sonar.plugins.java.api.tree.StatementTree 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.StatementTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_not_extract_fields_written.
@Test
public void should_not_extract_fields_written() throws Exception {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { new A().field1 = 0; a = false;} }; }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), true);
statementTree.accept(extractor);
// local variable "a" and field "field"
assertThat(extractor.usedVariables()).isEmpty();
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_not_extract_variable_declared.
@Test
public void should_not_extract_variable_declared() throws Exception {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { boolean b = a; }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), true);
statementTree.accept(extractor);
// only "a" should be detected
assertThat(extractor.usedVariables()).hasSize(1);
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_return_symbol_once.
@Test
public void should_return_symbol_once() {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { foo(a); bar(a); } }; }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false);
statementTree.accept(extractor);
assertThat(extractor.usedVariables()).hasSize(1);
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_extract_local_vars_read.
@Test
public void should_extract_local_vars_read() {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { System.out.println(a);} }; }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false);
statementTree.accept(extractor);
assertThat(extractor.usedVariables()).hasSize(1);
}
Aggregations