Search in sources :

Example 1 with AnnotatedTypeVariable

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

the class StubParser method processEnum.

/**
 * Gathers and returns a list of AnnotatedTypeVariable of the enum's type parameter
 * declarations.
 *
 * @param decl actual enum declaration
 * @param elt element representing enum
 * @param atypes map of annotated types
 * @param declAnnos map of declarations annotations
 * @return list of AnnotatedTypeVariable of the enum's type parameter declarations
 */
private List<AnnotatedTypeVariable> processEnum(EnumDeclaration decl, TypeElement elt) {
    annotateDecl(declAnnos, elt, decl.getAnnotations());
    AnnotatedDeclaredType type = atypeFactory.fromElement(elt);
    annotate(type, decl.getAnnotations());
    putNew(atypes, elt, type);
    List<AnnotatedTypeVariable> typeVariables = new ArrayList<>();
    for (AnnotatedTypeMirror typeV : type.getTypeArguments()) {
        if (typeV.getKind() != TypeKind.TYPEVAR) {
            stubWarn("expected an AnnotatedTypeVariable but found type kind " + typeV.getKind() + ": " + typeV);
        } else {
            typeVariables.add((AnnotatedTypeVariable) typeV);
        }
    }
    return typeVariables;
}
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 2 with AnnotatedTypeVariable

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

the class DefaultTypeArgumentInference method handleNullTypeArguments.

/**
 * If one of the inferredArgs are NullType, then re-run inference ignoring null method
 * arguments. Then lub the result of the second inference with the NullType and put the new
 * result back into inferredArgs.
 *
 * @param typeFactory type factory
 * @param methodElem element of the method
 * @param methodType annotated type of the method
 * @param argTypes annotated types of arguments to the method
 * @param assignedTo annotated type to which the result of the method invocation is assigned
 * @param targets set of type variables to infer
 * @param inferredArgs map of type variables to the annotated types of their type arguments
 */
private void handleNullTypeArguments(AnnotatedTypeFactory typeFactory, ExecutableElement methodElem, AnnotatedExecutableType methodType, List<AnnotatedTypeMirror> argTypes, AnnotatedTypeMirror assignedTo, Set<TypeVariable> targets, Map<TypeVariable, AnnotatedTypeMirror> inferredArgs) {
    if (!hasNullType(inferredArgs)) {
        return;
    }
    final Map<TypeVariable, AnnotatedTypeMirror> inferredArgsWithOutNull = infer(typeFactory, argTypes, assignedTo, methodElem, methodType, targets, false);
    for (AnnotatedTypeVariable atv : methodType.getTypeVariables()) {
        TypeVariable typeVar = atv.getUnderlyingType();
        AnnotatedTypeMirror result = inferredArgs.get(typeVar);
        if (result == null) {
            AnnotatedTypeMirror withoutNullResult = inferredArgsWithOutNull.get(typeVar);
            if (withoutNullResult != null) {
                inferredArgs.put(typeVar, withoutNullResult);
            }
        } else if (result.getKind() == TypeKind.NULL) {
            AnnotatedTypeMirror withoutNullResult = inferredArgsWithOutNull.get(typeVar);
            if (withoutNullResult == null) {
                // withoutNullResult is null when the only constraint on a type argument is
                // where a method argument is null.
                withoutNullResult = typeFactory.getUninferredWildcardType(atv);
            }
            AnnotatedTypeMirror lub = AnnotatedTypes.leastUpperBound(typeFactory, withoutNullResult, result);
            inferredArgs.put(typeVar, lub);
        }
    }
}
Also used : TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 3 with AnnotatedTypeVariable

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

the class DefaultTypeArgumentInference method addConstraintsBetweenTargets.

/**
 * Declarations of the form: {@code <A, B extends A>} implies a TUConstraint of {@code B <: A}.
 * Add these to the constraint list.
 */
public void addConstraintsBetweenTargets(Set<TUConstraint> constraints, Set<TypeVariable> targets, boolean asSubtype, AnnotatedTypeFactory typeFactory) {
    final Types types = typeFactory.getProcessingEnv().getTypeUtils();
    final List<TypeVariable> targetList = new ArrayList<>(targets);
    final Map<TypeVariable, AnnotatedTypeVariable> paramDeclarations = new HashMap<>();
    for (int i = 0; i < targetList.size(); i++) {
        final TypeVariable earlierTarget = targetList.get(i);
        for (int j = i + 1; j < targetList.size(); j++) {
            final TypeVariable laterTarget = targetList.get(j);
            if (types.isSameType(earlierTarget.getUpperBound(), laterTarget)) {
                final AnnotatedTypeVariable headDecl = addOrGetDeclarations(earlierTarget, typeFactory, paramDeclarations);
                final AnnotatedTypeVariable nextDecl = addOrGetDeclarations(laterTarget, typeFactory, paramDeclarations);
                if (asSubtype) {
                    constraints.add(new TSubU(headDecl, nextDecl));
                } else {
                    constraints.add(new TSuperU(nextDecl, headDecl));
                }
            } else if (types.isSameType(laterTarget.getUpperBound(), earlierTarget)) {
                final AnnotatedTypeVariable headDecl = addOrGetDeclarations(earlierTarget, typeFactory, paramDeclarations);
                final AnnotatedTypeVariable nextDecl = addOrGetDeclarations(laterTarget, typeFactory, paramDeclarations);
                if (asSubtype) {
                    constraints.add(new TSubU(nextDecl, headDecl));
                } else {
                    constraints.add(new TSuperU(headDecl, nextDecl));
                }
            }
        }
    }
}
Also used : Types(javax.lang.model.util.Types) AnnotatedTypes(org.checkerframework.framework.util.AnnotatedTypes) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) TSuperU(org.checkerframework.framework.util.typeinference.constraint.TSuperU) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AFConstraint(org.checkerframework.framework.util.typeinference.constraint.AFConstraint) TUConstraint(org.checkerframework.framework.util.typeinference.constraint.TUConstraint) TSubU(org.checkerframework.framework.util.typeinference.constraint.TSubU)

Example 4 with AnnotatedTypeVariable

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

the class DefaultTypeArgumentInference method addOrGetDeclarations.

public AnnotatedTypeVariable addOrGetDeclarations(TypeVariable target, AnnotatedTypeFactory typeFactory, Map<TypeVariable, AnnotatedTypeVariable> declarations) {
    AnnotatedTypeVariable atv = declarations.get(target);
    if (atv == null) {
        atv = (AnnotatedTypeVariable) typeFactory.getAnnotatedType(target.asElement());
        declarations.put(target, atv);
    }
    return atv;
}
Also used : AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 5 with AnnotatedTypeVariable

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

the class DefaultTypeArgumentInference method handleUninferredTypeVariables.

/**
 * For any types we have not inferred, use a wildcard with the bounds from the original type
 * parameter.
 */
private void handleUninferredTypeVariables(AnnotatedTypeFactory typeFactory, AnnotatedExecutableType methodType, Set<TypeVariable> targets, Map<TypeVariable, AnnotatedTypeMirror> inferredArgs) {
    for (AnnotatedTypeVariable atv : methodType.getTypeVariables()) {
        final TypeVariable typeVar = atv.getUnderlyingType();
        if (targets.contains((TypeVariable) TypeAnnotationUtils.unannotatedType(typeVar))) {
            final AnnotatedTypeMirror inferredType = inferredArgs.get(typeVar);
            if (inferredType == null || TypeArgInferenceUtil.containsTypeParameter(inferredType, targets)) {
                AnnotatedTypeMirror dummy = typeFactory.getUninferredWildcardType(atv);
                inferredArgs.put(atv.getUnderlyingType(), dummy);
            }
        }
    }
}
Also used : TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

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