Search in sources :

Example 1 with DefaultQualifiers

use of org.checkerframework.framework.qual.DefaultQualifiers 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 2 with DefaultQualifiers

use of org.checkerframework.framework.qual.DefaultQualifiers 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)2 PackageElement (javax.lang.model.element.PackageElement)2 TypeParameterElement (javax.lang.model.element.TypeParameterElement)2 DefaultQualifier (org.checkerframework.framework.qual.DefaultQualifier)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 EnumSet (java.util.EnumSet)1 Set (java.util.Set)1