Search in sources :

Example 1 with GenericAnnotatedTypeFactory

use of org.checkerframework.framework.type.GenericAnnotatedTypeFactory in project checker-framework by typetools.

the class CFAbstractTransfer method addFieldValues.

private void addFieldValues(S info, AnnotatedTypeFactory factory, ClassTree classTree, MethodTree methodTree) {
    // Add knowledge about final fields, or values of non-final fields
    // if we are inside a constructor (information about initializers)
    TypeMirror classType = TreeUtils.typeOf(classTree);
    List<Pair<VariableElement, V>> fieldValues = analysis.getFieldValues();
    for (Pair<VariableElement, V> p : fieldValues) {
        VariableElement element = p.first;
        V value = p.second;
        if (ElementUtils.isFinal(element) || TreeUtils.isConstructor(methodTree)) {
            Receiver receiver;
            if (ElementUtils.isStatic(element)) {
                receiver = new ClassName(classType);
            } else {
                receiver = new ThisReference(classType);
            }
            TypeMirror fieldType = ElementUtils.getType(element);
            Receiver field = new FieldAccess(receiver, fieldType, element);
            info.insertValue(field, value);
        }
    }
    // add properties about fields (static information from type)
    boolean isNotFullyInitializedReceiver = isNotFullyInitializedReceiver(methodTree);
    if (isNotFullyInitializedReceiver && !TreeUtils.isConstructor(methodTree)) {
        // and the method isn't a constructor
        return;
    }
    for (Tree member : classTree.getMembers()) {
        if (member instanceof VariableTree) {
            VariableTree vt = (VariableTree) member;
            final VariableElement element = TreeUtils.elementFromDeclaration(vt);
            AnnotatedTypeMirror type = ((GenericAnnotatedTypeFactory<?, ?, ?, ?>) factory).getAnnotatedTypeLhs(vt);
            TypeMirror fieldType = ElementUtils.getType(element);
            Receiver receiver;
            if (ElementUtils.isStatic(element)) {
                receiver = new ClassName(classType);
            } else {
                receiver = new ThisReference(classType);
            }
            V value = analysis.createAbstractValue(type);
            if (value == null)
                continue;
            if (TreeUtils.isConstructor(methodTree)) {
                // if we are in a constructor,
                // then we can still use the static type, but only
                // if there is also an initializer that already does
                // some initialization.
                boolean found = false;
                for (Pair<VariableElement, V> fieldValue : fieldValues) {
                    if (fieldValue.first.equals(element)) {
                        value = value.leastUpperBound(fieldValue.second);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // no initializer found, cannot use static type
                    continue;
                }
            }
            Receiver field = new FieldAccess(receiver, fieldType, element);
            info.insertValue(field, value);
        }
    }
}
Also used : GenericAnnotatedTypeFactory(org.checkerframework.framework.type.GenericAnnotatedTypeFactory) VariableTree(com.sun.source.tree.VariableTree) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) VariableElement(javax.lang.model.element.VariableElement) ThisReference(org.checkerframework.dataflow.analysis.FlowExpressions.ThisReference) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) FieldAccess(org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess) Pair(org.checkerframework.javacutil.Pair)

Example 2 with GenericAnnotatedTypeFactory

use of org.checkerframework.framework.type.GenericAnnotatedTypeFactory in project checker-framework by typetools.

the class QualifierDefaults method applyDefaults.

/**
 * Applies default annotations to a type. A {@link com.sun.source.tree.Tree} that determines the
 * appropriate scope for defaults.
 *
 * <p>For instance, if the tree is associated with a declaration (e.g., it's the use of a field,
 * or a method invocation), defaults in the scope of the <i>declaration</i> are used; if the
 * tree is not associated with a declaration (e.g., a typecast), defaults in the scope of the
 * tree are used.
 *
 * @param tree the tree associated with the type
 * @param type the type to which defaults will be applied
 * @see #applyDefaultsElement(javax.lang.model.element.Element,
 *     org.checkerframework.framework.type.AnnotatedTypeMirror)
 */
private void applyDefaults(Tree tree, AnnotatedTypeMirror type) {
    // The location to take defaults from.
    Element elt;
    switch(tree.getKind()) {
        case MEMBER_SELECT:
            elt = TreeUtils.elementFromUse((MemberSelectTree) tree);
            break;
        case IDENTIFIER:
            elt = TreeUtils.elementFromUse((IdentifierTree) tree);
            break;
        case METHOD_INVOCATION:
            elt = TreeUtils.elementFromUse((MethodInvocationTree) tree);
            break;
        default:
            // If no associated symbol was found, use the tree's (lexical)
            // scope.
            elt = nearestEnclosingExceptLocal(tree);
    }
    // System.out.println("applyDefaults on tree " + tree +
    // " gives elt: " + elt + "(" + elt.getKind() + ")");
    boolean defaultTypeVarLocals = (atypeFactory instanceof GenericAnnotatedTypeFactory<?, ?, ?, ?>) && (((GenericAnnotatedTypeFactory<?, ?, ?, ?>) atypeFactory).getShouldDefaultTypeVarLocals());
    applyToTypeVar = defaultTypeVarLocals && elt != null && elt.getKind() == ElementKind.LOCAL_VARIABLE && type.getKind() == TypeKind.TYPEVAR;
    applyDefaultsElement(elt, type);
    applyToTypeVar = false;
}
Also used : GenericAnnotatedTypeFactory(org.checkerframework.framework.type.GenericAnnotatedTypeFactory) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Aggregations

GenericAnnotatedTypeFactory (org.checkerframework.framework.type.GenericAnnotatedTypeFactory)2 ClassTree (com.sun.source.tree.ClassTree)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 VariableTree (com.sun.source.tree.VariableTree)1 Element (javax.lang.model.element.Element)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeParameterElement (javax.lang.model.element.TypeParameterElement)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 ClassName (org.checkerframework.dataflow.analysis.FlowExpressions.ClassName)1 FieldAccess (org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess)1 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)1 ThisReference (org.checkerframework.dataflow.analysis.FlowExpressions.ThisReference)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 Pair (org.checkerframework.javacutil.Pair)1