Search in sources :

Example 51 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class AnnotatedTypes method containsModifierImpl.

/*
     * For type variables we might hit the same type again. We keep a list of visited types.
     */
private static boolean containsModifierImpl(AnnotatedTypeMirror type, AnnotationMirror modifier, List<AnnotatedTypeMirror> visited) {
    boolean found = type.hasAnnotation(modifier);
    boolean vis = visited.contains(type);
    visited.add(type);
    if (!found && !vis) {
        if (type.getKind() == TypeKind.DECLARED) {
            AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) type;
            for (AnnotatedTypeMirror typeMirror : declaredType.getTypeArguments()) {
                found |= containsModifierImpl(typeMirror, modifier, visited);
                if (found) {
                    break;
                }
            }
        } else if (type.getKind() == TypeKind.ARRAY) {
            AnnotatedArrayType arrayType = (AnnotatedArrayType) type;
            found = containsModifierImpl(arrayType.getComponentType(), modifier, visited);
        } else if (type.getKind() == TypeKind.TYPEVAR) {
            AnnotatedTypeVariable atv = (AnnotatedTypeVariable) type;
            if (atv.getUpperBound() != null) {
                found = containsModifierImpl(atv.getUpperBound(), modifier, visited);
            }
            if (!found && atv.getLowerBound() != null) {
                found = containsModifierImpl(atv.getLowerBound(), modifier, visited);
            }
        } else if (type.getKind() == TypeKind.WILDCARD) {
            AnnotatedWildcardType awc = (AnnotatedWildcardType) type;
            if (awc.getExtendsBound() != null) {
                found = containsModifierImpl(awc.getExtendsBound(), modifier, visited);
            }
            if (!found && awc.getSuperBound() != null) {
                found = containsModifierImpl(awc.getSuperBound(), modifier, visited);
            }
        }
    }
    return found;
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 52 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class AnnotatedTypes method implementsAnnotation.

/**
 * @return true if atm is an Annotation interface, i.e. an implementation of
 *     java.lang.annotation.Annotation e.g. @interface MyAnno - implementsAnnotation would
 *     return true when called on an AnnotatedDeclaredType representing a use of MyAnno
 */
public static boolean implementsAnnotation(final AnnotatedTypeMirror atm) {
    if (atm.getKind() != TypeKind.DECLARED) {
        return false;
    }
    final AnnotatedTypeMirror.AnnotatedDeclaredType declaredType = (AnnotatedTypeMirror.AnnotatedDeclaredType) atm;
    Symbol.ClassSymbol classSymbol = (Symbol.ClassSymbol) declaredType.getUnderlyingType().asElement();
    for (final Type iface : classSymbol.getInterfaces()) {
        if (TypesUtils.isDeclaredOfName(iface, annotationClassName)) {
            return true;
        }
    }
    return false;
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedIntersectionType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedIntersectionType) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) Type(com.sun.tools.javac.code.Type) ElementType(java.lang.annotation.ElementType) Symbol(com.sun.tools.javac.code.Symbol) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 53 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class AnnotatedTypes method getIteratedType.

/**
 * Returns the iterated type of the passed iterable type, and throws {@link
 * IllegalArgumentException} if the passed type is not iterable.
 *
 * <p>The iterated type is the component type of an array, and the type argument of {@link
 * Iterable} for declared types.
 *
 * @param iterableType the iterable type (either array or declared)
 * @return the types of elements in the iterable type
 */
public static AnnotatedTypeMirror getIteratedType(ProcessingEnvironment processingEnv, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror iterableType) {
    if (iterableType.getKind() == TypeKind.ARRAY) {
        return ((AnnotatedArrayType) iterableType).getComponentType();
    }
    // For type variables and wildcards take the effective upper bound.
    if (iterableType.getKind() == TypeKind.WILDCARD) {
        return getIteratedType(processingEnv, atypeFactory, ((AnnotatedWildcardType) iterableType).getExtendsBound().deepCopy());
    }
    if (iterableType.getKind() == TypeKind.TYPEVAR) {
        return getIteratedType(processingEnv, atypeFactory, ((AnnotatedTypeVariable) iterableType).getUpperBound());
    }
    if (iterableType.getKind() != TypeKind.DECLARED) {
        ErrorReporter.errorAbort("AnnotatedTypes.getIteratedType: not iterable type: " + iterableType);
        // dead code
        return null;
    }
    TypeElement iterableElement = processingEnv.getElementUtils().getTypeElement("java.lang.Iterable");
    AnnotatedDeclaredType iterableElmType = atypeFactory.getAnnotatedType(iterableElement);
    AnnotatedDeclaredType dt = asSuper(atypeFactory, iterableType, iterableElmType);
    if (dt.getTypeArguments().isEmpty()) {
        TypeElement e = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
        AnnotatedDeclaredType t = atypeFactory.getAnnotatedType(e);
        return t;
    } else {
        return dt.getTypeArguments().get(0);
    }
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) TypeElement(javax.lang.model.element.TypeElement) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)

Example 54 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class AtmLubVisitor method visitDeclared_Declared.

@Override
public Void visitDeclared_Declared(AnnotatedDeclaredType type1, AnnotatedDeclaredType type2, AnnotatedTypeMirror lub) {
    AnnotatedDeclaredType castedLub = castLub(type1, lub);
    lubPrimaryAnnotations(type1, type2, lub);
    List<AnnotatedTypeMirror> lubTypArgs = new ArrayList<>();
    for (int i = 0; i < type1.getTypeArguments().size(); i++) {
        AnnotatedTypeMirror type1TypeArg = type1.getTypeArguments().get(i);
        AnnotatedTypeMirror type2TypeArg = type2.getTypeArguments().get(i);
        AnnotatedTypeMirror lubTypeArg = castedLub.getTypeArguments().get(i);
        lubTypeArgument(type1TypeArg, type2TypeArg, lubTypeArg);
    }
    if (lubTypArgs.size() > 0) {
        castedLub.setTypeArguments(lubTypArgs);
    }
    return null;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 55 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType 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;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedUnionType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedUnionType)

Aggregations

AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)72 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)26 ArrayList (java.util.ArrayList)19 ExpressionTree (com.sun.source.tree.ExpressionTree)18 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)18 MethodTree (com.sun.source.tree.MethodTree)17 Tree (com.sun.source.tree.Tree)16 ClassTree (com.sun.source.tree.ClassTree)14 VariableTree (com.sun.source.tree.VariableTree)14 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)13 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)12 NewClassTree (com.sun.source.tree.NewClassTree)12 ExecutableElement (javax.lang.model.element.ExecutableElement)11 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)10 NewArrayTree (com.sun.source.tree.NewArrayTree)9 ReturnTree (com.sun.source.tree.ReturnTree)9 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)9 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)9 AssignmentTree (com.sun.source.tree.AssignmentTree)8