Search in sources :

Example 1 with DefaultFor

use of org.checkerframework.framework.qual.DefaultFor 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 DefaultFor

use of org.checkerframework.framework.qual.DefaultFor 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) {
    // 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);
        }
        if (qual.getAnnotation(DefaultQualifierInHierarchy.class) != null) {
            defs.addCheckedCodeDefault(AnnotationBuilder.fromClass(elements, qual), TypeUseLocation.OTHERWISE);
        }
    }
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) DefaultQualifierInHierarchy(org.checkerframework.framework.qual.DefaultQualifierInHierarchy) DefaultFor(org.checkerframework.framework.qual.DefaultFor)

Example 3 with DefaultFor

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

the class SubtypingAnnotatedTypeFactory method addCheckedCodeDefaults.

/**
 * If necessary, make Unqualified the default qualifier. Keep most logic in sync with super.
 *
 * @see
 *     org.checkerframework.framework.type.GenericAnnotatedTypeFactory#addCheckedCodeDefaults(org.checkerframework.framework.util.defaults.QualifierDefaults)
 */
@Override
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)

Example 4 with DefaultFor

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

the class WholeProgramInferenceScenesStorage 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.util.defaults.QualifierDefaults}. Look into the
 * createQualifierDefaults method in {@link GenericAnnotatedTypeFactory} (which uses the
 * QualifierDefaults class linked above) before changing anything here. See
 * https://github.com/typetools/checker-framework/issues/683 .
 *
 * @param am an annotation to test for whether it should be inserted into source code
 * @param location where the location would be inserted; used to determine if {@code am} is the
 *     default for that location
 * @param atm its kind is used to determine if {@code am} is the default for that kind
 * @return true if am should not be inserted into source code, or if am is invisible
 */
private boolean shouldIgnore(AnnotationMirror am, TypeUseLocation location, 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 a default annotation.
    // This case checks if it is meta-annotated with @DefaultFor.
    // TODO: Handle cases of annotations added via an
    // org.checkerframework.framework.type.treeannotator.LiteralTreeAnnotator.
    DefaultFor defaultFor = elt.getAnnotation(DefaultFor.class);
    if (defaultFor != null) {
        org.checkerframework.framework.qual.TypeKind[] types = defaultFor.typeKinds();
        TypeKind atmKind = atm.getUnderlyingType().getKind();
        if (hasMatchingTypeKind(atmKind, types)) {
            return true;
        }
    }
    return false;
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) Target(java.lang.annotation.Target) DefaultQualifierInHierarchy(org.checkerframework.framework.qual.DefaultQualifierInHierarchy) ATypeElement(scenelib.annotations.el.ATypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeKind(javax.lang.model.type.TypeKind) InvisibleQualifier(org.checkerframework.framework.qual.InvisibleQualifier) DefaultFor(org.checkerframework.framework.qual.DefaultFor) DefaultQualifier(org.checkerframework.framework.qual.DefaultQualifier)

Aggregations

DefaultFor (org.checkerframework.framework.qual.DefaultFor)4 DefaultQualifierInHierarchy (org.checkerframework.framework.qual.DefaultQualifierInHierarchy)4 TypeUseLocation (org.checkerframework.framework.qual.TypeUseLocation)4 Target (java.lang.annotation.Target)2 Element (javax.lang.model.element.Element)2 TypeKind (javax.lang.model.type.TypeKind)2 DefaultQualifier (org.checkerframework.framework.qual.DefaultQualifier)2 InvisibleQualifier (org.checkerframework.framework.qual.InvisibleQualifier)2 ATypeElement (scenelib.annotations.el.ATypeElement)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 VariableElement (javax.lang.model.element.VariableElement)1 MirroredTypesException (javax.lang.model.type.MirroredTypesException)1 TypeMirror (javax.lang.model.type.TypeMirror)1 ImplicitFor (org.checkerframework.framework.qual.ImplicitFor)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)1 AClass (scenelib.annotations.el.AClass)1