Search in sources :

Example 1 with DefaultQualifier

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

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

the class QualifierDefaults method defaultsAt.

private DefaultSet defaultsAt(final Element elt) {
    if (elt == null) {
        return DefaultSet.EMPTY;
    }
    if (elementDefaults.containsKey(elt)) {
        return elementDefaults.get(elt);
    }
    DefaultSet qualifiers = null;
    {
        DefaultQualifier d = elt.getAnnotation(DefaultQualifier.class);
        if (d != null) {
            qualifiers = new DefaultSet();
            Set<Default> p = fromDefaultQualifier(d);
            if (p != null) {
                qualifiers.addAll(p);
            }
        }
    }
    {
        DefaultQualifiers ds = elt.getAnnotation(DefaultQualifiers.class);
        if (ds != null) {
            if (qualifiers == null) {
                qualifiers = new DefaultSet();
            }
            for (DefaultQualifier d : ds.value()) {
                Set<Default> p = fromDefaultQualifier(d);
                if (p != null) {
                    qualifiers.addAll(p);
                }
            }
        }
    }
    Element parent;
    if (elt.getKind() == ElementKind.PACKAGE) {
        parent = ElementUtils.parentPackage((PackageElement) elt, elements);
    } else {
        parent = elt.getEnclosingElement();
    }
    DefaultSet parentDefaults = defaultsAt(parent);
    if (qualifiers == null || qualifiers.isEmpty()) {
        qualifiers = parentDefaults;
    } else {
        qualifiers.addAll(parentDefaults);
    }
    if (qualifiers != null && !qualifiers.isEmpty()) {
        elementDefaults.put(elt, qualifiers);
        return qualifiers;
    } else {
        return DefaultSet.EMPTY;
    }
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) PackageElement(javax.lang.model.element.PackageElement) DefaultQualifiers(org.checkerframework.framework.qual.DefaultQualifiers) DefaultQualifier(org.checkerframework.framework.qual.DefaultQualifier)

Example 3 with DefaultQualifier

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

the class QualifierDefaults method nearestEnclosingExceptLocal.

/**
 * Determines the nearest enclosing element for a tree by climbing the tree toward the root and
 * obtaining the element for the first declaration (variable, method, or class) that encloses
 * the tree. Initializers of local variables are handled in a special way: within an initializer
 * we look for the DefaultQualifier(s) annotation and keep track of the previously visited tree.
 * TODO: explain the behavior better.
 *
 * @param tree the tree
 * @return the nearest enclosing element for a tree
 */
private Element nearestEnclosingExceptLocal(Tree tree) {
    TreePath path = atypeFactory.getPath(tree);
    if (path == null) {
        Element method = atypeFactory.getEnclosingMethod(tree);
        if (method != null) {
            return method;
        } else {
            return TreeUtils.elementFromTree(tree);
        }
    }
    Tree prev = null;
    for (Tree t : path) {
        switch(t.getKind()) {
            case VARIABLE:
                VariableTree vtree = (VariableTree) t;
                ExpressionTree vtreeInit = vtree.getInitializer();
                if (vtreeInit != null && prev == vtreeInit) {
                    Element elt = TreeUtils.elementFromDeclaration((VariableTree) t);
                    DefaultQualifier d = elt.getAnnotation(DefaultQualifier.class);
                    DefaultQualifiers ds = elt.getAnnotation(DefaultQualifiers.class);
                    if (d == null && ds == null) {
                        break;
                    }
                }
                if (prev != null && prev.getKind() == Tree.Kind.MODIFIERS) {
                    // type.
                    break;
                }
                return TreeUtils.elementFromDeclaration((VariableTree) t);
            case METHOD:
                return TreeUtils.elementFromDeclaration((MethodTree) t);
            case CLASS:
            case ENUM:
            case INTERFACE:
            case ANNOTATION_TYPE:
                return TreeUtils.elementFromDeclaration((ClassTree) t);
            // Do nothing.
            default:
        }
        prev = t;
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) DefaultQualifiers(org.checkerframework.framework.qual.DefaultQualifiers) DefaultQualifier(org.checkerframework.framework.qual.DefaultQualifier)

Aggregations

Element (javax.lang.model.element.Element)3 DefaultQualifier (org.checkerframework.framework.qual.DefaultQualifier)3 PackageElement (javax.lang.model.element.PackageElement)2 TypeParameterElement (javax.lang.model.element.TypeParameterElement)2 DefaultQualifiers (org.checkerframework.framework.qual.DefaultQualifiers)2 ClassTree (com.sun.source.tree.ClassTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 IdentifierTree (com.sun.source.tree.IdentifierTree)1 MemberSelectTree (com.sun.source.tree.MemberSelectTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 MethodTree (com.sun.source.tree.MethodTree)1 Tree (com.sun.source.tree.Tree)1 TypeParameterTree (com.sun.source.tree.TypeParameterTree)1 VariableTree (com.sun.source.tree.VariableTree)1 TreePath (com.sun.source.util.TreePath)1 Target (java.lang.annotation.Target)1 EnumSet (java.util.EnumSet)1 Set (java.util.Set)1 MirroredTypesException (javax.lang.model.type.MirroredTypesException)1 TypeKind (javax.lang.model.type.TypeKind)1