use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method type.
@Test
public void type() {
ArrayTypeTree tree = (ArrayTypeTree) ((MethodTree) firstTypeMember("class T { int[] m() { return null; } }")).returnType();
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method enum_field.
@Test
public void enum_field() {
List<Tree> declarations = firstType("enum T { ; public int f1 = 42, f2[]; }").members();
assertThat(declarations).hasSize(3);
assertThat(declarations.get(0).is(Tree.Kind.EMPTY_STATEMENT)).isTrue();
VariableTree tree = (VariableTree) declarations.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("f1");
assertThat(tree.initializer()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 6);
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.PUBLIC);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("f2");
assertThat(tree.initializer()).isNull();
assertThatChildrenIteratorHasSize(tree, 4);
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method class_field.
@Test
public void class_field() {
List<Tree> declarations = firstType("class T { public int f1 = 42, f2[]; }").members();
assertThat(declarations).hasSize(2);
VariableTree tree = (VariableTree) declarations.get(0);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("f1");
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()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("f2");
assertThat(tree.initializer()).isNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 4);
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method array_field.
@Test
public void array_field() {
VariableTree field;
ArrayTypeTree arrayTypeTree, childArrayTypeTree;
field = (VariableTree) firstTypeMember("class T { int[] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int[][] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
assertThat(arrayTypeTree.type()).isInstanceOf(ArrayTypeTree.class);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int @Foo [] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
field = (VariableTree) firstTypeMember("class T { int @Foo @bar [] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 2);
assertThatChildrenIteratorHasSize(arrayTypeTree, 5);
field = (VariableTree) firstTypeMember("class T { int[] @Foo [] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int[] a[]; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int[] a @Foo []; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThat(arrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method annotation_constant.
@Test
public void annotation_constant() {
List<Tree> members = firstType("@interface T { int c1 = 1, c2[] = { 2 }; }").members();
assertThat(members).hasSize(2);
VariableTree tree = (VariableTree) members.get(0);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("c1");
assertThat(tree.initializer()).isNotNull();
// 1+5, as empty modifiers are always included
assertThatChildrenIteratorHasSize(tree, 6);
tree = (VariableTree) members.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("c2");
assertThat(tree.initializer()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 6);
}
Aggregations