Search in sources :

Example 1 with UnionType

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.");
    }
}
Also used : UnionType(javax.lang.model.type.UnionType) TypeMirror(javax.lang.model.type.TypeMirror) UnknownTypeException(javax.lang.model.type.UnknownTypeException)

Example 2 with UnionType

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;
}
Also used : UnionType(javax.lang.model.type.UnionType) TypeVariable(javax.lang.model.type.TypeVariable) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 3 with UnionType

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);
            }
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) UnionType(javax.lang.model.type.UnionType) WildcardType(javax.lang.model.type.WildcardType) TypeVariable(javax.lang.model.type.TypeVariable) TypeMirror(javax.lang.model.type.TypeMirror) StringJoiner(java.util.StringJoiner) DeclaredType(javax.lang.model.type.DeclaredType)

Example 4 with UnionType

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.");
    }
}
Also used : UnionType(javax.lang.model.type.UnionType) TypeMirror(javax.lang.model.type.TypeMirror) UnknownTypeException(javax.lang.model.type.UnknownTypeException)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)4 UnionType (javax.lang.model.type.UnionType)4 DeclaredType (javax.lang.model.type.DeclaredType)2 TypeVariable (javax.lang.model.type.TypeVariable)2 UnknownTypeException (javax.lang.model.type.UnknownTypeException)2 StringJoiner (java.util.StringJoiner)1 ArrayType (javax.lang.model.type.ArrayType)1 WildcardType (javax.lang.model.type.WildcardType)1