Search in sources :

Example 1 with InnerTypeLocation

use of scenelib.annotations.el.InnerTypeLocation in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method updateTypeElementFromATM.

/**
 * Updates an {@link scenelib.annotations.el.ATypeElement} to have the annotations of an {@link
 * org.checkerframework.framework.type.AnnotatedTypeMirror} passed as argument. Annotations in
 * the original set that should be ignored (see {@link #shouldIgnore}) are not added to the
 * resulting set. This method also checks if the AnnotatedTypeMirror has explicit annotations in
 * source code, and if that is the case no annotations are added for that location.
 *
 * <p>This method removes from the ATypeElement all annotations supported by atf before
 * inserting new ones. It is assumed that every time this method is called, the
 * AnnotatedTypeMirror has a better type estimate for the ATypeElement. Therefore, it is not a
 * problem to remove all annotations before inserting the new annotations.
 *
 * @param newATM the AnnotatedTypeMirror whose annotations will be added to the ATypeElement
 * @param curATM used to check if the element which will be updated has explicit annotations in
 *     source code
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used
 * @param typeToUpdate the ATypeElement which will be updated
 * @param idx used to write annotations on compound types of an ATypeElement
 * @param defLoc the location where the annotation will be added
 */
private void updateTypeElementFromATM(AnnotatedTypeMirror newATM, AnnotatedTypeMirror curATM, AnnotatedTypeFactory atf, ATypeElement typeToUpdate, int idx, TypeUseLocation defLoc) {
    // The others stay intact.
    if (idx == 1) {
        // This if avoids clearing the annotations multiple times in cases
        // of type variables and compound types.
        Set<Annotation> annosToRemove = getSupportedAnnosInSet(typeToUpdate.tlAnnotationsHere, atf);
        // This method may be called consecutive times for the same ATypeElement.
        // Each time it is called, the AnnotatedTypeMirror has a better type
        // estimate for the ATypeElement. Therefore, it is not a problem to remove
        // all annotations before inserting the new annotations.
        typeToUpdate.tlAnnotationsHere.removeAll(annosToRemove);
    }
    // Only update the ATypeElement if there are no explicit annotations
    if (curATM.getExplicitAnnotations().size() == 0) {
        for (AnnotationMirror am : newATM.getAnnotations()) {
            addAnnotationsToATypeElement(newATM, atf, typeToUpdate, defLoc, am, curATM.hasEffectiveAnnotation(am));
        }
    } else if (curATM.getKind() == TypeKind.TYPEVAR) {
        // vars upper bound from being inserted.
        for (AnnotationMirror am : newATM.getAnnotations()) {
            if (curATM.getAnnotationInHierarchy(am) != null) {
                // in the same hierarchy.
                break;
            }
            addAnnotationsToATypeElement(newATM, atf, typeToUpdate, defLoc, am, curATM.hasEffectiveAnnotation(am));
        }
    }
    // Recursively update compound type and type variable type if they exist.
    if (newATM.getKind() == TypeKind.ARRAY && curATM.getKind() == TypeKind.ARRAY) {
        AnnotatedArrayType newAAT = (AnnotatedArrayType) newATM;
        AnnotatedArrayType oldAAT = (AnnotatedArrayType) curATM;
        updateTypeElementFromATM(newAAT.getComponentType(), oldAAT.getComponentType(), atf, typeToUpdate.innerTypes.vivify(new InnerTypeLocation(TypeAnnotationPosition.getTypePathFromBinary(Collections.nCopies(2 * idx, 0)))), idx + 1, defLoc);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) InnerTypeLocation(scenelib.annotations.el.InnerTypeLocation) Annotation(scenelib.annotations.Annotation)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)1 Annotation (scenelib.annotations.Annotation)1 InnerTypeLocation (scenelib.annotations.el.InnerTypeLocation)1