Search in sources :

Example 31 with AnnotatedTypeVariable

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable 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 32 with AnnotatedTypeVariable

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

the class AnnotatedTypeComparer method visitTypeVariable.

@Override
public R visitTypeVariable(AnnotatedTypeVariable type, AnnotatedTypeMirror p) {
    R r;
    if (visitedNodes.containsKey(type)) {
        return visitedNodes.get(type);
    }
    visitedNodes.put(type, null);
    if (p instanceof AnnotatedTypeVariable) {
        AnnotatedTypeVariable tv = (AnnotatedTypeVariable) p;
        r = scan(type.getLowerBound(), tv.getLowerBound());
        visitedNodes.put(type, r);
        r = scanAndReduce(type.getUpperBound(), tv.getUpperBound(), r);
        visitedNodes.put(type, r);
    } else {
        r = scan(type.getLowerBound(), p.getErased());
        visitedNodes.put(type, r);
        r = scanAndReduce(type.getUpperBound(), p.getErased(), r);
        visitedNodes.put(type, r);
    }
    return r;
}
Also used : AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 33 with AnnotatedTypeVariable

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable 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 34 with AnnotatedTypeVariable

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

the class AtmLubVisitor method visitTypevar_Typevar.

@Override
public Void visitTypevar_Typevar(AnnotatedTypeVariable type1, AnnotatedTypeVariable type2, AnnotatedTypeMirror lub1) {
    if (visited(lub1)) {
        return null;
    }
    AnnotatedTypeVariable lub = castLub(type1, lub1);
    visit(type1.getUpperBound(), type2.getUpperBound(), lub.getUpperBound());
    visit(type1.getLowerBound(), type2.getLowerBound(), lub.getLowerBound());
    lubPrimaryOnBoundedType(type1, type2, lub);
    return null;
}
Also used : AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 35 with AnnotatedTypeVariable

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable 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.isCaptured((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)

Aggregations

AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)40 TypeVariable (javax.lang.model.type.TypeVariable)16 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)16 ArrayList (java.util.ArrayList)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)9 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)9 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)7 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeElement (javax.lang.model.element.TypeElement)3 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)3 AnnotationMirrorSet (org.checkerframework.framework.util.AnnotationMirrorSet)3 TUConstraint (org.checkerframework.framework.util.typeinference.constraint.TUConstraint)3 TypeParameter (com.github.javaparser.ast.type.TypeParameter)2 ClassTree (com.sun.source.tree.ClassTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 MethodTree (com.sun.source.tree.MethodTree)2 TypeParameterTree (com.sun.source.tree.TypeParameterTree)2