Search in sources :

Example 1 with ArrayCreation

use of org.checkerframework.dataflow.expression.ArrayCreation in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method getMinLenFromString.

/**
 * Returns the minimum length of an array expression or 0 if the min length is unknown.
 *
 * @param sequenceExpression Java expression
 * @param tree expression tree or variable declaration
 * @param currentPath path to local scope
 * @return min length of sequenceExpression or 0
 */
public int getMinLenFromString(String sequenceExpression, Tree tree, TreePath currentPath) {
    AnnotationMirror lengthAnno;
    JavaExpression expressionObj;
    try {
        expressionObj = parseJavaExpressionString(sequenceExpression, currentPath);
    } catch (JavaExpressionParseException e) {
        // ignore parse errors and return 0.
        return 0;
    }
    if (expressionObj instanceof ValueLiteral) {
        ValueLiteral sequenceLiteral = (ValueLiteral) expressionObj;
        Object sequenceLiteralValue = sequenceLiteral.getValue();
        if (sequenceLiteralValue instanceof String) {
            return ((String) sequenceLiteralValue).length();
        }
    } else if (expressionObj instanceof ArrayCreation) {
        ArrayCreation arrayCreation = (ArrayCreation) expressionObj;
        // This is only expected to support array creations in varargs methods
        return arrayCreation.getInitializers().size();
    } else if (expressionObj instanceof ArrayAccess) {
        List<? extends AnnotationMirror> annoList = expressionObj.getType().getAnnotationMirrors();
        for (AnnotationMirror anno : annoList) {
            String ANNO_NAME = AnnotationUtils.annotationName(anno);
            if (ANNO_NAME.equals(MINLEN_NAME)) {
                return getMinLenValue(canonicalAnnotation(anno));
            } else if (ANNO_NAME.equals(ARRAYLEN_NAME) || ANNO_NAME.equals(ARRAYLENRANGE_NAME)) {
                return getMinLenValue(anno);
            }
        }
    }
    lengthAnno = getAnnotationFromJavaExpression(expressionObj, tree, ArrayLenRange.class);
    if (lengthAnno == null) {
        lengthAnno = getAnnotationFromJavaExpression(expressionObj, tree, ArrayLen.class);
    }
    if (lengthAnno == null) {
        lengthAnno = getAnnotationFromJavaExpression(expressionObj, tree, StringVal.class);
    }
    if (lengthAnno == null) {
        // Could not find a more precise type, so return 0;
        return 0;
    }
    return getMinLenValue(lengthAnno);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayAccess(org.checkerframework.dataflow.expression.ArrayAccess) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ArrayLen(org.checkerframework.common.value.qual.ArrayLen) JavaExpressionParseException(org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException) ArrayCreation(org.checkerframework.dataflow.expression.ArrayCreation) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) ValueLiteral(org.checkerframework.dataflow.expression.ValueLiteral) StringVal(org.checkerframework.common.value.qual.StringVal)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 ArrayLen (org.checkerframework.common.value.qual.ArrayLen)1 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)1 StringVal (org.checkerframework.common.value.qual.StringVal)1 ArrayAccess (org.checkerframework.dataflow.expression.ArrayAccess)1 ArrayCreation (org.checkerframework.dataflow.expression.ArrayCreation)1 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)1 ValueLiteral (org.checkerframework.dataflow.expression.ValueLiteral)1 JavaExpressionParseException (org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException)1