Search in sources :

Example 46 with TypeVariable

use of javax.lang.model.type.TypeVariable in project buck by facebook.

the class StandaloneTypeVariableTest method testAsElement.

@Test
public void testAsElement() throws IOException {
    compile("class Foo<T> { }");
    TypeParameterElement tElement = elements.getTypeElement("Foo").getTypeParameters().get(0);
    TypeVariable tVar = (TypeVariable) tElement.asType();
    assertSame(tElement, tVar.asElement());
}
Also used : TypeVariable(javax.lang.model.type.TypeVariable) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Test(org.junit.Test)

Example 47 with TypeVariable

use of javax.lang.model.type.TypeVariable in project buck by facebook.

the class StandaloneTypeVariableTest method testGetUpperBoundMultipleBounds.

@Test
public void testGetUpperBoundMultipleBounds() throws IOException {
    compile("class Foo<T extends java.lang.CharSequence & java.lang.Runnable> { }");
    TypeMirror charSequenceType = elements.getTypeElement("java.lang.CharSequence").asType();
    TypeMirror runnableType = elements.getTypeElement("java.lang.Runnable").asType();
    TypeVariable tVar = (TypeVariable) elements.getTypeElement("Foo").getTypeParameters().get(0).asType();
    IntersectionType upperBound = (IntersectionType) tVar.getUpperBound();
    List<? extends TypeMirror> bounds = upperBound.getBounds();
    assertSame(2, bounds.size());
    assertSameType(charSequenceType, bounds.get(0));
    assertSameType(runnableType, bounds.get(1));
}
Also used : IntersectionType(javax.lang.model.type.IntersectionType) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) Test(org.junit.Test)

Example 48 with TypeVariable

use of javax.lang.model.type.TypeVariable in project buck by facebook.

the class StandaloneTypeVariableTest method testGetUpperBoundUnbounded.

@Test
public void testGetUpperBoundUnbounded() throws IOException {
    compile("class Foo<T> { }");
    TypeMirror objectType = elements.getTypeElement("java.lang.Object").asType();
    TypeVariable tVar = (TypeVariable) elements.getTypeElement("Foo").getTypeParameters().get(0).asType();
    assertSameType(objectType, tVar.getUpperBound());
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) Test(org.junit.Test)

Example 49 with TypeVariable

use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.

the class CFTreeBuilder method createAnnotatedType.

private Tree createAnnotatedType(AnnotatedTypeMirror annotatedType) {
    // Implementation based on com.sun.tools.javac.tree.TreeMaker.Type
    // Convert the annotations from a set of AnnotationMirrors
    // to a list of AnnotationTrees.
    Set<AnnotationMirror> annotations = annotatedType.getAnnotations();
    List<JCTree.JCAnnotation> annotationTrees = List.nil();
    for (AnnotationMirror am : annotations) {
        // TODO: what TypeAnnotationPosition should be used?
        Attribute.TypeCompound typeCompound = TypeAnnotationUtils.createTypeCompoundFromAnnotationMirror(am, TypeAnnotationUtils.unknownTAPosition(), env);
        JCTree.JCAnnotation annotationTree = maker.Annotation(typeCompound);
        JCTree.JCAnnotation typeAnnotationTree = maker.TypeAnnotation(annotationTree.getAnnotationType(), annotationTree.getArguments());
        typeAnnotationTree.attribute = typeCompound;
        annotationTrees = annotationTrees.append(typeAnnotationTree);
    }
    // Convert the underlying type from a TypeMirror to an
    // ExpressionTree and combine with the AnnotationTrees
    // to form a ClassTree of kind ANNOTATION_TYPE.
    Tree underlyingTypeTree;
    switch(annotatedType.getKind()) {
        case BYTE:
            underlyingTypeTree = maker.TypeIdent(TypeTag.BYTE);
            break;
        case CHAR:
            underlyingTypeTree = maker.TypeIdent(TypeTag.BYTE);
            break;
        case SHORT:
            underlyingTypeTree = maker.TypeIdent(TypeTag.SHORT);
            break;
        case INT:
            underlyingTypeTree = maker.TypeIdent(TypeTag.INT);
            break;
        case LONG:
            underlyingTypeTree = maker.TypeIdent(TypeTag.LONG);
            break;
        case FLOAT:
            underlyingTypeTree = maker.TypeIdent(TypeTag.FLOAT);
            break;
        case DOUBLE:
            underlyingTypeTree = maker.TypeIdent(TypeTag.DOUBLE);
            break;
        case BOOLEAN:
            underlyingTypeTree = maker.TypeIdent(TypeTag.BOOLEAN);
            break;
        case VOID:
            underlyingTypeTree = maker.TypeIdent(TypeTag.VOID);
            break;
        case TYPEVAR:
            {
                // No recursive annotations.
                AnnotatedTypeMirror.AnnotatedTypeVariable variable = (AnnotatedTypeMirror.AnnotatedTypeVariable) annotatedType;
                TypeVariable underlyingTypeVar = variable.getUnderlyingType();
                underlyingTypeTree = maker.Ident((Symbol.TypeSymbol) (underlyingTypeVar).asElement());
                break;
            }
        case WILDCARD:
            {
                AnnotatedTypeMirror.AnnotatedWildcardType wildcard = (AnnotatedTypeMirror.AnnotatedWildcardType) annotatedType;
                WildcardType wildcardType = wildcard.getUnderlyingType();
                if (wildcardType.getExtendsBound() != null) {
                    Tree annotatedExtendsBound = createAnnotatedType(wildcard.getExtendsBound());
                    underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), (JCTree) annotatedExtendsBound);
                } else if (wildcardType.getSuperBound() != null) {
                    Tree annotatedSuperBound = createAnnotatedType(wildcard.getSuperBound());
                    underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), (JCTree) annotatedSuperBound);
                } else {
                    underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                break;
            }
        case DECLARED:
            {
                underlyingTypeTree = maker.Type((Type) annotatedType.getUnderlyingType());
                if (underlyingTypeTree instanceof JCTree.JCTypeApply) {
                    // Replace the type parameters with annotated versions.
                    AnnotatedTypeMirror.AnnotatedDeclaredType annotatedDeclaredType = (AnnotatedTypeMirror.AnnotatedDeclaredType) annotatedType;
                    List<JCTree.JCExpression> typeArgTrees = List.nil();
                    for (AnnotatedTypeMirror arg : annotatedDeclaredType.getTypeArguments()) {
                        typeArgTrees = typeArgTrees.append((JCTree.JCExpression) createAnnotatedType(arg));
                    }
                    JCTree.JCExpression clazz = (JCTree.JCExpression) ((JCTree.JCTypeApply) underlyingTypeTree).getType();
                    underlyingTypeTree = maker.TypeApply(clazz, typeArgTrees);
                }
                break;
            }
        case ARRAY:
            {
                AnnotatedTypeMirror.AnnotatedArrayType annotatedArrayType = (AnnotatedTypeMirror.AnnotatedArrayType) annotatedType;
                Tree annotatedComponentTree = createAnnotatedType(annotatedArrayType.getComponentType());
                underlyingTypeTree = maker.TypeArray((JCTree.JCExpression) annotatedComponentTree);
                break;
            }
        case ERROR:
            underlyingTypeTree = maker.TypeIdent(TypeTag.ERROR);
            break;
        default:
            assert false : "unexpected type: " + annotatedType;
            underlyingTypeTree = null;
            break;
    }
    ((JCTree) underlyingTypeTree).setType((Type) annotatedType.getUnderlyingType());
    if (annotationTrees.isEmpty()) {
        return underlyingTypeTree;
    }
    JCTree.JCAnnotatedType annotatedTypeTree = maker.AnnotatedType(annotationTrees, (JCTree.JCExpression) underlyingTypeTree);
    annotatedTypeTree.setType((Type) annotatedType.getUnderlyingType());
    return annotatedTypeTree;
}
Also used : Attribute(com.sun.tools.javac.code.Attribute) Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) WildcardType(javax.lang.model.type.WildcardType) TypeVariable(javax.lang.model.type.TypeVariable) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.sun.source.tree.Tree) List(com.sun.tools.javac.util.List)

Example 50 with TypeVariable

use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.

the class DefaultTypeHierarchy method visitWildcardSubtype.

protected boolean visitWildcardSubtype(AnnotatedWildcardType subtype, AnnotatedTypeMirror supertype, VisitHistory visited) {
    if (subtype.isUninferredTypeArgument()) {
        return subtype.atypeFactory.ignoreUninferredTypeArguments;
    }
    TypeMirror superTypeMirror = supertype.getUnderlyingType();
    if (supertype.getKind() == TypeKind.TYPEVAR) {
        TypeVariable atv = (TypeVariable) supertype.getUnderlyingType();
        if (TypesUtils.isCaptured(atv)) {
            superTypeMirror = TypesUtils.getCapturedWildcard(atv);
        }
    }
    if (superTypeMirror.getKind() == TypeKind.WILDCARD) {
        // This can happen at a method invocation where a type variable in the method
        // declaration is substituted with a wildcard.
        // For example:
        // <T> void method(Gen<T> t) {}
        // Gen<?> x;
        // method(x); // this method is called when checking this method call
        // And also when checking lambdas
        boolean subtypeHasAnno = subtype.getAnnotationInHierarchy(currentTop) != null;
        boolean supertypeHasAnno = supertype.getAnnotationInHierarchy(currentTop) != null;
        if (subtypeHasAnno && supertypeHasAnno) {
            // as the bounds are the same
            return isPrimarySubtype(subtype, supertype, true);
        } else if (!subtypeHasAnno && !supertypeHasAnno && areEqualInHierarchy(subtype, supertype, currentTop)) {
            // Two unannotated uses of wildcard types are the same type
            return true;
        }
    }
    return isSubtype(subtype.getExtendsBound(), supertype, visited);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable)

Aggregations

TypeVariable (javax.lang.model.type.TypeVariable)80 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)38 TypeMirror (javax.lang.model.type.TypeMirror)30 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)22 TypeElement (javax.lang.model.element.TypeElement)21 ArrayList (java.util.ArrayList)16 DeclaredType (javax.lang.model.type.DeclaredType)15 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)13 Map (java.util.Map)13 ExecutableElement (javax.lang.model.element.ExecutableElement)12 TypeParameterElement (javax.lang.model.element.TypeParameterElement)12 Test (org.junit.Test)10 ArrayType (javax.lang.model.type.ArrayType)9 WildcardType (javax.lang.model.type.WildcardType)9 AnnotationMirror (javax.lang.model.element.AnnotationMirror)8 Element (javax.lang.model.element.Element)8 AnnotationMirrorMap (org.checkerframework.framework.util.AnnotationMirrorMap)8 AnnotationMirrorSet (org.checkerframework.framework.util.AnnotationMirrorSet)8 VariableElement (javax.lang.model.element.VariableElement)7