use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedUnionType in project checker-framework by typetools.
the class AsSuperVisitor method ensurePrimaryIsCorrectForUnionsAndIntersections.
/**
* The code in this class is assuming that the primary annotation of an {@link
* AnnotatedIntersectionType} is the greatest lower bound of the annotations on its direct super
* types and that the primary annotation of an {@link AnnotatedUnionType} is the least upper
* bound of its alternatives. This method makes this assumption true.
*/
private void ensurePrimaryIsCorrectForUnionsAndIntersections(AnnotatedTypeMirror type) {
if (type.getKind() == TypeKind.INTERSECTION) {
AnnotatedIntersectionType intersectionType = (AnnotatedIntersectionType) type;
Set<AnnotationMirror> glbs = null;
for (AnnotatedDeclaredType directST : intersectionType.directSuperTypes()) {
if (glbs == null) {
glbs = directST.getAnnotations();
} else {
Set<AnnotationMirror> newGlbs = AnnotationUtils.createAnnotationSet();
for (AnnotationMirror glb : glbs) {
AnnotationMirror anno = directST.getAnnotationInHierarchy(glb);
newGlbs.add(annotatedTypeFactory.getQualifierHierarchy().greatestLowerBound(anno, glb));
}
glbs = newGlbs;
}
}
type.replaceAnnotations(glbs);
} else if (type.getKind() == TypeKind.UNION) {
AnnotatedUnionType annotatedUnionType = (AnnotatedUnionType) type;
Set<AnnotationMirror> lubs = null;
for (AnnotatedDeclaredType altern : annotatedUnionType.getAlternatives()) {
if (lubs == null) {
lubs = altern.getAnnotations();
} else {
Set<AnnotationMirror> newLubs = AnnotationUtils.createAnnotationSet();
for (AnnotationMirror lub : lubs) {
AnnotationMirror anno = altern.getAnnotationInHierarchy(lub);
newLubs.add(annotatedTypeFactory.getQualifierHierarchy().leastUpperBound(anno, lub));
}
lubs = newLubs;
}
}
type.replaceAnnotations(lubs);
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedUnionType in project checker-framework by typetools.
the class BaseTypeVisitor method checkExceptionParameter.
// **********************************************************************
// Helper methods to provide a single overriding point
// **********************************************************************
/**
* Issue error if the exception parameter is not a supertype of the annotation specified by
* {@link #getExceptionParameterLowerBoundAnnotations()}, which is top by default.
*
* <p>Subclasses may override this method to change the behavior of this check. Subclasses
* wishing to enforce that exception parameter be annotated with other annotations can just
* override {@link #getExceptionParameterLowerBoundAnnotations()}.
*
* @param node CatchTree to check
*/
protected void checkExceptionParameter(CatchTree node) {
Set<? extends AnnotationMirror> requiredAnnotations = getExceptionParameterLowerBoundAnnotations();
AnnotatedTypeMirror exPar = atypeFactory.getAnnotatedType(node.getParameter());
for (AnnotationMirror required : requiredAnnotations) {
AnnotationMirror found = exPar.getAnnotationInHierarchy(required);
assert found != null;
if (!atypeFactory.getQualifierHierarchy().isSubtype(required, found)) {
checker.report(Result.failure("exception.parameter.invalid", found, required), node.getParameter());
}
if (exPar.getKind() == TypeKind.UNION) {
AnnotatedUnionType aut = (AnnotatedUnionType) exPar;
for (AnnotatedTypeMirror alterntive : aut.getAlternatives()) {
AnnotationMirror foundAltern = alterntive.getAnnotationInHierarchy(required);
if (!atypeFactory.getQualifierHierarchy().isSubtype(required, foundAltern)) {
checker.report(Result.failure("exception.parameter.invalid", foundAltern, required), node.getParameter());
}
}
}
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedUnionType in project checker-framework by typetools.
the class BaseTypeVisitor method checkThrownExpression.
/**
* Checks the type of the thrown expression.
*
* <p>By default, this method checks that the thrown expression is a subtype of top.
*
* <p>Issue error if the thrown expression is not a sub type of the annotation given by {@link
* #getThrowUpperBoundAnnotations()}, the same as {@link
* #getExceptionParameterLowerBoundAnnotations()} by default.
*
* <p>Subclasses may override this method to change the behavior of this check. Subclasses
* wishing to enforce that the thrown expression be a subtype of a type besides {@link
* #getExceptionParameterLowerBoundAnnotations}, should override {@link
* #getThrowUpperBoundAnnotations()}.
*
* @param node ThrowTree to check
*/
protected void checkThrownExpression(ThrowTree node) {
AnnotatedTypeMirror throwType = atypeFactory.getAnnotatedType(node.getExpression());
Set<? extends AnnotationMirror> required = getThrowUpperBoundAnnotations();
switch(throwType.getKind()) {
case NULL:
case DECLARED:
Set<AnnotationMirror> found = throwType.getAnnotations();
if (!atypeFactory.getQualifierHierarchy().isSubtype(found, required)) {
checker.report(Result.failure("throw.type.invalid", found, required), node.getExpression());
}
break;
case TYPEVAR:
case WILDCARD:
// TODO: this code might change after the type var changes.
Set<AnnotationMirror> foundEffective = throwType.getEffectiveAnnotations();
if (!atypeFactory.getQualifierHierarchy().isSubtype(foundEffective, required)) {
checker.report(Result.failure("throw.type.invalid", foundEffective, required), node.getExpression());
}
break;
case UNION:
AnnotatedUnionType unionType = (AnnotatedUnionType) throwType;
Set<AnnotationMirror> foundPrimary = unionType.getAnnotations();
if (!atypeFactory.getQualifierHierarchy().isSubtype(foundPrimary, required)) {
checker.report(Result.failure("throw.type.invalid", foundPrimary, required), node.getExpression());
}
for (AnnotatedTypeMirror altern : unionType.getAlternatives()) {
if (!atypeFactory.getQualifierHierarchy().isSubtype(altern.getAnnotations(), required)) {
checker.report(Result.failure("throw.type.invalid", altern.getAnnotations(), required), node.getExpression());
}
}
break;
default:
ErrorReporter.errorAbort("Unexpected throw expression type: " + throwType.getKind());
break;
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedUnionType in project checker-framework by typetools.
the class AnnotatedTypeCopier method visitUnion.
@Override
public AnnotatedTypeMirror visitUnion(AnnotatedUnionType original, IdentityHashMap<AnnotatedTypeMirror, AnnotatedTypeMirror> originalToCopy) {
if (originalToCopy.containsKey(original)) {
return originalToCopy.get(original);
}
final AnnotatedUnionType copy = (AnnotatedUnionType) AnnotatedTypeMirror.createType(original.getUnderlyingType(), original.atypeFactory, original.isDeclaration());
maybeCopyPrimaryAnnotations(original, copy);
originalToCopy.put(original, copy);
if (original.alternatives != null) {
final List<AnnotatedDeclaredType> copyAlternatives = new ArrayList<>();
for (final AnnotatedDeclaredType supertype : original.alternatives) {
copyAlternatives.add((AnnotatedDeclaredType) visit(supertype, originalToCopy));
}
copy.alternatives = Collections.unmodifiableList(copyAlternatives);
}
return copy;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedUnionType in project checker-framework by typetools.
the class AtmLubVisitor method visitUnion_Union.
@Override
public Void visitUnion_Union(AnnotatedUnionType type1, AnnotatedUnionType type2, AnnotatedTypeMirror lub) {
AnnotatedUnionType castedLub = castLub(type1, lub);
lubPrimaryAnnotations(type1, type2, lub);
for (int i = 0; i < castedLub.getAlternatives().size(); i++) {
AnnotatedDeclaredType lubAltern = castedLub.getAlternatives().get(i);
visit(type1.getAlternatives().get(i), type2.getAlternatives().get(i), lubAltern);
}
return null;
}
Aggregations