Search in sources :

Example 1 with TypeUseLocation

use of org.checkerframework.framework.qual.TypeUseLocation in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method shouldIgnore.

/**
 * Returns true if {@code am} should not be inserted in source code, for example {@link
 * org.checkerframework.common.value.qual.BottomVal}. This happens when {@code am} cannot be
 * inserted in source code or is the default for the location passed as argument.
 *
 * <p>Invisible qualifiers, which are annotations that contain the {@link
 * org.checkerframework.framework.qual.InvisibleQualifier} meta-annotation, also return true.
 *
 * <p>TODO: Merge functionality somewhere else with {@link
 * org.checkerframework.framework.type.GenericAnnotatedTypeFactory#createQualifierDefaults}.
 * Look into the createQualifierDefaults method before changing anything here. See Issue 683
 * https://github.com/typetools/checker-framework/issues/683
 */
private boolean shouldIgnore(AnnotationMirror am, TypeUseLocation location, AnnotatedTypeFactory atf, AnnotatedTypeMirror atm) {
    Element elt = am.getAnnotationType().asElement();
    // Checks if am is an implementation detail (a type qualifier used
    // internally by the type system and not meant to be seen by the user.)
    Target target = elt.getAnnotation(Target.class);
    if (target != null && target.value().length == 0)
        return true;
    if (elt.getAnnotation(InvisibleQualifier.class) != null)
        return true;
    // Checks if am is default
    if (elt.getAnnotation(DefaultQualifierInHierarchy.class) != null) {
        return true;
    }
    DefaultQualifier defaultQual = elt.getAnnotation(DefaultQualifier.class);
    if (defaultQual != null) {
        for (TypeUseLocation loc : defaultQual.locations()) {
            if (loc == TypeUseLocation.ALL || loc == location) {
                return true;
            }
        }
    }
    DefaultFor defaultQualForLocation = elt.getAnnotation(DefaultFor.class);
    if (defaultQualForLocation != null) {
        for (TypeUseLocation loc : defaultQualForLocation.value()) {
            if (loc == TypeUseLocation.ALL || loc == location) {
                return true;
            }
        }
    }
    // Checks if am is an implicit annotation.
    // This case checks if it is meta-annotated with @ImplicitFor.
    // TODO: Handle cases of implicit annotations added via an
    // org.checkerframework.framework.type.treeannotator.ImplicitsTreeAnnotator.
    ImplicitFor implicitFor = elt.getAnnotation(ImplicitFor.class);
    if (implicitFor != null) {
        org.checkerframework.framework.qual.TypeKind[] types = implicitFor.types();
        TypeKind atmKind = atm.getUnderlyingType().getKind();
        if (hasMatchingTypeKind(atmKind, types)) {
            return true;
        }
        try {
            Class<?>[] names = implicitFor.typeNames();
            for (Class<?> c : names) {
                TypeMirror underlyingtype = atm.getUnderlyingType();
                while (underlyingtype instanceof javax.lang.model.type.ArrayType) {
                    underlyingtype = ((javax.lang.model.type.ArrayType) underlyingtype).getComponentType();
                }
                if (c.getCanonicalName().equals(atm.getUnderlyingType().toString())) {
                    return true;
                }
            }
        } catch (MirroredTypesException e) {
        }
    }
    return false;
}
Also used : MirroredTypesException(javax.lang.model.type.MirroredTypesException) ImplicitFor(org.checkerframework.framework.qual.ImplicitFor) ATypeElement(scenelib.annotations.el.ATypeElement) Element(javax.lang.model.element.Element) TypeKind(javax.lang.model.type.TypeKind) InvisibleQualifier(org.checkerframework.framework.qual.InvisibleQualifier) DefaultFor(org.checkerframework.framework.qual.DefaultFor) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) Target(java.lang.annotation.Target) DefaultQualifierInHierarchy(org.checkerframework.framework.qual.DefaultQualifierInHierarchy) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) AClass(scenelib.annotations.el.AClass) DefaultQualifier(org.checkerframework.framework.qual.DefaultQualifier)

Example 2 with TypeUseLocation

use of org.checkerframework.framework.qual.TypeUseLocation in project checker-framework by typetools.

the class QualifierDefaults method addClimbStandardDefaults.

/**
 * Add standard CLIMB defaults that do not conflict with previously added defaults.
 */
public void addClimbStandardDefaults() {
    QualifierHierarchy qualHierarchy = this.atypeFactory.getQualifierHierarchy();
    Set<? extends AnnotationMirror> tops = qualHierarchy.getTopAnnotations();
    Set<? extends AnnotationMirror> bottoms = qualHierarchy.getBottomAnnotations();
    for (TypeUseLocation loc : standardClimbDefaultsTop) {
        for (AnnotationMirror top : tops) {
            if (!conflictsWithExistingDefaults(checkedCodeDefaults, top, loc)) {
                // Only add standard defaults in locations where a default has not been
                // specified
                addCheckedCodeDefault(top, loc);
            }
        }
    }
    for (TypeUseLocation loc : standardClimbDefaultsBottom) {
        for (AnnotationMirror bottom : bottoms) {
            if (!conflictsWithExistingDefaults(checkedCodeDefaults, bottom, loc)) {
                // Only add standard defaults in locations where a default has not been
                // specified
                addCheckedCodeDefault(bottom, loc);
            }
        }
    }
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) AnnotationMirror(javax.lang.model.element.AnnotationMirror) QualifierHierarchy(org.checkerframework.framework.type.QualifierHierarchy)

Example 3 with TypeUseLocation

use of org.checkerframework.framework.qual.TypeUseLocation in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method addAnnotationsToATypeElement.

private void addAnnotationsToATypeElement(AnnotatedTypeMirror newATM, AnnotatedTypeFactory atf, ATypeElement typeToUpdate, TypeUseLocation defLoc, AnnotationMirror am, boolean isEffectiveAnnotation) {
    Annotation anno = AnnotationConverter.annotationMirrorToAnnotation(am);
    if (anno != null) {
        typeToUpdate.tlAnnotationsHere.add(anno);
        if (isEffectiveAnnotation || shouldIgnore(am, defLoc, atf, newATM)) {
            // firstKey works as a unique identifier for each annotation
            // that should not be inserted in source code
            String firstKey = typeToUpdate.description.toString() + typeToUpdate.tlAnnotationsHere.toString();
            Pair<String, TypeUseLocation> key = Pair.of(firstKey, defLoc);
            Set<String> annosIgnored = annosToIgnore.get(key);
            if (annosIgnored == null) {
                annosIgnored = new HashSet<>();
                annosToIgnore.put(key, annosIgnored);
            }
            annosIgnored.add(anno.def().toString());
        }
    }
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) Annotation(scenelib.annotations.Annotation)

Example 4 with TypeUseLocation

use of org.checkerframework.framework.qual.TypeUseLocation in project checker-framework by typetools.

the class GenericAnnotatedTypeFactory method addUncheckedCodeDefaults.

/**
 * Adds default qualifiers for code that is not type-checked by reading
 * {@code @DefaultInUncheckedCodeFor} and {@code @DefaultQualifierInHierarchyInUncheckedCode}
 * meta-annotations. Then it applies the standard unchecked code defaults, if a default was not
 * specified for a particular location.
 *
 * <p>Standard unchecked code default are: <br>
 * top: {@code TypeUseLocation.RETURN,TypeUseLocation.FIELD,TypeUseLocation.UPPER_BOUND}<br>
 * bottom: {@code TypeUseLocation.PARAMETER, TypeUseLocation.LOWER_BOUND}<br>
 *
 * <p>If {@code @DefaultQualifierInHierarchyInUncheckedCode} code is not found or a default for
 * {@code TypeUseLocation.Otherwise} is not used, the defaults for checked code will be applied
 * to locations without a default for unchecked code.
 *
 * <p>Subclasses may override this method to add defaults that cannot be specified with a
 * {@code @DefaultInUncheckedCodeFor} or {@code @DefaultQualifierInHierarchyInUncheckedCode}
 * meta-annotations or to change the standard defaults.
 *
 * @param defs {@link QualifierDefaults} object to which defaults are added
 */
protected void addUncheckedCodeDefaults(QualifierDefaults defs) {
    for (Class<? extends Annotation> annotation : getSupportedTypeQualifiers()) {
        DefaultInUncheckedCodeFor defaultInUncheckedCodeFor = annotation.getAnnotation(DefaultInUncheckedCodeFor.class);
        if (defaultInUncheckedCodeFor != null) {
            final TypeUseLocation[] locations = defaultInUncheckedCodeFor.value();
            defs.addUncheckedCodeDefaults(AnnotationBuilder.fromClass(elements, annotation), locations);
        }
        if (annotation.getAnnotation(DefaultQualifierInHierarchyInUncheckedCode.class) != null) {
            defs.addUncheckedCodeDefault(AnnotationBuilder.fromClass(elements, annotation), TypeUseLocation.OTHERWISE);
        }
    }
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) DefaultQualifierInHierarchyInUncheckedCode(org.checkerframework.framework.qual.DefaultQualifierInHierarchyInUncheckedCode) DefaultInUncheckedCodeFor(org.checkerframework.framework.qual.DefaultInUncheckedCodeFor)

Example 5 with TypeUseLocation

use of org.checkerframework.framework.qual.TypeUseLocation in project checker-framework by typetools.

the class GenericAnnotatedTypeFactory method addCheckedCodeDefaults.

/**
 * Adds default qualifiers for type-checked code by reading {@link DefaultFor} and {@link
 * DefaultQualifierInHierarchy} meta-annotations. Subclasses may override this method to add
 * defaults that cannot be specified with a {@link DefaultFor} or {@link
 * DefaultQualifierInHierarchy} meta-annotations.
 *
 * @param defs QualifierDefault object to which defaults are added
 */
protected void addCheckedCodeDefaults(QualifierDefaults defs) {
    boolean foundOtherwise = false;
    // Add defaults from @DefaultFor and @DefaultQualifierInHierarchy
    for (Class<? extends Annotation> qual : getSupportedTypeQualifiers()) {
        DefaultFor defaultFor = qual.getAnnotation(DefaultFor.class);
        if (defaultFor != null) {
            final TypeUseLocation[] locations = defaultFor.value();
            defs.addCheckedCodeDefaults(AnnotationBuilder.fromClass(elements, qual), locations);
            foundOtherwise = foundOtherwise || Arrays.asList(locations).contains(TypeUseLocation.OTHERWISE);
        }
        if (qual.getAnnotation(DefaultQualifierInHierarchy.class) != null) {
            defs.addCheckedCodeDefault(AnnotationBuilder.fromClass(elements, qual), TypeUseLocation.OTHERWISE);
            foundOtherwise = true;
        }
    }
    // If Unqualified is a supported qualifier, make it the default.
    AnnotationMirror unqualified = AnnotationBuilder.fromClass(elements, Unqualified.class);
    if (!foundOtherwise && this.isSupportedQualifier(unqualified)) {
        defs.addCheckedCodeDefault(unqualified, TypeUseLocation.OTHERWISE);
    }
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) AnnotationMirror(javax.lang.model.element.AnnotationMirror) DefaultQualifierInHierarchy(org.checkerframework.framework.qual.DefaultQualifierInHierarchy) DefaultFor(org.checkerframework.framework.qual.DefaultFor)

Aggregations

TypeUseLocation (org.checkerframework.framework.qual.TypeUseLocation)7 AnnotationMirror (javax.lang.model.element.AnnotationMirror)4 DefaultFor (org.checkerframework.framework.qual.DefaultFor)2 DefaultQualifierInHierarchy (org.checkerframework.framework.qual.DefaultQualifierInHierarchy)2 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)2 Annotation (java.lang.annotation.Annotation)1 Target (java.lang.annotation.Target)1 Element (javax.lang.model.element.Element)1 MirroredTypeException (javax.lang.model.type.MirroredTypeException)1 MirroredTypesException (javax.lang.model.type.MirroredTypesException)1 TypeKind (javax.lang.model.type.TypeKind)1 TypeMirror (javax.lang.model.type.TypeMirror)1 DefaultInUncheckedCodeFor (org.checkerframework.framework.qual.DefaultInUncheckedCodeFor)1 DefaultQualifier (org.checkerframework.framework.qual.DefaultQualifier)1 DefaultQualifierInHierarchyInUncheckedCode (org.checkerframework.framework.qual.DefaultQualifierInHierarchyInUncheckedCode)1 ImplicitFor (org.checkerframework.framework.qual.ImplicitFor)1 InvisibleQualifier (org.checkerframework.framework.qual.InvisibleQualifier)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)1 Annotation (scenelib.annotations.Annotation)1