use of javax.lang.model.type.UnionType in project ceylon-compiler by ceylon.
the class ModelChecker method validateUnionTypeInfo.
private void validateUnionTypeInfo(Element ex) {
UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class);
assertTrue(ut != null, "UnionType annotation must be present");
TypeMirror expectedUnionType = ex.asType();
assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected");
try {
new SimpleTypeVisitor6<Void, Void>() {
}.visit(expectedUnionType);
throw new RuntimeException("Expected UnknownTypeException not thrown.");
} catch (UnknownTypeException ute) {
// Expected
;
}
UnionType unionType = new SimpleTypeVisitor7<UnionType, Void>() {
@Override
protected UnionType defaultAction(TypeMirror e, Void p) {
return null;
}
@Override
public UnionType visitUnion(UnionType t, Void p) {
return t;
}
}.visit(expectedUnionType);
assertTrue(unionType != null, "Must get a non-null union type.");
assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match");
String[] typeNames = ut.value();
for (int i = 0; i < typeNames.length; i++) {
TypeMirror typeFromAnnotation = nameToType(typeNames[i]);
assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)), "Types were not equal.");
}
}
use of javax.lang.model.type.UnionType 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.UnionType in project checker-framework by typetools.
the class TypesUtils method simpleTypeName.
/**
* Returns the simple type name, without annotations.
*
* @param type a type
* @return the simple type name, without annotations
*/
public static String simpleTypeName(TypeMirror type) {
switch(type.getKind()) {
case ARRAY:
return simpleTypeName(((ArrayType) type).getComponentType()) + "[]";
case TYPEVAR:
return ((TypeVariable) type).asElement().getSimpleName().toString();
case DECLARED:
return ((DeclaredType) type).asElement().getSimpleName().toString();
case NULL:
return "<nulltype>";
case VOID:
return "void";
case WILDCARD:
WildcardType wildcard = (WildcardType) type;
TypeMirror extendsBound = wildcard.getExtendsBound();
TypeMirror superBound = wildcard.getSuperBound();
return "?" + (extendsBound != null ? " extends " + simpleTypeName(extendsBound) : "") + (superBound != null ? " super " + simpleTypeName(superBound) : "");
case UNION:
StringJoiner sj = new StringJoiner(" | ");
for (TypeMirror alternative : ((UnionType) type).getAlternatives()) {
sj.add(simpleTypeName(alternative));
}
return sj.toString();
default:
if (type.getKind().isPrimitive()) {
return TypeAnnotationUtils.unannotatedType(type).toString();
} else {
throw new BugInCF("simpleTypeName: unhandled type kind: %s, type: %s", type.getKind(), type);
}
}
}
use of javax.lang.model.type.UnionType in project ceylon by eclipse.
the class ModelChecker method validateUnionTypeInfo.
private void validateUnionTypeInfo(Element ex) {
UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class);
assertTrue(ut != null, "UnionType annotation must be present");
TypeMirror expectedUnionType = ex.asType();
assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected");
try {
new SimpleTypeVisitor6<Void, Void>() {
}.visit(expectedUnionType);
throw new RuntimeException("Expected UnknownTypeException not thrown.");
} catch (UnknownTypeException ute) {
// Expected
;
}
UnionType unionType = new SimpleTypeVisitor7<UnionType, Void>() {
@Override
protected UnionType defaultAction(TypeMirror e, Void p) {
return null;
}
@Override
public UnionType visitUnion(UnionType t, Void p) {
return t;
}
}.visit(expectedUnionType);
assertTrue(unionType != null, "Must get a non-null union type.");
assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match");
String[] typeNames = ut.value();
for (int i = 0; i < typeNames.length; i++) {
TypeMirror typeFromAnnotation = nameToType(typeNames[i]);
assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)), "Types were not equal.");
}
}
Aggregations