Search in sources :

Example 1 with TypeArguments

use of org.sonar.plugins.java.api.tree.TypeArguments 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);
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) Test(org.junit.Test)

Example 2 with TypeArguments

use of org.sonar.plugins.java.api.tree.TypeArguments in project sonar-java by SonarSource.

the class JavaTreeModelTest method type_parameters_tokens.

@Test
public void type_parameters_tokens() {
    ParameterizedTypeTree tree = (ParameterizedTypeTree) firstType("class Foo<E> extends List<E> {}").superClass();
    assertThat(tree).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
    TypeArguments typeArguments = tree.typeArguments();
    assertThat(typeArguments).isNotNull();
    assertThat(typeArguments).hasSize(1);
    assertThat(typeArguments.separators()).isEmpty();
    assertThat(typeArguments.openBracketToken()).isNotNull();
    assertThat(typeArguments.closeBracketToken()).isNotNull();
    assertThatChildrenIteratorHasSize(typeArguments, 3);
    tree = (ParameterizedTypeTree) firstType("class Mop<K,V> implements Map<K,V> {}").superInterfaces().get(0);
    assertThat(tree).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
    typeArguments = tree.typeArguments();
    assertThat(typeArguments).isNotNull();
    assertThat(typeArguments).hasSize(2);
    assertThat(typeArguments.separators()).hasSize(1);
    assertThat(typeArguments.openBracketToken()).isNotNull();
    assertThat(typeArguments.closeBracketToken()).isNotNull();
    assertThatChildrenIteratorHasSize(typeArguments, 5);
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) Test(org.junit.Test)

Example 3 with TypeArguments

use of org.sonar.plugins.java.api.tree.TypeArguments in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method resolveIdentifierType.

private JavaType resolveIdentifierType(Resolve.Env newClassEnv, @Nullable ExpressionTree enclosingExpression, TypeTree typeTree, String typeName) {
    if (enclosingExpression != null) {
        resolveAs(enclosingExpression, JavaSymbol.VAR);
        Resolution idType = resolve.findIdentInType(newClassEnv, (JavaSymbol.TypeJavaSymbol) enclosingExpression.symbolType().symbol(), typeName, JavaSymbol.TYP);
        JavaType type = idType.type();
        if (typeTree.is(Tree.Kind.PARAMETERIZED_TYPE)) {
            TypeArguments typeArguments = ((ParameterizedTypeTree) typeTree).typeArguments();
            scan(typeArguments);
            type = parametrizedTypeWithTypeArguments(type.symbol, typeArguments);
        }
        registerType(typeTree, type);
    } else {
        resolveAs(typeTree, JavaSymbol.TYP, newClassEnv, false);
    }
    return (JavaType) typeTree.symbolType();
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) Resolution(org.sonar.java.resolve.Resolve.Resolution)

Aggregations

ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)3 TypeArguments (org.sonar.plugins.java.api.tree.TypeArguments)3 Test (org.junit.Test)2 Resolution (org.sonar.java.resolve.Resolve.Resolution)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1 WildcardTree (org.sonar.plugins.java.api.tree.WildcardTree)1