Search in sources :

Example 41 with TypeVariable

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

the class DefaultInferredTypesApplier method removePrimaryAnnotationTypeVar.

private void removePrimaryAnnotationTypeVar(AnnotatedTypeVariable annotatedTypeVariable, TypeMirror inferredTypeMirror, AnnotationMirror top, AnnotationMirror previousAnnotation) {
    if (inferredTypeMirror.getKind() != TypeKind.TYPEVAR) {
        throw new BugInCF("Missing annos");
    }
    TypeVariable typeVar = (TypeVariable) inferredTypeMirror;
    AnnotatedTypeVariable typeVariableDecl = (AnnotatedTypeVariable) factory.getAnnotatedType(typeVar.asElement());
    AnnotationMirror upperBound = typeVariableDecl.getEffectiveAnnotationInHierarchy(top);
    if (omitSubtypingCheck || hierarchy.isSubtype(upperBound, previousAnnotation)) {
        annotatedTypeVariable.removeAnnotationInHierarchy(top);
        AnnotationMirror ub = typeVariableDecl.getUpperBound().getAnnotationInHierarchy(top);
        apply(annotatedTypeVariable.getUpperBound(), ub, typeVar.getUpperBound(), top);
        AnnotationMirror lb = typeVariableDecl.getLowerBound().getAnnotationInHierarchy(top);
        apply(annotatedTypeVariable.getLowerBound(), lb, typeVar.getLowerBound(), top);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 42 with TypeVariable

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

the class TryCatchFrame method possibleLabels.

/**
 * Given a type of thrown exception, add the set of possible control flow successor {@link Label}s
 * to the argument set. Return true if the exception is known to be caught by one of those labels
 * and false if it may propagate still further.
 */
@Override
public boolean possibleLabels(TypeMirror thrown, Set<Label> labels) {
    while (!(thrown instanceof DeclaredType)) {
        assert thrown instanceof TypeVariable : "thrown type must be a variable or a declared type";
        thrown = ((TypeVariable) thrown).getUpperBound();
    }
    DeclaredType declaredThrown = (DeclaredType) thrown;
    assert thrown != null : "thrown type must be bounded by a declared type";
    for (Pair<TypeMirror, Label> pair : catchLabels) {
        TypeMirror caught = pair.first;
        boolean canApply = false;
        if (caught.getKind() == TypeKind.DECLARED) {
            DeclaredType declaredCaught = (DeclaredType) caught;
            if (types.isSubtype(declaredThrown, declaredCaught)) {
                // No later catch blocks can apply.
                labels.add(pair.second);
                return true;
            } else if (types.isSubtype(declaredCaught, declaredThrown)) {
                canApply = true;
            }
        } else {
            assert caught.getKind() == TypeKind.UNION : "caught type must be a union or a declared type";
            UnionType caughtUnion = (UnionType) caught;
            for (TypeMirror alternative : caughtUnion.getAlternatives()) {
                assert alternative.getKind() == TypeKind.DECLARED : "alternatives of an caught union type must be declared types";
                DeclaredType declaredAlt = (DeclaredType) alternative;
                if (types.isSubtype(declaredThrown, declaredAlt)) {
                    // No later catch blocks can apply.
                    labels.add(pair.second);
                    return true;
                } else if (types.isSubtype(declaredAlt, declaredThrown)) {
                    canApply = true;
                }
            }
        }
        if (canApply) {
            labels.add(pair.second);
        }
    }
    return false;
}
Also used : UnionType(javax.lang.model.type.UnionType) TypeVariable(javax.lang.model.type.TypeVariable) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 43 with TypeVariable

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

the class TypesUtils method order.

/**
 * Returns the list of type variables such that a type variable in the list only references type
 * variables at a lower index than itself.
 *
 * @param collection a collection of type variables
 * @param types type utilities
 * @return the type variables ordered so that each type variable only references earlier type
 *     variables
 */
public static List<TypeVariable> order(Collection<TypeVariable> collection, Types types) {
    List<TypeVariable> list = new ArrayList<>(collection);
    List<TypeVariable> ordered = new ArrayList<>();
    while (!list.isEmpty()) {
        TypeVariable free = doesNotContainOthers(list, types);
        list.remove(free);
        ordered.add(free);
    }
    return ordered;
}
Also used : TypeVariable(javax.lang.model.type.TypeVariable) ArrayList(java.util.ArrayList)

Example 44 with TypeVariable

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

the class BaseTypeValidator method visitParameterizedType.

/**
 * Checks that the annotations on the type arguments supplied to a type or a method invocation are
 * within the bounds of the type variables as declared, and issues the "type.argument" error if
 * they are not.
 *
 * @param type the type to check
 * @param tree the type's tree
 */
protected Void visitParameterizedType(AnnotatedDeclaredType type, ParameterizedTypeTree tree) {
    if (TreeUtils.isDiamondTree(tree)) {
        return null;
    }
    final TypeElement element = (TypeElement) type.getUnderlyingType().asElement();
    if (checker.shouldSkipUses(element)) {
        return null;
    }
    AnnotatedDeclaredType capturedType = (AnnotatedDeclaredType) atypeFactory.applyCaptureConversion(type);
    List<AnnotatedTypeParameterBounds> bounds = atypeFactory.typeVariablesFromUse(capturedType, element);
    visitor.checkTypeArguments(tree, bounds, capturedType.getTypeArguments(), tree.getTypeArguments(), element.getSimpleName(), element.getTypeParameters());
    @SuppressWarnings(// applyCaptureConversion returns the passed type if type does not
    "interning:not.interned") boolean // have wildcards.
    hasCapturedTypeVariables = capturedType != type;
    if (hasCapturedTypeVariables) {
        // Check that the extends bound of the captured type variable is a subtype of the extends
        // bound of the wildcard.
        int numTypeArgs = capturedType.getTypeArguments().size();
        // First create a mapping from captured type variable to its wildcard.
        Map<TypeVariable, AnnotatedTypeMirror> typeVarToWildcard = new HashMap<>(numTypeArgs);
        for (int i = 0; i < numTypeArgs; i++) {
            AnnotatedTypeMirror captureTypeArg = capturedType.getTypeArguments().get(i);
            if (TypesUtils.isCapturedTypeVariable(captureTypeArg.getUnderlyingType()) && type.getTypeArguments().get(i).getKind() == TypeKind.WILDCARD) {
                AnnotatedTypeVariable capturedTypeVar = (AnnotatedTypeVariable) captureTypeArg;
                AnnotatedWildcardType wildcard = (AnnotatedWildcardType) type.getTypeArguments().get(i);
                typeVarToWildcard.put(capturedTypeVar.getUnderlyingType(), wildcard);
            }
        }
        for (int i = 0; i < numTypeArgs; i++) {
            AnnotatedTypeMirror captureTypeArg = capturedType.getTypeArguments().get(i);
            if (type.getTypeArguments().get(i).getKind() == TypeKind.WILDCARD) {
                AnnotatedWildcardType wildcard = (AnnotatedWildcardType) type.getTypeArguments().get(i);
                if (TypesUtils.isCapturedTypeVariable(captureTypeArg.getUnderlyingType())) {
                    AnnotatedTypeVariable capturedTypeVar = (AnnotatedTypeVariable) captureTypeArg;
                    // Substitute the captured type variables with their wildcards. Without this, the
                    // isSubtype check crashes because wildcards aren't comparable with type variables.
                    AnnotatedTypeMirror catpureTypeVarUB = atypeFactory.getTypeVarSubstitutor().substituteWithoutCopyingTypeArguments(typeVarToWildcard, capturedTypeVar.getUpperBound());
                    if (!atypeFactory.getTypeHierarchy().isSubtype(catpureTypeVarUB, wildcard.getExtendsBound())) {
                        checker.reportError(tree.getTypeArguments().get(i), "type.argument", element.getTypeParameters().get(i), element.getSimpleName(), wildcard.getExtendsBound(), capturedTypeVar.getUpperBound());
                    }
                } else if (AnnotatedTypes.isExplicitlySuperBounded(wildcard)) {
                    // bound.
                    if (!(atypeFactory.getQualifierHierarchy().isSubtype(wildcard.getSuperBound().getEffectiveAnnotations(), wildcard.getExtendsBound().getAnnotations()) && atypeFactory.getQualifierHierarchy().isSubtype(wildcard.getExtendsBound().getAnnotations(), wildcard.getSuperBound().getEffectiveAnnotations()))) {
                        checker.reportError(tree.getTypeArguments().get(i), "super.wildcard", wildcard.getExtendsBound(), wildcard.getSuperBound());
                    }
                }
            }
        }
    }
    return null;
}
Also used : AnnotatedTypeParameterBounds(org.checkerframework.framework.type.AnnotatedTypeParameterBounds) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 45 with TypeVariable

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

the class StandaloneTypeVariableTest method testGetKind.

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

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