Search in sources :

Example 21 with AnnotatedTypeVariable

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

the class WholeProgramInferenceScenesHelper method updateAnnotationSetInScene.

/**
 * Updates the set of annotations in a location of a Scene.
 *
 * <ul>
 *   <li>If there was no previous annotation for that location, then the updated set will be the
 *       annotations in newATM.
 *   <li>If there was a previous annotation, the updated set will be the LUB between the
 *       previous annotation and newATM.
 * </ul>
 *
 * <p>
 *
 * @param type ATypeElement of the Scene which will be modified
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used
 * @param jaifPath used to identify a Scene
 * @param rhsATM the RHS of the annotated type on the source code
 * @param lhsATM the LHS of the annotated type on the source code
 * @param defLoc the location where the annotation will be added
 */
protected void updateAnnotationSetInScene(ATypeElement type, AnnotatedTypeFactory atf, String jaifPath, AnnotatedTypeMirror rhsATM, AnnotatedTypeMirror lhsATM, TypeUseLocation defLoc) {
    if (rhsATM instanceof AnnotatedNullType && ignoreNullAssignments) {
        return;
    }
    AnnotatedTypeMirror atmFromJaif = AnnotatedTypeMirror.createType(rhsATM.getUnderlyingType(), atf, false);
    typeElementToATM(atmFromJaif, type, atf);
    updatesATMWithLUB(atf, rhsATM, atmFromJaif);
    if (lhsATM instanceof AnnotatedTypeVariable) {
        Set<AnnotationMirror> upperAnnos = ((AnnotatedTypeVariable) lhsATM).getUpperBound().getEffectiveAnnotations();
        // current type on the source code, halt.
        if (upperAnnos.size() == rhsATM.getAnnotations().size() && atf.getQualifierHierarchy().isSubtype(rhsATM.getAnnotations(), upperAnnos)) {
            return;
        }
    }
    updateTypeElementFromATM(rhsATM, lhsATM, atf, type, 1, defLoc);
    modifiedScenes.add(jaifPath);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedNullType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedNullType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 22 with AnnotatedTypeVariable

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

the class WholeProgramInferenceScenesHelper method typeElementToATM.

/**
 * Updates an {@link org.checkerframework.framework.type.AnnotatedTypeMirror} to contain the
 * {@link scenelib.annotations.Annotation}s of an {@link scenelib.annotations.el.ATypeElement}.
 *
 * @param atm the AnnotatedTypeMirror to be modified
 * @param type the {@link scenelib.annotations.el.ATypeElement}
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used
 */
private void typeElementToATM(AnnotatedTypeMirror atm, ATypeElement type, AnnotatedTypeFactory atf) {
    Set<Annotation> annos = getSupportedAnnosInSet(type.tlAnnotationsHere, atf);
    for (Annotation anno : annos) {
        AnnotationMirror am = AnnotationConverter.annotationToAnnotationMirror(anno, atf.getProcessingEnv());
        atm.addAnnotation(am);
    }
    if (atm.getKind() == TypeKind.ARRAY) {
        AnnotatedArrayType aat = (AnnotatedArrayType) atm;
        for (ATypeElement innerType : type.innerTypes.values()) {
            typeElementToATM(aat.getComponentType(), innerType, atf);
        }
    }
    if (atm.getKind() == TypeKind.TYPEVAR) {
        AnnotatedTypeVariable atv = (AnnotatedTypeVariable) atm;
        for (ATypeElement innerType : type.innerTypes.values()) {
            typeElementToATM(atv.getUpperBound(), innerType, atf);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) Annotation(scenelib.annotations.Annotation) ATypeElement(scenelib.annotations.el.ATypeElement)

Example 23 with AnnotatedTypeVariable

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

the class CFAbstractValue method getEffectTypeVar.

/**
 * Returns the AnnotatedTypeVariable associated with the given TypeMirror or null.
 *
 * <p>If TypeMirror is a type variable, then the AnnotatedTypeVariable return is the declaration
 * of that TypeMirror. If the TypeMirror is a wildcard that extends a type variable, the
 * AnnotatedTypeVariable return is the declaration of that type variable. Otherwise, null is
 * returned.
 */
private AnnotatedTypeVariable getEffectTypeVar(TypeMirror typeMirror) {
    if (typeMirror == null) {
        return null;
    } else if (typeMirror.getKind() == TypeKind.WILDCARD) {
        return getEffectTypeVar(((WildcardType) typeMirror).getExtendsBound());
    } else if (typeMirror.getKind() == TypeKind.TYPEVAR) {
        TypeVariable typevar = ((TypeVariable) typeMirror);
        AnnotatedTypeMirror atm = analysis.getTypeFactory().getAnnotatedType(typevar.asElement());
        return (AnnotatedTypeVariable) atm;
    } else {
        return null;
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(javax.lang.model.type.WildcardType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 24 with AnnotatedTypeVariable

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

the class StubParser method annotateTypeParameters.

private void annotateTypeParameters(// for debugging
BodyDeclaration<?> decl, // for debugging; TypeElement or ExecutableElement
Object elt, Map<Element, AnnotatedTypeMirror> atypes, List<? extends AnnotatedTypeMirror> typeArguments, List<TypeParameter> typeParameters) {
    if (typeParameters == null) {
        return;
    }
    if (typeParameters.size() != typeArguments.size()) {
        String msg = String.format("annotateTypeParameters: mismatched sizes:  typeParameters (size %d)=%s;  typeArguments (size %d)=%s;  decl=%s;  elt=%s (%s).", typeParameters.size(), typeParameters, typeArguments.size(), typeArguments, decl.toString().replace(LINE_SEPARATOR, " "), elt.toString().replace(LINE_SEPARATOR, " "), elt.getClass());
        if (!debugStubParser) {
            msg = msg + "%n  For more details, run with -AstubDebug";
        }
        stubWarn(msg);
    }
    for (int i = 0; i < typeParameters.size(); ++i) {
        TypeParameter param = typeParameters.get(i);
        AnnotatedTypeVariable paramType = (AnnotatedTypeVariable) typeArguments.get(i);
        if (param.getTypeBound() == null || param.getTypeBound().isEmpty()) {
            // No bound so annotations are both lower and upper bounds
            annotate(paramType, param.getAnnotations());
        } else if (param.getTypeBound() != null && param.getTypeBound().size() > 0) {
            annotate(paramType.getLowerBound(), param.getAnnotations());
            annotate(paramType.getUpperBound(), param.getTypeBound().get(0), null);
            if (param.getTypeBound().size() > 1) {
                // TODO: add support for intersection types
                stubWarnNotFound("Annotations on intersection types are not yet supported");
            }
        }
        putNew(atypes, paramType.getUnderlyingType().asElement(), paramType);
    }
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 25 with AnnotatedTypeVariable

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

the class StubParser method processType.

/**
 * @return list of AnnotatedTypeVariable of the type's type parameter declarations
 */
private List<AnnotatedTypeVariable> processType(ClassOrInterfaceDeclaration decl, TypeElement elt) {
    annotateDecl(declAnnos, elt, decl.getAnnotations());
    AnnotatedDeclaredType type = atypeFactory.fromElement(elt);
    annotate(type, decl.getAnnotations());
    final List<? extends AnnotatedTypeMirror> typeArguments = type.getTypeArguments();
    final List<TypeParameter> typeParameters = decl.getTypeParameters();
    if (debugStubParser) {
        int numParams = (typeParameters == null ? 0 : typeParameters.size());
        int numArgs = (typeArguments == null ? 0 : typeArguments.size());
        if (numParams != numArgs) {
            stubDebug(String.format("parseType:  mismatched sizes for typeParameters=%s (size %d) and typeArguments=%s (size %d); decl=%s; elt=%s (%s); type=%s (%s); parseState=%s", typeParameters, numParams, typeArguments, numArgs, decl.toString().replace(LINE_SEPARATOR, " "), elt.toString().replace(LINE_SEPARATOR, " "), elt.getClass(), type, type.getClass(), parseState));
            stubDebug("Proceeding despite mismatched sizes");
        }
    }
    annotateTypeParameters(decl, elt, atypes, typeArguments, typeParameters);
    annotateSupertypes(decl, type);
    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 : TypeParameter(com.github.javaparser.ast.type.TypeParameter) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) 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