Search in sources :

Example 11 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class DefaultTypeHierarchy method isContainedBy.

/**
 * A declared type is considered a supertype of another declared type only if all of the type
 * arguments of the declared type "contain" the corresponding type arguments of the subtype.
 * Containment is described in the JLS section 4.5.1 "Type Arguments of Parameterized Types",
 * https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.5.1
 *
 * @param inside the "subtype" type argument
 * @param outside the "supertype" type argument
 * @param visited a history of type pairs that have been visited, used to halt on recursive
 *     bounds
 * @param canBeCovariant whether or not type arguments are allowed to be covariant
 * @return true if inside is contained by outside OR, if canBeCovariant == true, inside is a
 *     subtype of outside
 */
protected boolean isContainedBy(final AnnotatedTypeMirror inside, final AnnotatedTypeMirror outside, VisitHistory visited, boolean canBeCovariant) {
    if (ignoreUninferredTypeArgument(inside) || ignoreUninferredTypeArgument(outside)) {
        return true;
    }
    if (outside.getKind() == TypeKind.WILDCARD) {
        final AnnotatedWildcardType outsideWc = (AnnotatedWildcardType) outside;
        AnnotatedTypeMirror outsideWcUB = outsideWc.getExtendsBound();
        if (inside.getKind() == TypeKind.WILDCARD) {
            outsideWcUB = checker.getTypeFactory().widenToUpperBound(outsideWcUB, (AnnotatedWildcardType) inside);
        }
        while (outsideWcUB.getKind() == TypeKind.WILDCARD) {
            outsideWcUB = ((AnnotatedWildcardType) outsideWcUB).getExtendsBound();
        }
        AnnotatedTypeMirror castedInside = castedAsSuper(inside, outsideWcUB);
        if (!checkAndSubtype(castedInside, outsideWcUB, visited)) {
            return false;
        }
        return canBeCovariant || checkAndSubtype(outsideWc.getSuperBound(), inside, visited);
    } else {
        // ARE_EQUAL -> DO CAPTURE CONVERSION
        if (canBeCovariant) {
            return isSubtype(inside, outside, visited);
        }
        return areEqualInHierarchy(inside, outside, currentTop);
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)

Example 12 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class PropagationTypeAnnotator method visitWildcard.

/**
 * Rather than defaulting the missing bounds of a wildcard, find the bound annotations on the
 * type parameter it replaced. Place those annotations on the wildcard.
 *
 * @param wildcardAtm type to annotate
 */
@Override
public Void visitWildcard(AnnotatedWildcardType wildcardAtm, Void aVoid) {
    if (visitedNodes.containsKey(wildcardAtm) || pause) {
        return null;
    }
    visitedNodes.put(wildcardAtm, null);
    final WildcardType wildcard = (WildcardType) wildcardAtm.getUnderlyingType();
    Element typeParamElement = TypesUtils.wildcardToTypeParam(wildcard);
    if (typeParamElement == null) {
        typeParamElement = (parents.isEmpty()) ? null : getTypeParamFromEnclosingClass(wildcardAtm, parents.peekFirst());
    }
    if (typeParamElement != null) {
        pause = true;
        AnnotatedTypeVariable typeParam = (AnnotatedTypeVariable) typeFactory.getAnnotatedType(typeParamElement);
        pause = false;
        final Set<? extends AnnotationMirror> tops = typeFactory.getQualifierHierarchy().getTopAnnotations();
        if (wildcard.isUnbound()) {
            propagateExtendsBound(wildcardAtm, typeParam, tops);
            propagateSuperBound(wildcardAtm, typeParam, tops);
        } else if (wildcard.isExtendsBound()) {
            propagateSuperBound(wildcardAtm, typeParam, tops);
        } else {
            // is super bound
            propagateExtendsBound(wildcardAtm, typeParam, tops);
        }
    }
    scan(wildcardAtm.getExtendsBound(), null);
    scan(wildcardAtm.getSuperBound(), null);
    return null;
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 13 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class AnnotatedTypes method containsModifierImpl.

/*
     * For type variables we might hit the same type again. We keep a list of visited types.
     */
private static boolean containsModifierImpl(AnnotatedTypeMirror type, AnnotationMirror modifier, List<AnnotatedTypeMirror> visited) {
    boolean found = type.hasAnnotation(modifier);
    boolean vis = visited.contains(type);
    visited.add(type);
    if (!found && !vis) {
        if (type.getKind() == TypeKind.DECLARED) {
            AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) type;
            for (AnnotatedTypeMirror typeMirror : declaredType.getTypeArguments()) {
                found |= containsModifierImpl(typeMirror, modifier, visited);
                if (found) {
                    break;
                }
            }
        } else if (type.getKind() == TypeKind.ARRAY) {
            AnnotatedArrayType arrayType = (AnnotatedArrayType) type;
            found = containsModifierImpl(arrayType.getComponentType(), modifier, visited);
        } else if (type.getKind() == TypeKind.TYPEVAR) {
            AnnotatedTypeVariable atv = (AnnotatedTypeVariable) type;
            if (atv.getUpperBound() != null) {
                found = containsModifierImpl(atv.getUpperBound(), modifier, visited);
            }
            if (!found && atv.getLowerBound() != null) {
                found = containsModifierImpl(atv.getLowerBound(), modifier, visited);
            }
        } else if (type.getKind() == TypeKind.WILDCARD) {
            AnnotatedWildcardType awc = (AnnotatedWildcardType) type;
            if (awc.getExtendsBound() != null) {
                found = containsModifierImpl(awc.getExtendsBound(), modifier, visited);
            }
            if (!found && awc.getSuperBound() != null) {
                found = containsModifierImpl(awc.getSuperBound(), modifier, visited);
            }
        }
    }
    return found;
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 14 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class AnnotatedTypes method getIteratedType.

/**
 * Returns the iterated type of the passed iterable type, and throws {@link
 * IllegalArgumentException} if the passed type is not iterable.
 *
 * <p>The iterated type is the component type of an array, and the type argument of {@link
 * Iterable} for declared types.
 *
 * @param iterableType the iterable type (either array or declared)
 * @return the types of elements in the iterable type
 */
public static AnnotatedTypeMirror getIteratedType(ProcessingEnvironment processingEnv, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror iterableType) {
    if (iterableType.getKind() == TypeKind.ARRAY) {
        return ((AnnotatedArrayType) iterableType).getComponentType();
    }
    // For type variables and wildcards take the effective upper bound.
    if (iterableType.getKind() == TypeKind.WILDCARD) {
        return getIteratedType(processingEnv, atypeFactory, ((AnnotatedWildcardType) iterableType).getExtendsBound().deepCopy());
    }
    if (iterableType.getKind() == TypeKind.TYPEVAR) {
        return getIteratedType(processingEnv, atypeFactory, ((AnnotatedTypeVariable) iterableType).getUpperBound());
    }
    if (iterableType.getKind() != TypeKind.DECLARED) {
        ErrorReporter.errorAbort("AnnotatedTypes.getIteratedType: not iterable type: " + iterableType);
        // dead code
        return null;
    }
    TypeElement iterableElement = processingEnv.getElementUtils().getTypeElement("java.lang.Iterable");
    AnnotatedDeclaredType iterableElmType = atypeFactory.getAnnotatedType(iterableElement);
    AnnotatedDeclaredType dt = asSuper(atypeFactory, iterableType, iterableElmType);
    if (dt.getTypeArguments().isEmpty()) {
        TypeElement e = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
        AnnotatedDeclaredType t = atypeFactory.getAnnotatedType(e);
        return t;
    } else {
        return dt.getTypeArguments().get(0);
    }
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) TypeElement(javax.lang.model.element.TypeElement) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)

Example 15 with AnnotatedWildcardType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType in project checker-framework by typetools.

the class AtmLubVisitor method visitWildcard_Wildcard.

@Override
public Void visitWildcard_Wildcard(AnnotatedWildcardType type1, AnnotatedWildcardType type2, AnnotatedTypeMirror lub1) {
    if (visited(lub1)) {
        return null;
    }
    AnnotatedWildcardType lub = castLub(type1, lub1);
    visit(type1.getExtendsBound(), type2.getExtendsBound(), lub.getExtendsBound());
    visit(type1.getSuperBound(), type2.getSuperBound(), lub.getSuperBound());
    lubPrimaryOnBoundedType(type1, type2, lub);
    return null;
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)

Aggregations

AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)19 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)7 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)6 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)5 TypeMirror (javax.lang.model.type.TypeMirror)4 ArrayList (java.util.ArrayList)3 TypeElement (javax.lang.model.element.TypeElement)3 WildcardType (com.sun.tools.javac.code.Type.WildcardType)2 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)1 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 WildcardType (com.github.javaparser.ast.type.WildcardType)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Element (javax.lang.model.element.Element)1