use of org.sonar.plugins.java.api.tree.WildcardTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method type_arguments.
/**
* 4.5.1. Type Arguments and Wildcards
*/
@Test
public void type_arguments() {
VariableTree variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<? extends A, ? super B, ?, C> var; } }");
assertThatChildrenIteratorHasSize(variableTree, 4);
ParameterizedTypeTree parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
TypeArguments typeArguments = parameterizedTypeTree.typeArguments();
assertThat(typeArguments).hasSize(4);
assertThat(typeArguments.separators()).hasSize(3);
assertThatChildrenIteratorHasSize(typeArguments, 9);
WildcardTree wildcard = (WildcardTree) typeArguments.get(0);
assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.queryToken()).isNotNull();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.extendsOrSuperToken()).isNotNull();
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
assertThatChildrenIteratorHasSize(wildcard, 3);
wildcard = (WildcardTree) typeArguments.get(1);
assertThat(wildcard.is(Tree.Kind.SUPER_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.queryToken()).isNotNull();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.extendsOrSuperToken()).isNotNull();
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("super");
assertThatChildrenIteratorHasSize(wildcard, 3);
wildcard = (WildcardTree) typeArguments.get(2);
assertThat(wildcard.is(Tree.Kind.UNBOUNDED_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isNull();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.queryToken()).isNotNull();
assertThat(wildcard.extendsOrSuperToken()).isNull();
assertThatChildrenIteratorHasSize(wildcard, 1);
assertThat(typeArguments.get(3)).isInstanceOf(IdentifierTree.class);
variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<@Foo ? extends A> var; } }");
parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
typeArguments = parameterizedTypeTree.typeArguments();
assertThatChildrenIteratorHasSize(typeArguments, 3);
wildcard = (WildcardTree) typeArguments.get(0);
assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.annotations()).hasSize(1);
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
assertThatChildrenIteratorHasSize(wildcard, 4);
variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<? extends @Foo @Bar A> var; } }");
parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
typeArguments = parameterizedTypeTree.typeArguments();
assertThatChildrenIteratorHasSize(typeArguments, 3);
wildcard = (WildcardTree) typeArguments.get(0);
assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.annotations()).isEmpty();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
assertThatChildrenIteratorHasSize(wildcard, 3);
}
Aggregations