use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method basic_type.
@Test
public void basic_type() {
PrimitiveTypeTree tree = (PrimitiveTypeTree) ((MethodTree) firstTypeMember("class T { int m() { return null; } }")).returnType();
assertThat(tree.keyword().text()).isEqualTo("int");
assertThatChildrenIteratorHasSize(tree, 1);
tree = (PrimitiveTypeTree) ((MethodTree) firstTypeMember("class T { void m() { return null; } }")).returnType();
assertThat(tree.keyword().text()).isEqualTo("void");
assertThatChildrenIteratorHasSize(tree, 1);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method class_constructor.
@Test
public void class_constructor() {
MethodTree tree = (MethodTree) firstTypeMember("class T { T(int p1, int... p2) throws Exception1, Exception2 {} }");
assertThat(tree.is(Tree.Kind.CONSTRUCTOR)).isTrue();
assertThat(tree.returnType()).isNull();
assertThat(tree.simpleName().name()).isEqualTo("T");
assertThat(tree.parameters()).hasSize(2);
assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.parameters().get(1).type()).isInstanceOf(ArrayTypeTree.class);
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 10);
}
use of org.sonar.plugins.java.api.tree.MethodTree 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.MethodTree 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.MethodTree 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();
}
Aggregations