Search in sources :

Example 21 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 22 with AnnotatedWildcardType

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

the class AtmLubVisitor method lubTypeArgument.

private void lubTypeArgument(AnnotatedTypeMirror type1, AnnotatedTypeMirror type2, AnnotatedTypeMirror lub) {
    // In lub(), asSuper is called on type1 and type2, but asSuper does not recur into type
    // arguments, so call asSuper on the type arguments so that they have the same underlying type.
    final AnnotatedTypeMirror type1AsLub = AnnotatedTypes.asSuper(atypeFactory, type1, lub);
    final AnnotatedTypeMirror type2AsLub = AnnotatedTypes.asSuper(atypeFactory, type2, lub);
    // but Gen<@A ? super @B Object> is returned.)
    if (lub.getKind() == TypeKind.WILDCARD) {
        if (visited(lub)) {
            return;
        }
        AnnotatedWildcardType type1Wildcard = (AnnotatedWildcardType) type1AsLub;
        AnnotatedWildcardType type2Wildcard = (AnnotatedWildcardType) type2AsLub;
        AnnotatedWildcardType lubWildcard = (AnnotatedWildcardType) lub;
        if (type1Wildcard.isUninferredTypeArgument() || type2Wildcard.isUninferredTypeArgument()) {
            lubWildcard.setUninferredTypeArgument();
        }
        lubWildcard(type1Wildcard.getSuperBound(), type1Wildcard.getExtendsBound(), type2Wildcard.getSuperBound(), type2Wildcard.getExtendsBound(), lubWildcard.getSuperBound(), lubWildcard.getExtendsBound());
    } else if (lub.getKind() == TypeKind.TYPEVAR && TypesUtils.isCapturedTypeVariable((TypeVariable) lub.getUnderlyingType())) {
        if (visited(lub)) {
            return;
        }
        AnnotatedTypeVariable type1typevar = (AnnotatedTypeVariable) type1AsLub;
        AnnotatedTypeVariable type2typevar = (AnnotatedTypeVariable) type2AsLub;
        AnnotatedTypeVariable lubTypevar = (AnnotatedTypeVariable) lub;
        lubWildcard(type1typevar.getLowerBound(), type1typevar.getUpperBound(), type2typevar.getLowerBound(), type2typevar.getUpperBound(), lubTypevar.getLowerBound(), lubTypevar.getUpperBound());
    } else {
        // Don't add to visit history because that will happen in visitTypevar_Typevar or
        // visitWildcard_Wildcard if needed.
        visit(type1AsLub, type2AsLub, lub);
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 23 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)

Example 24 with AnnotatedWildcardType

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

the class PropagationTypeAnnotator method visitDeclared.

/**
 * Sometimes the underlying type parameters of AnnotatedWildcardTypes are not available on the
 * wildcards themselves. Instead, record enclosing class to find the type parameter to use as a
 * backup in visitWildcards.
 *
 * @param declaredType type to record
 */
@Override
public Void visitDeclared(AnnotatedDeclaredType declaredType, Void aVoid) {
    if (pause) {
        return null;
    }
    if (declaredType.isUnderlyingTypeRaw()) {
        // Copy annotations from the declaration to the wildcards.
        AnnotatedDeclaredType declaration = (AnnotatedDeclaredType) typeFactory.fromElement(declaredType.getUnderlyingType().asElement());
        List<AnnotatedTypeMirror> typeArgs = declaredType.getTypeArguments();
        for (int i = 0; i < typeArgs.size(); i++) {
            if (typeArgs.get(i).getKind() != TypeKind.WILDCARD || !((AnnotatedWildcardType) typeArgs.get(i)).isUninferredTypeArgument()) {
                // Sometimes the framework infers a more precise type argument, so just use it.
                continue;
            }
            AnnotatedTypeVariable typeParam = (AnnotatedTypeVariable) declaration.getTypeArguments().get(i);
            AnnotatedWildcardType wct = (AnnotatedWildcardType) typeArgs.get(i);
            wct.getExtendsBound().replaceAnnotations(typeParam.getUpperBound().getAnnotations());
            wct.getSuperBound().replaceAnnotations(typeParam.getLowerBound().getAnnotations());
            wct.replaceAnnotations(typeParam.getAnnotations());
        }
    }
    parents.addFirst(declaredType);
    super.visitDeclared(declaredType, aVoid);
    parents.removeFirst();
    return null;
}
Also used : 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 25 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 && !parents.isEmpty()) {
        typeParamElement = getTypeParameterElement(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)

Aggregations

AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)30 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)14 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)13 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)9 TypeMirror (javax.lang.model.type.TypeMirror)8 HashMap (java.util.HashMap)5 TypeElement (javax.lang.model.element.TypeElement)5 TypeVariable (javax.lang.model.type.TypeVariable)5 WildcardType (com.sun.tools.javac.code.Type.WildcardType)4 BugInCF (org.checkerframework.javacutil.BugInCF)4 ArrayList (java.util.ArrayList)3 DeclaredType (javax.lang.model.type.DeclaredType)3 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)2 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)2 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 WildcardType (com.github.javaparser.ast.type.WildcardType)2 IdentityHashMap (java.util.IdentityHashMap)2 Element (javax.lang.model.element.Element)2