use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable 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.AnnotatedTypeVariable in project checker-framework by typetools.
the class DefaultTypeArgumentInference method handleNullTypeArguments.
/**
* If one of the inferredArgs are NullType, then re-run inference ignoring null method
* arguments. Then lub the result of the second inference with the NullType and put the new
* result back into inferredArgs.
*
* @param typeFactory type factory
* @param methodElem element of the method
* @param methodType annotated type of the method
* @param argTypes annotated types of arguments to the method
* @param assignedTo annotated type to which the result of the method invocation is assigned
* @param targets set of type variables to infer
* @param inferredArgs map of type variables to the annotated types of their type arguments
*/
private void handleNullTypeArguments(AnnotatedTypeFactory typeFactory, ExecutableElement methodElem, AnnotatedExecutableType methodType, List<AnnotatedTypeMirror> argTypes, AnnotatedTypeMirror assignedTo, Set<TypeVariable> targets, Map<TypeVariable, AnnotatedTypeMirror> inferredArgs) {
if (!hasNullType(inferredArgs)) {
return;
}
final Map<TypeVariable, AnnotatedTypeMirror> inferredArgsWithOutNull = infer(typeFactory, argTypes, assignedTo, methodElem, methodType, targets, false);
for (AnnotatedTypeVariable atv : methodType.getTypeVariables()) {
TypeVariable typeVar = atv.getUnderlyingType();
AnnotatedTypeMirror result = inferredArgs.get(typeVar);
if (result == null) {
AnnotatedTypeMirror withoutNullResult = inferredArgsWithOutNull.get(typeVar);
if (withoutNullResult != null) {
inferredArgs.put(typeVar, withoutNullResult);
}
} else if (result.getKind() == TypeKind.NULL) {
AnnotatedTypeMirror withoutNullResult = inferredArgsWithOutNull.get(typeVar);
if (withoutNullResult == null) {
// withoutNullResult is null when the only constraint on a type argument is
// where a method argument is null.
withoutNullResult = typeFactory.getUninferredWildcardType(atv);
}
AnnotatedTypeMirror lub = AnnotatedTypes.leastUpperBound(typeFactory, withoutNullResult, result);
inferredArgs.put(typeVar, lub);
}
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable in project checker-framework by typetools.
the class DefaultTypeArgumentInference method addConstraintsBetweenTargets.
/**
* Declarations of the form: {@code <A, B extends A>} implies a TUConstraint of {@code B <: A}.
* Add these to the constraint list.
*/
public void addConstraintsBetweenTargets(Set<TUConstraint> constraints, Set<TypeVariable> targets, boolean asSubtype, AnnotatedTypeFactory typeFactory) {
final Types types = typeFactory.getProcessingEnv().getTypeUtils();
final List<TypeVariable> targetList = new ArrayList<>(targets);
final Map<TypeVariable, AnnotatedTypeVariable> paramDeclarations = new HashMap<>();
for (int i = 0; i < targetList.size(); i++) {
final TypeVariable earlierTarget = targetList.get(i);
for (int j = i + 1; j < targetList.size(); j++) {
final TypeVariable laterTarget = targetList.get(j);
if (types.isSameType(earlierTarget.getUpperBound(), laterTarget)) {
final AnnotatedTypeVariable headDecl = addOrGetDeclarations(earlierTarget, typeFactory, paramDeclarations);
final AnnotatedTypeVariable nextDecl = addOrGetDeclarations(laterTarget, typeFactory, paramDeclarations);
if (asSubtype) {
constraints.add(new TSubU(headDecl, nextDecl));
} else {
constraints.add(new TSuperU(nextDecl, headDecl));
}
} else if (types.isSameType(laterTarget.getUpperBound(), earlierTarget)) {
final AnnotatedTypeVariable headDecl = addOrGetDeclarations(earlierTarget, typeFactory, paramDeclarations);
final AnnotatedTypeVariable nextDecl = addOrGetDeclarations(laterTarget, typeFactory, paramDeclarations);
if (asSubtype) {
constraints.add(new TSubU(nextDecl, headDecl));
} else {
constraints.add(new TSuperU(headDecl, nextDecl));
}
}
}
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable in project checker-framework by typetools.
the class DefaultTypeArgumentInference method addOrGetDeclarations.
public AnnotatedTypeVariable addOrGetDeclarations(TypeVariable target, AnnotatedTypeFactory typeFactory, Map<TypeVariable, AnnotatedTypeVariable> declarations) {
AnnotatedTypeVariable atv = declarations.get(target);
if (atv == null) {
atv = (AnnotatedTypeVariable) typeFactory.getAnnotatedType(target.asElement());
declarations.put(target, atv);
}
return atv;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable in project checker-framework by typetools.
the class DefaultTypeArgumentInference method handleUninferredTypeVariables.
/**
* For any types we have not inferred, use a wildcard with the bounds from the original type
* parameter.
*/
private void handleUninferredTypeVariables(AnnotatedTypeFactory typeFactory, AnnotatedExecutableType methodType, Set<TypeVariable> targets, Map<TypeVariable, AnnotatedTypeMirror> inferredArgs) {
for (AnnotatedTypeVariable atv : methodType.getTypeVariables()) {
final TypeVariable typeVar = atv.getUnderlyingType();
if (targets.contains((TypeVariable) TypeAnnotationUtils.unannotatedType(typeVar))) {
final AnnotatedTypeMirror inferredType = inferredArgs.get(typeVar);
if (inferredType == null || TypeArgInferenceUtil.containsTypeParameter(inferredType, targets)) {
AnnotatedTypeMirror dummy = typeFactory.getUninferredWildcardType(atv);
inferredArgs.put(atv.getUnderlyingType(), dummy);
}
}
}
}
Aggregations