use of org.checkerframework.framework.type.AnnotatedTypeMirror 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.AnnotatedTypeMirror in project checker-framework by typetools.
the class StubParser method processEnum.
/**
* Gathers and returns a list of AnnotatedTypeVariable of the enum's type parameter
* declarations.
*
* @param decl actual enum declaration
* @param elt element representing enum
* @param atypes map of annotated types
* @param declAnnos map of declarations annotations
* @return list of AnnotatedTypeVariable of the enum's type parameter declarations
*/
private List<AnnotatedTypeVariable> processEnum(EnumDeclaration decl, TypeElement elt) {
annotateDecl(declAnnos, elt, decl.getAnnotations());
AnnotatedDeclaredType type = atypeFactory.fromElement(elt);
annotate(type, decl.getAnnotations());
putNew(atypes, elt, type);
List<AnnotatedTypeVariable> typeVariables = new ArrayList<>();
for (AnnotatedTypeMirror typeV : type.getTypeArguments()) {
if (typeV.getKind() != TypeKind.TYPEVAR) {
stubWarn("expected an AnnotatedTypeVariable but found type kind " + typeV.getKind() + ": " + typeV);
} else {
typeVariables.add((AnnotatedTypeVariable) typeV);
}
}
return typeVariables;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class StubParser method annotateAsArray.
/**
* Add the annotations from {@code type} to {@code atype}. Type annotations that parsed as
* declaration annotations (ie those in {@code declAnnos} are applied to the innermost component
* type.
*
* @param atype annotated type to which to add annotations
* @param type parsed type
* @param declAnnos annotations stored on the declaration of the variable with this type or null
*/
private void annotateAsArray(AnnotatedArrayType atype, ReferenceType type, NodeList<AnnotationExpr> declAnnos) {
annotateInnermostComponentType(atype, declAnnos);
Type typeDef = type;
AnnotatedTypeMirror currentAtype = atype;
while (typeDef.isArrayType() && currentAtype.getKind() == TypeKind.ARRAY) {
// handle generic type
clearAnnotations(currentAtype, typeDef);
List<AnnotationExpr> annotations = typeDef.getAnnotations();
if (annotations != null) {
annotate(currentAtype, annotations);
}
typeDef = ((com.github.javaparser.ast.type.ArrayType) typeDef).getComponentType();
currentAtype = ((AnnotatedArrayType) currentAtype).getComponentType();
if (typeDef.isArrayType() ^ currentAtype.getKind() == TypeKind.ARRAY) {
stubWarn("Mismatched array lengths; atype: " + atype + "%n type: " + type);
}
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class QualifierPolymorphism method annotate.
public void annotate(AnnotatedExecutableType functionalInterface, AnnotatedExecutableType memberReference) {
for (AnnotationMirror type : functionalInterface.getReturnType().getAnnotations()) {
if (isPolymorphicQualified(type)) {
// on memberReference.
return;
}
}
List<AnnotatedTypeMirror> args = functionalInterface.getParameterTypes();
List<AnnotatedTypeMirror> requiredArgs = memberReference.getParameterTypes();
if (args.size() == requiredArgs.size() + 1) {
// If the member reference is a reference to an instance method of an arbitrary
// object, then first parameter of the functional interface corresponds to the
// receiver of the member reference.
List<AnnotatedTypeMirror> newRequiredArgs = new ArrayList<>();
newRequiredArgs.add(memberReference.getReceiverType());
newRequiredArgs.addAll(requiredArgs);
requiredArgs = newRequiredArgs;
}
// Deal with varargs
if (memberReference.isVarArgs() && !functionalInterface.isVarArgs()) {
requiredArgs = AnnotatedTypes.expandVarArgsFromTypes(memberReference, args);
}
Map<AnnotationMirror, Set<? extends AnnotationMirror>> matchingMapping = collector.visit(args, requiredArgs);
if (matchingMapping != null && !matchingMapping.isEmpty()) {
replacer.visit(memberReference, matchingMapping);
} else {
// TODO: Do we need this (return type?)
completer.visit(memberReference);
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class DependentTypesHelper method standardizeReturnType.
public void standardizeReturnType(MethodTree m, AnnotatedTypeMirror atm) {
if (atm.getKind() == TypeKind.NONE) {
return;
}
if (!hasDependentType(atm)) {
return;
}
Element ele = TreeUtils.elementFromDeclaration(m);
TypeMirror enclosingType = ElementUtils.enclosingClass(ele).asType();
FlowExpressionContext context = FlowExpressionContext.buildContextForMethodDeclaration(m, enclosingType, factory.getContext());
standardizeDoNotUseLocals(context, factory.getPath(m), atm);
}
Aggregations