use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.
the class DefaultInferredTypesApplier method removePrimaryAnnotationTypeVar.
private void removePrimaryAnnotationTypeVar(AnnotatedTypeVariable annotatedTypeVariable, TypeMirror inferredTypeMirror, AnnotationMirror top, AnnotationMirror previousAnnotation) {
if (inferredTypeMirror.getKind() != TypeKind.TYPEVAR) {
throw new BugInCF("Missing annos");
}
TypeVariable typeVar = (TypeVariable) inferredTypeMirror;
AnnotatedTypeVariable typeVariableDecl = (AnnotatedTypeVariable) factory.getAnnotatedType(typeVar.asElement());
AnnotationMirror upperBound = typeVariableDecl.getEffectiveAnnotationInHierarchy(top);
if (omitSubtypingCheck || hierarchy.isSubtype(upperBound, previousAnnotation)) {
annotatedTypeVariable.removeAnnotationInHierarchy(top);
AnnotationMirror ub = typeVariableDecl.getUpperBound().getAnnotationInHierarchy(top);
apply(annotatedTypeVariable.getUpperBound(), ub, typeVar.getUpperBound(), top);
AnnotationMirror lb = typeVariableDecl.getLowerBound().getAnnotationInHierarchy(top);
apply(annotatedTypeVariable.getLowerBound(), lb, typeVar.getLowerBound(), top);
}
}
use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.
the class TryCatchFrame method possibleLabels.
/**
* Given a type of thrown exception, add the set of possible control flow successor {@link Label}s
* to the argument set. Return true if the exception is known to be caught by one of those labels
* and false if it may propagate still further.
*/
@Override
public boolean possibleLabels(TypeMirror thrown, Set<Label> labels) {
while (!(thrown instanceof DeclaredType)) {
assert thrown instanceof TypeVariable : "thrown type must be a variable or a declared type";
thrown = ((TypeVariable) thrown).getUpperBound();
}
DeclaredType declaredThrown = (DeclaredType) thrown;
assert thrown != null : "thrown type must be bounded by a declared type";
for (Pair<TypeMirror, Label> pair : catchLabels) {
TypeMirror caught = pair.first;
boolean canApply = false;
if (caught.getKind() == TypeKind.DECLARED) {
DeclaredType declaredCaught = (DeclaredType) caught;
if (types.isSubtype(declaredThrown, declaredCaught)) {
// No later catch blocks can apply.
labels.add(pair.second);
return true;
} else if (types.isSubtype(declaredCaught, declaredThrown)) {
canApply = true;
}
} else {
assert caught.getKind() == TypeKind.UNION : "caught type must be a union or a declared type";
UnionType caughtUnion = (UnionType) caught;
for (TypeMirror alternative : caughtUnion.getAlternatives()) {
assert alternative.getKind() == TypeKind.DECLARED : "alternatives of an caught union type must be declared types";
DeclaredType declaredAlt = (DeclaredType) alternative;
if (types.isSubtype(declaredThrown, declaredAlt)) {
// No later catch blocks can apply.
labels.add(pair.second);
return true;
} else if (types.isSubtype(declaredAlt, declaredThrown)) {
canApply = true;
}
}
}
if (canApply) {
labels.add(pair.second);
}
}
return false;
}
use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.
the class TypesUtils method order.
/**
* Returns the list of type variables such that a type variable in the list only references type
* variables at a lower index than itself.
*
* @param collection a collection of type variables
* @param types type utilities
* @return the type variables ordered so that each type variable only references earlier type
* variables
*/
public static List<TypeVariable> order(Collection<TypeVariable> collection, Types types) {
List<TypeVariable> list = new ArrayList<>(collection);
List<TypeVariable> ordered = new ArrayList<>();
while (!list.isEmpty()) {
TypeVariable free = doesNotContainOthers(list, types);
list.remove(free);
ordered.add(free);
}
return ordered;
}
use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.
the class BaseTypeValidator method visitParameterizedType.
/**
* Checks that the annotations on the type arguments supplied to a type or a method invocation are
* within the bounds of the type variables as declared, and issues the "type.argument" error if
* they are not.
*
* @param type the type to check
* @param tree the type's tree
*/
protected Void visitParameterizedType(AnnotatedDeclaredType type, ParameterizedTypeTree tree) {
if (TreeUtils.isDiamondTree(tree)) {
return null;
}
final TypeElement element = (TypeElement) type.getUnderlyingType().asElement();
if (checker.shouldSkipUses(element)) {
return null;
}
AnnotatedDeclaredType capturedType = (AnnotatedDeclaredType) atypeFactory.applyCaptureConversion(type);
List<AnnotatedTypeParameterBounds> bounds = atypeFactory.typeVariablesFromUse(capturedType, element);
visitor.checkTypeArguments(tree, bounds, capturedType.getTypeArguments(), tree.getTypeArguments(), element.getSimpleName(), element.getTypeParameters());
@SuppressWarnings(// applyCaptureConversion returns the passed type if type does not
"interning:not.interned") boolean // have wildcards.
hasCapturedTypeVariables = capturedType != type;
if (hasCapturedTypeVariables) {
// Check that the extends bound of the captured type variable is a subtype of the extends
// bound of the wildcard.
int numTypeArgs = capturedType.getTypeArguments().size();
// First create a mapping from captured type variable to its wildcard.
Map<TypeVariable, AnnotatedTypeMirror> typeVarToWildcard = new HashMap<>(numTypeArgs);
for (int i = 0; i < numTypeArgs; i++) {
AnnotatedTypeMirror captureTypeArg = capturedType.getTypeArguments().get(i);
if (TypesUtils.isCapturedTypeVariable(captureTypeArg.getUnderlyingType()) && type.getTypeArguments().get(i).getKind() == TypeKind.WILDCARD) {
AnnotatedTypeVariable capturedTypeVar = (AnnotatedTypeVariable) captureTypeArg;
AnnotatedWildcardType wildcard = (AnnotatedWildcardType) type.getTypeArguments().get(i);
typeVarToWildcard.put(capturedTypeVar.getUnderlyingType(), wildcard);
}
}
for (int i = 0; i < numTypeArgs; i++) {
AnnotatedTypeMirror captureTypeArg = capturedType.getTypeArguments().get(i);
if (type.getTypeArguments().get(i).getKind() == TypeKind.WILDCARD) {
AnnotatedWildcardType wildcard = (AnnotatedWildcardType) type.getTypeArguments().get(i);
if (TypesUtils.isCapturedTypeVariable(captureTypeArg.getUnderlyingType())) {
AnnotatedTypeVariable capturedTypeVar = (AnnotatedTypeVariable) captureTypeArg;
// Substitute the captured type variables with their wildcards. Without this, the
// isSubtype check crashes because wildcards aren't comparable with type variables.
AnnotatedTypeMirror catpureTypeVarUB = atypeFactory.getTypeVarSubstitutor().substituteWithoutCopyingTypeArguments(typeVarToWildcard, capturedTypeVar.getUpperBound());
if (!atypeFactory.getTypeHierarchy().isSubtype(catpureTypeVarUB, wildcard.getExtendsBound())) {
checker.reportError(tree.getTypeArguments().get(i), "type.argument", element.getTypeParameters().get(i), element.getSimpleName(), wildcard.getExtendsBound(), capturedTypeVar.getUpperBound());
}
} else if (AnnotatedTypes.isExplicitlySuperBounded(wildcard)) {
// bound.
if (!(atypeFactory.getQualifierHierarchy().isSubtype(wildcard.getSuperBound().getEffectiveAnnotations(), wildcard.getExtendsBound().getAnnotations()) && atypeFactory.getQualifierHierarchy().isSubtype(wildcard.getExtendsBound().getAnnotations(), wildcard.getSuperBound().getEffectiveAnnotations()))) {
checker.reportError(tree.getTypeArguments().get(i), "super.wildcard", wildcard.getExtendsBound(), wildcard.getSuperBound());
}
}
}
}
}
return null;
}
use of javax.lang.model.type.TypeVariable in project buck by facebook.
the class StandaloneTypeVariableTest method testGetKind.
@Test
public void testGetKind() throws IOException {
compile("class Foo<T> { }");
TypeVariable tVar = (TypeVariable) elements.getTypeElement("Foo").getTypeParameters().get(0).asType();
assertSame(TypeKind.TYPEVAR, tVar.getKind());
}
Aggregations