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);
}
}
}
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;
}
Aggregations