Search in sources :

Example 46 with AnnotatedTypeMirror

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

the class PropagationTypeAnnotator method getTypeParamFromEnclosingClass.

/**
 * Search parent's type arguments for wildcard. Using the index of wildcard, find the
 * corresponding type parameter element and return it. Returns null if the wildcard is the
 * result of substitution and therefore not in the list of type arguments.
 */
private Element getTypeParamFromEnclosingClass(final AnnotatedWildcardType wildcard, final AnnotatedDeclaredType parent) {
    Integer wildcardIndex = null;
    int currentIndex = 0;
    for (AnnotatedTypeMirror typeArg : parent.getTypeArguments()) {
        // which they should have been replaced by capture
        if (typeArg == wildcard) {
            wildcardIndex = currentIndex;
            break;
        }
        currentIndex += 1;
    }
    if (wildcardIndex != null) {
        final TypeElement typeElement = (TypeElement) typeFactory.getProcessingEnv().getTypeUtils().asElement(parent.getUnderlyingType());
        return typeElement.getTypeParameters().get(wildcardIndex);
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 47 with AnnotatedTypeMirror

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

the class EquivalentAtmComboScanner method scan.

protected RETURN_TYPE scan(Iterable<? extends AnnotatedTypeMirror> types1, Iterable<? extends AnnotatedTypeMirror> types2, PARAM param) {
    RETURN_TYPE r = null;
    boolean first = true;
    Iterator<? extends AnnotatedTypeMirror> tIter1 = types1.iterator();
    Iterator<? extends AnnotatedTypeMirror> tIter2 = types2.iterator();
    while (tIter1.hasNext() && tIter2.hasNext()) {
        final AnnotatedTypeMirror type1 = tIter1.next();
        final AnnotatedTypeMirror type2 = tIter2.next();
        r = first ? scan(type1, type2, param) : scanAndReduce(type1, type2, param, r);
    }
    return r;
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 48 with AnnotatedTypeMirror

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

the class AnnotatedTypes method asOuterSuper.

/**
 * Return the base type of type or any of its outer types that starts with the given type. If
 * none exists, return null.
 *
 * @param type a type
 * @param superType a type
 */
private static AnnotatedTypeMirror asOuterSuper(Types types, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror type, AnnotatedTypeMirror superType) {
    if (type.getKind() == TypeKind.DECLARED) {
        AnnotatedDeclaredType dt = (AnnotatedDeclaredType) type;
        AnnotatedDeclaredType enclosingType = dt;
        TypeMirror superTypeMirror = types.erasure(superType.getUnderlyingType());
        while (enclosingType != null) {
            TypeMirror enclosingTypeMirror = types.erasure(enclosingType.getUnderlyingType());
            if (types.isSubtype(enclosingTypeMirror, superTypeMirror)) {
                dt = enclosingType;
                break;
            }
            enclosingType = enclosingType.getEnclosingType();
        }
        if (enclosingType == null) {
            // checker.message(Kind.WARNING, msg);
            return superType;
        }
        return asSuper(atypeFactory, dt, superType);
    }
    return asSuper(atypeFactory, type, superType);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 49 with AnnotatedTypeMirror

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

the class AnnotatedTypes method addTypeVarMappings.

private static void addTypeVarMappings(Types types, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror t, TypeElement enclosingClassOfElem, Map<TypeVariable, AnnotatedTypeMirror> mappings) {
    if (enclosingClassOfElem.getTypeParameters().isEmpty()) {
        return;
    }
    AnnotatedDeclaredType enclosingType = atypeFactory.getAnnotatedType(enclosingClassOfElem);
    AnnotatedDeclaredType base = (AnnotatedDeclaredType) asOuterSuper(types, atypeFactory, t, enclosingType);
    final List<AnnotatedTypeVariable> ownerParams = new ArrayList<>(enclosingType.getTypeArguments().size());
    for (final AnnotatedTypeMirror typeParam : enclosingType.getTypeArguments()) {
        if (typeParam.getKind() != TypeKind.TYPEVAR) {
            ErrorReporter.errorAbort("Type arguments of a declaration should be type variables\n" + "enclosingClassOfElem=" + enclosingClassOfElem + "\n" + "enclosingType=" + enclosingType + "\n" + "typeMirror=" + t);
        }
        ownerParams.add((AnnotatedTypeVariable) typeParam);
    }
    List<AnnotatedTypeMirror> baseParams = base.getTypeArguments();
    if (ownerParams.size() != baseParams.size() && !base.wasRaw()) {
        ErrorReporter.errorAbort("Unexpected number of parameters.\n" + "enclosingType=" + enclosingType + "\n" + "baseType=" + base);
    }
    if (!ownerParams.isEmpty() && baseParams.isEmpty() && base.wasRaw()) {
        List<AnnotatedTypeMirror> newBaseParams = new ArrayList<>();
        for (AnnotatedTypeVariable arg : ownerParams) {
            // If base type was raw and the type arguments are missing,
            // set them to the erased type of the type variable.
            // (which is the erased type of the upper bound.)
            newBaseParams.add(arg.getErased());
        }
        baseParams = newBaseParams;
    }
    for (int i = 0; i < ownerParams.size(); ++i) {
        mappings.put(ownerParams.get(i).getUnderlyingType(), baseParams.get(i));
    }
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 50 with AnnotatedTypeMirror

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

the class AnnotatedTypes method findEffectiveAnnotations.

/**
 * When comparing types against the bounds of a type variable, we may encounter other type
 * variables, wildcards, and intersections in those bounds. This method traverses the bounds
 * until it finds a concrete type from which it can pull an annotation. This occurs for every
 * hierarchy in QualifierHierarchy
 *
 * @return the set of effective annotation mirrors in all hierarchies
 */
public static Set<AnnotationMirror> findEffectiveAnnotations(final QualifierHierarchy qualifierHierarchy, final AnnotatedTypeMirror toSearch) {
    AnnotatedTypeMirror source = toSearch;
    TypeKind kind = source.getKind();
    while (kind == TypeKind.TYPEVAR || kind == TypeKind.WILDCARD || kind == TypeKind.INTERSECTION) {
        switch(source.getKind()) {
            case TYPEVAR:
                source = ((AnnotatedTypeVariable) source).getUpperBound();
                break;
            case WILDCARD:
                source = ((AnnotatedWildcardType) source).getExtendsBound();
                break;
            case INTERSECTION:
                // if there are multiple conflicting annotations, choose the lowest
                final Set<AnnotationMirror> glb = glbOfBounds((AnnotatedIntersectionType) source, qualifierHierarchy);
                return glb;
            default:
                ErrorReporter.errorAbort("Unexpected AnnotatedTypeMirror with no primary annotation!" + "toSearch=" + toSearch + "source=" + source);
        }
        kind = source.getKind();
    }
    return source.getAnnotations();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeKind(javax.lang.model.type.TypeKind) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)188 AnnotationMirror (javax.lang.model.element.AnnotationMirror)42 ExpressionTree (com.sun.source.tree.ExpressionTree)32 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)27 Tree (com.sun.source.tree.Tree)25 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)25 VariableTree (com.sun.source.tree.VariableTree)22 ArrayList (java.util.ArrayList)22 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)21 MethodTree (com.sun.source.tree.MethodTree)20 TypeVariable (javax.lang.model.type.TypeVariable)19 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)18 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)17 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)16 TypeMirror (javax.lang.model.type.TypeMirror)16 VariableElement (javax.lang.model.element.VariableElement)15 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)13 MemberSelectTree (com.sun.source.tree.MemberSelectTree)13 NewClassTree (com.sun.source.tree.NewClassTree)13 Element (javax.lang.model.element.Element)13