Search in sources :

Example 26 with AnnotatedTypeVariable

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

the class StubParser method annotate.

/**
 * Add to {@code atype}:
 *
 * <ol>
 *   <li>the annotations from {@code typeDef}, and
 *   <li>any type annotations that parsed as declaration annotations (ie those in {@code
 *       declAnnos}).
 * </ol>
 *
 * @param atype annotated type to which to add annotations
 * @param typeDef parsed type
 * @param declAnnos annotations stored on the declaration of the variable with this type, or
 *     null
 */
private void annotate(AnnotatedTypeMirror atype, Type typeDef, NodeList<AnnotationExpr> declAnnos) {
    if (atype.getKind() == TypeKind.ARRAY) {
        annotateAsArray((AnnotatedArrayType) atype, (ReferenceType) typeDef, declAnnos);
        return;
    }
    clearAnnotations(atype, typeDef);
    // Primary annotations for the type of a variable declaration are not stored in typeDef, but
    // rather as declaration annotations (passed as declAnnos to this method).  But, if typeDef
    // is not the type of a variable, then the primary annotations are stored in typeDef.
    NodeList<AnnotationExpr> primaryAnnotations;
    if (typeDef.getAnnotations().isEmpty() && declAnnos != null) {
        primaryAnnotations = declAnnos;
    } else {
        primaryAnnotations = typeDef.getAnnotations();
    }
    if (atype.getKind() != TypeKind.WILDCARD) {
        // The primary annotation on a wildcard applies to the super or extends bound and
        // are added below.
        annotate(atype, primaryAnnotations);
    }
    switch(atype.getKind()) {
        case DECLARED:
            ClassOrInterfaceType declType = unwrapDeclaredType(typeDef);
            if (declType == null) {
                break;
            }
            AnnotatedDeclaredType adeclType = (AnnotatedDeclaredType) atype;
            if (declType.getTypeArguments().isPresent() && !declType.getTypeArguments().get().isEmpty() && !adeclType.getTypeArguments().isEmpty()) {
                assert declType.getTypeArguments().get().size() == adeclType.getTypeArguments().size() : String.format("Mismatch in type argument size between %s (%d) and %s (%d)", declType, declType.getTypeArguments().get().size(), adeclType, adeclType.getTypeArguments().size());
                for (int i = 0; i < declType.getTypeArguments().get().size(); ++i) {
                    annotate(adeclType.getTypeArguments().get(i), declType.getTypeArguments().get().get(i), null);
                }
            }
            break;
        case WILDCARD:
            AnnotatedWildcardType wildcardType = (AnnotatedWildcardType) atype;
            WildcardType wildcardDef = (WildcardType) typeDef;
            if (wildcardDef.getExtendedType().isPresent()) {
                annotate(wildcardType.getExtendsBound(), wildcardDef.getExtendedType().get(), null);
                annotate(wildcardType.getSuperBound(), primaryAnnotations);
            } else if (wildcardDef.getSuperType().isPresent()) {
                annotate(wildcardType.getSuperBound(), wildcardDef.getSuperType().get(), null);
                annotate(wildcardType.getExtendsBound(), primaryAnnotations);
            } else {
                annotate(atype, primaryAnnotations);
            }
            break;
        case TYPEVAR:
            // Add annotations from the declaration of the TypeVariable
            AnnotatedTypeVariable typeVarUse = (AnnotatedTypeVariable) atype;
            for (AnnotatedTypeVariable typePar : typeParameters) {
                if (typePar.getUnderlyingType() == atype.getUnderlyingType()) {
                    AnnotatedTypeMerger.merge(typePar.getUpperBound(), typeVarUse.getUpperBound());
                    AnnotatedTypeMerger.merge(typePar.getLowerBound(), typeVarUse.getLowerBound());
                }
            }
            break;
        default:
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.github.javaparser.ast.type.WildcardType) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 27 with AnnotatedTypeVariable

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

the class AnnotatedTypeCopier method visitExecutable.

@Override
public AnnotatedTypeMirror visitExecutable(AnnotatedExecutableType original, IdentityHashMap<AnnotatedTypeMirror, AnnotatedTypeMirror> originalToCopy) {
    if (originalToCopy.containsKey(original)) {
        return originalToCopy.get(original);
    }
    final AnnotatedExecutableType copy = (AnnotatedExecutableType) AnnotatedTypeMirror.createType(original.getUnderlyingType(), original.atypeFactory, original.isDeclaration());
    maybeCopyPrimaryAnnotations(original, copy);
    originalToCopy.put(original, copy);
    copy.setElement(original.getElement());
    if (original.receiverType != null) {
        copy.receiverType = (AnnotatedDeclaredType) visit(original.receiverType, originalToCopy);
    }
    for (final AnnotatedTypeMirror param : original.paramTypes) {
        copy.paramTypes.add(visit(param, originalToCopy));
    }
    for (final AnnotatedTypeMirror thrown : original.throwsTypes) {
        copy.throwsTypes.add(visit(thrown, originalToCopy));
    }
    copy.returnType = visit(original.returnType, originalToCopy);
    for (final AnnotatedTypeVariable typeVariable : original.typeVarTypes) {
        // This field is needed to identify exactly when the declaration of an executable's
        // type parameter is visited.  When subtypes of this class visit the type parameter's
        // component types, they will likely set visitingExecutableTypeParam to false.
        // Therefore, we set this variable on each iteration of the loop.
        // See TypeVariableSubstitutor.Visitor.visitTypeVariable for an example of this.
        visitingExecutableTypeParam = true;
        copy.typeVarTypes.add((AnnotatedTypeVariable) visit(typeVariable, originalToCopy));
    }
    visitingExecutableTypeParam = false;
    return copy;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 28 with AnnotatedTypeVariable

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

the class DefaultInferredTypesApplier method apply.

private void apply(AnnotatedTypeMirror type, AnnotationMirror inferred, TypeMirror inferredTypeMirror, AnnotationMirror top) {
    AnnotationMirror primary = type.getAnnotationInHierarchy(top);
    if (inferred == null) {
        if (primary == null) {
        // Type doesn't have a primary either, nothing to remove
        } else if (type.getKind() == TypeKind.TYPEVAR) {
            removePrimaryAnnotationTypeVar((AnnotatedTypeVariable) type, inferredTypeMirror, top, primary);
        } else {
            removePrimaryTypeVarApplyUpperBound(type, inferredTypeMirror, top, primary);
        }
    } else {
        if (primary == null) {
            Set<AnnotationMirror> lowerbounds = AnnotatedTypes.findEffectiveLowerBoundAnnotations(hierarchy, type);
            AnnotationMirror lowerbound = hierarchy.findAnnotationInHierarchy(lowerbounds, top);
            if (omitSubtypingCheck || hierarchy.isSubtype(inferred, lowerbound)) {
                type.replaceAnnotation(inferred);
            }
        } else if ((omitSubtypingCheck || hierarchy.isSubtype(inferred, primary))) {
            type.replaceAnnotation(inferred);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 29 with AnnotatedTypeVariable

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

the class DefaultInferredTypesApplier method removePrimaryTypeVarApplyUpperBound.

private void removePrimaryTypeVarApplyUpperBound(AnnotatedTypeMirror type, TypeMirror inferredTypeMirror, AnnotationMirror top, AnnotationMirror notInferred) {
    if (inferredTypeMirror.getKind() != TypeKind.TYPEVAR) {
        ErrorReporter.errorAbort("Inferred value should not be missing annotations: " + inferredTypeMirror);
        return;
    }
    TypeVariable typeVar = (TypeVariable) inferredTypeMirror;
    AnnotatedTypeVariable typeVariableDecl = (AnnotatedTypeVariable) factory.getAnnotatedType(typeVar.asElement());
    AnnotationMirror upperBound = typeVariableDecl.getEffectiveAnnotationInHierarchy(top);
    if (omitSubtypingCheck || hierarchy.isSubtype(upperBound, notInferred)) {
        type.replaceAnnotation(upperBound);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 30 with AnnotatedTypeVariable

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

the class TypeFromTypeTreeVisitor method visitTypeParameter.

@Override
public AnnotatedTypeMirror visitTypeParameter(TypeParameterTree node, AnnotatedTypeFactory f) {
    List<AnnotatedTypeMirror> bounds = new ArrayList<>(node.getBounds().size());
    for (Tree t : node.getBounds()) {
        AnnotatedTypeMirror bound;
        if (visitedBounds.containsKey(t) && f == visitedBounds.get(t).atypeFactory) {
            bound = visitedBounds.get(t);
        } else {
            visitedBounds.put(t, f.type(t));
            bound = visit(t, f);
            visitedBounds.remove(t);
        }
        bounds.add(bound);
    }
    AnnotatedTypeVariable result = (AnnotatedTypeVariable) f.type(node);
    List<? extends AnnotationMirror> annotations = TreeUtils.annotationsFromTree(node);
    result.getLowerBound().addAnnotations(annotations);
    switch(bounds.size()) {
        case 0:
            break;
        case 1:
            result.setUpperBound(bounds.get(0));
            break;
        default:
            AnnotatedIntersectionType upperBound = (AnnotatedIntersectionType) result.getUpperBound();
            List<AnnotatedDeclaredType> superBounds = new ArrayList<>(bounds.size());
            for (AnnotatedTypeMirror b : bounds) {
                superBounds.add((AnnotatedDeclaredType) b);
            }
            upperBound.setDirectSuperTypes(superBounds);
    }
    return result;
}
Also used : AnnotatedIntersectionType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedIntersectionType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) MethodTree(com.sun.source.tree.MethodTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) WildcardTree(com.sun.source.tree.WildcardTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree) 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