Search in sources :

Example 16 with AnnotatedArrayType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType 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 17 with AnnotatedArrayType

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

the class AnnotatedTypes method expandVarArgsFromTypes.

public static List<AnnotatedTypeMirror> expandVarArgsFromTypes(AnnotatedExecutableType method, List<AnnotatedTypeMirror> args) {
    List<AnnotatedTypeMirror> parameters = method.getParameterTypes();
    if (!method.getElement().isVarArgs()) {
        return parameters;
    }
    AnnotatedArrayType varargs = (AnnotatedArrayType) parameters.get(parameters.size() - 1);
    if (parameters.size() == args.size()) {
        // Check if one sent an element or an array
        AnnotatedTypeMirror lastArg = args.get(args.size() - 1);
        if (lastArg.getKind() == TypeKind.ARRAY && (getArrayDepth(varargs) == getArrayDepth((AnnotatedArrayType) lastArg) || // substituted for an array.
        varargs.getComponentType().getKind() == TypeKind.TYPEVAR)) {
            return parameters;
        }
    }
    parameters = new ArrayList<>(parameters.subList(0, parameters.size() - 1));
    for (int i = args.size() - parameters.size(); i > 0; --i) {
        parameters.add(varargs.getComponentType());
    }
    return parameters;
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 18 with AnnotatedArrayType

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

the class UpperBoundVisitor method relaxedCommonAssignment.

/**
 * Returns whether the assignment is legal based on the relaxed assignment rules.
 *
 * <p>The relaxed assignment rules is the following: Assuming the varType (left-hand side) is
 * less than the length of some array given some offset
 *
 * <p>1. If both the offset and the value expression (rhs) are ints known at compile time, and
 * if the min length of the array is greater than offset + value, then the assignment is legal.
 * (This method returns true.)
 *
 * <p>2. If the value expression (rhs) is less than the length of an array that is the same
 * length as the array in the varType, and if the offsets are equal, then the assignment is
 * legal. (This method returns true.)
 *
 * <p>3. Otherwise the assignment is only legal if the usual assignment rules are true, so this
 * method returns false.
 *
 * <p>If the varType is less than the length of multiple arrays, then the this method only
 * returns true if the relaxed rules above apply for each array.
 *
 * <p>If the varType is an array type and the value express is an array initializer, then the
 * above rules are applied for expression in the initializer where the varType is the component
 * type of the array.
 */
private boolean relaxedCommonAssignment(AnnotatedTypeMirror varType, ExpressionTree valueExp) {
    List<? extends ExpressionTree> expressions;
    if (valueExp.getKind() == Kind.NEW_ARRAY && varType.getKind() == TypeKind.ARRAY) {
        expressions = ((NewArrayTree) valueExp).getInitializers();
        if (expressions == null || expressions.isEmpty()) {
            return false;
        }
        // The qualifier we need for an array is in the component type, not varType.
        AnnotatedTypeMirror componentType = ((AnnotatedArrayType) varType).getComponentType();
        UBQualifier qualifier = UBQualifier.createUBQualifier(componentType, atypeFactory.UNKNOWN);
        if (!qualifier.isLessThanLengthQualifier()) {
            return false;
        }
        for (ExpressionTree expressionTree : expressions) {
            if (!relaxedCommonAssignmentCheck((LessThanLengthOf) qualifier, expressionTree)) {
                return false;
            }
        }
        return true;
    }
    UBQualifier qualifier = UBQualifier.createUBQualifier(varType, atypeFactory.UNKNOWN);
    return qualifier.isLessThanLengthQualifier() && relaxedCommonAssignmentCheck((LessThanLengthOf) qualifier, valueExp);
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) LessThanLengthOf(org.checkerframework.checker.index.upperbound.UBQualifier.LessThanLengthOf) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 19 with AnnotatedArrayType

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

the class NullnessVisitor method visitNewArray.

@Override
public Void visitNewArray(NewArrayTree node, Void p) {
    AnnotatedArrayType type = atypeFactory.getAnnotatedType(node);
    AnnotatedTypeMirror componentType = type.getComponentType();
    if (componentType.hasEffectiveAnnotation(NONNULL) && !isNewArrayAllZeroDims(node) && !isNewArrayInToArray(node) && !TypesUtils.isPrimitive(componentType.getUnderlyingType()) && checker.getLintOption("forbidnonnullarraycomponents", false)) {
        checker.report(Result.failure("new.array.type.invalid", componentType.getAnnotations(), type.toString()), node);
    }
    return super.visitNewArray(node, p);
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 20 with AnnotatedArrayType

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

the class AliasingVisitor method visitVariable.

@Override
public Void visitVariable(VariableTree node, Void p) {
    // Component types are not allowed to have the @Unique annotation.
    AnnotatedTypeMirror varType = atypeFactory.getAnnotatedType(node);
    VariableElement elt = TreeUtils.elementFromDeclaration(node);
    if (elt.getKind().isField() && varType.hasExplicitAnnotation(Unique.class)) {
        checker.report(Result.failure("unique.location.forbidden"), node);
    } else if (node.getType().getKind() == Kind.ARRAY_TYPE) {
        AnnotatedArrayType arrayType = (AnnotatedArrayType) varType;
        if (arrayType.getComponentType().hasAnnotation(Unique.class)) {
            checker.report(Result.failure("unique.location.forbidden"), node);
        }
    } else if (node.getType().getKind() == Kind.PARAMETERIZED_TYPE) {
        AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) varType;
        for (AnnotatedTypeMirror atm : declaredType.getTypeArguments()) {
            if (atm.hasAnnotation(Unique.class)) {
                checker.report(Result.failure("unique.location.forbidden"), node);
            }
        }
    }
    return super.visitVariable(node, p);
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) Unique(org.checkerframework.common.aliasing.qual.Unique) VariableElement(javax.lang.model.element.VariableElement) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)23 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)15 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)6 ExpressionTree (com.sun.source.tree.ExpressionTree)4 NewArrayTree (com.sun.source.tree.NewArrayTree)4 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)4 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)3 Tree (com.sun.source.tree.Tree)3 TypeCastTree (com.sun.source.tree.TypeCastTree)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AnnotationMirror (javax.lang.model.element.AnnotationMirror)3 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)3 AnnotationTree (com.sun.source.tree.AnnotationTree)2 AssignmentTree (com.sun.source.tree.AssignmentTree)2 ClassTree (com.sun.source.tree.ClassTree)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)2 IdentifierTree (com.sun.source.tree.IdentifierTree)2