use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class PropagationTypeAnnotator method getTypeParamFromEnclosingClass.
/**
* Search parent's type arguments for wildcard. Using the index of wildcard, find the
* corresponding type parameter element and return it. Returns null if the wildcard is the
* result of substitution and therefore not in the list of type arguments.
*/
private Element getTypeParamFromEnclosingClass(final AnnotatedWildcardType wildcard, final AnnotatedDeclaredType parent) {
Integer wildcardIndex = null;
int currentIndex = 0;
for (AnnotatedTypeMirror typeArg : parent.getTypeArguments()) {
// which they should have been replaced by capture
if (typeArg == wildcard) {
wildcardIndex = currentIndex;
break;
}
currentIndex += 1;
}
if (wildcardIndex != null) {
final TypeElement typeElement = (TypeElement) typeFactory.getProcessingEnv().getTypeUtils().asElement(parent.getUnderlyingType());
return typeElement.getTypeParameters().get(wildcardIndex);
}
return null;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class EquivalentAtmComboScanner method scan.
protected RETURN_TYPE scan(Iterable<? extends AnnotatedTypeMirror> types1, Iterable<? extends AnnotatedTypeMirror> types2, PARAM param) {
RETURN_TYPE r = null;
boolean first = true;
Iterator<? extends AnnotatedTypeMirror> tIter1 = types1.iterator();
Iterator<? extends AnnotatedTypeMirror> tIter2 = types2.iterator();
while (tIter1.hasNext() && tIter2.hasNext()) {
final AnnotatedTypeMirror type1 = tIter1.next();
final AnnotatedTypeMirror type2 = tIter2.next();
r = first ? scan(type1, type2, param) : scanAndReduce(type1, type2, param, r);
}
return r;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class AnnotatedTypes method asOuterSuper.
/**
* Return the base type of type or any of its outer types that starts with the given type. If
* none exists, return null.
*
* @param type a type
* @param superType a type
*/
private static AnnotatedTypeMirror asOuterSuper(Types types, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror type, AnnotatedTypeMirror superType) {
if (type.getKind() == TypeKind.DECLARED) {
AnnotatedDeclaredType dt = (AnnotatedDeclaredType) type;
AnnotatedDeclaredType enclosingType = dt;
TypeMirror superTypeMirror = types.erasure(superType.getUnderlyingType());
while (enclosingType != null) {
TypeMirror enclosingTypeMirror = types.erasure(enclosingType.getUnderlyingType());
if (types.isSubtype(enclosingTypeMirror, superTypeMirror)) {
dt = enclosingType;
break;
}
enclosingType = enclosingType.getEnclosingType();
}
if (enclosingType == null) {
// checker.message(Kind.WARNING, msg);
return superType;
}
return asSuper(atypeFactory, dt, superType);
}
return asSuper(atypeFactory, type, superType);
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class AnnotatedTypes method addTypeVarMappings.
private static void addTypeVarMappings(Types types, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror t, TypeElement enclosingClassOfElem, Map<TypeVariable, AnnotatedTypeMirror> mappings) {
if (enclosingClassOfElem.getTypeParameters().isEmpty()) {
return;
}
AnnotatedDeclaredType enclosingType = atypeFactory.getAnnotatedType(enclosingClassOfElem);
AnnotatedDeclaredType base = (AnnotatedDeclaredType) asOuterSuper(types, atypeFactory, t, enclosingType);
final List<AnnotatedTypeVariable> ownerParams = new ArrayList<>(enclosingType.getTypeArguments().size());
for (final AnnotatedTypeMirror typeParam : enclosingType.getTypeArguments()) {
if (typeParam.getKind() != TypeKind.TYPEVAR) {
ErrorReporter.errorAbort("Type arguments of a declaration should be type variables\n" + "enclosingClassOfElem=" + enclosingClassOfElem + "\n" + "enclosingType=" + enclosingType + "\n" + "typeMirror=" + t);
}
ownerParams.add((AnnotatedTypeVariable) typeParam);
}
List<AnnotatedTypeMirror> baseParams = base.getTypeArguments();
if (ownerParams.size() != baseParams.size() && !base.wasRaw()) {
ErrorReporter.errorAbort("Unexpected number of parameters.\n" + "enclosingType=" + enclosingType + "\n" + "baseType=" + base);
}
if (!ownerParams.isEmpty() && baseParams.isEmpty() && base.wasRaw()) {
List<AnnotatedTypeMirror> newBaseParams = new ArrayList<>();
for (AnnotatedTypeVariable arg : ownerParams) {
// If base type was raw and the type arguments are missing,
// set them to the erased type of the type variable.
// (which is the erased type of the upper bound.)
newBaseParams.add(arg.getErased());
}
baseParams = newBaseParams;
}
for (int i = 0; i < ownerParams.size(); ++i) {
mappings.put(ownerParams.get(i).getUnderlyingType(), baseParams.get(i));
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class AnnotatedTypes method findEffectiveAnnotations.
/**
* When comparing types against the bounds of a type variable, we may encounter other type
* variables, wildcards, and intersections in those bounds. This method traverses the bounds
* until it finds a concrete type from which it can pull an annotation. This occurs for every
* hierarchy in QualifierHierarchy
*
* @return the set of effective annotation mirrors in all hierarchies
*/
public static Set<AnnotationMirror> findEffectiveAnnotations(final QualifierHierarchy qualifierHierarchy, final AnnotatedTypeMirror toSearch) {
AnnotatedTypeMirror source = toSearch;
TypeKind kind = source.getKind();
while (kind == TypeKind.TYPEVAR || kind == TypeKind.WILDCARD || kind == TypeKind.INTERSECTION) {
switch(source.getKind()) {
case TYPEVAR:
source = ((AnnotatedTypeVariable) source).getUpperBound();
break;
case WILDCARD:
source = ((AnnotatedWildcardType) source).getExtendsBound();
break;
case INTERSECTION:
// if there are multiple conflicting annotations, choose the lowest
final Set<AnnotationMirror> glb = glbOfBounds((AnnotatedIntersectionType) source, qualifierHierarchy);
return glb;
default:
ErrorReporter.errorAbort("Unexpected AnnotatedTypeMirror with no primary annotation!" + "toSearch=" + toSearch + "source=" + source);
}
kind = source.getKind();
}
return source.getAnnotations();
}
Aggregations