Search in sources :

Example 1 with ArrayLenRange

use of org.checkerframework.common.value.qual.ArrayLenRange in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method createArrayLengthResultAnnotation.

/**
 * Returns a constant value annotation for a length of an array or string type with a constant
 * value annotation
 */
AnnotationMirror createArrayLengthResultAnnotation(AnnotatedTypeMirror receiverType) {
    AnnotationMirror arrayAnno = receiverType.getAnnotationInHierarchy(UNKNOWNVAL);
    if (AnnotationUtils.areSameByClass(arrayAnno, ArrayLen.class)) {
        // array.length, where array : @ArrayLen(x)
        List<Integer> lengths = ValueAnnotatedTypeFactory.getArrayLength(arrayAnno);
        return createNumberAnnotationMirror(new ArrayList<>(lengths));
    }
    // Check for an ArrayLenRange annotation.
    if (AnnotationUtils.areSameByClass(arrayAnno, ArrayLenRange.class)) {
        // array.length, where array : @ArrayLenRange(x)
        Range range = getRange(arrayAnno);
        return createIntRangeAnnotation(range);
    }
    if (AnnotationUtils.areSameByClass(arrayAnno, StringVal.class)) {
        List<String> strings = ValueAnnotatedTypeFactory.getStringValues(arrayAnno);
        List<Integer> lengths = ValueCheckerUtils.getLengthsForStringValues(strings);
        return createNumberAnnotationMirror(new ArrayList<>(lengths));
    }
    return createIntRangeAnnotation(0, Integer.MAX_VALUE);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) IntRange(org.checkerframework.common.value.qual.IntRange) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) Range(org.checkerframework.common.value.util.Range)

Example 2 with ArrayLenRange

use of org.checkerframework.common.value.qual.ArrayLenRange in project checker-framework by typetools.

the class ValueVisitor method validateType.

/**
 * Overridden to issue errors at the appropriate place if an {@code IntRange} or {@code
 * ArrayLenRange} annotation has {@code from > to}. {@code from > to} either indicates a user
 * error when writing an annotation or an error in the checker's implementation, as {@code from}
 * should always be {@code <= to}.
 */
@Override
public boolean validateType(Tree tree, AnnotatedTypeMirror type) {
    boolean result = super.validateType(tree, type);
    if (!result) {
        AnnotationMirror anno = type.getAnnotationInHierarchy(atypeFactory.UNKNOWNVAL);
        if (AnnotationUtils.areSameByClass(anno, IntRange.class)) {
            long to = atypeFactory.getToValueFromIntRange(type);
            long from = atypeFactory.getFromValueFromIntRange(type);
            if (from > to) {
                checker.report(Result.failure("from.greater.than.to"), tree);
                return false;
            }
        } else if (AnnotationUtils.areSameByClass(anno, ArrayLenRange.class)) {
            int from = AnnotationUtils.getElementValue(anno, "from", Integer.class, true);
            int to = AnnotationUtils.getElementValue(anno, "to", Integer.class, true);
            if (from > to) {
                checker.report(Result.failure("from.greater.than.to"), tree);
                return false;
            }
        }
    }
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)2 IntRange (org.checkerframework.common.value.qual.IntRange)1 Range (org.checkerframework.common.value.util.Range)1