Search in sources :

Example 1 with ValueLiteral

use of org.checkerframework.dataflow.expression.ValueLiteral 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)

Example 2 with ValueLiteral

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

the class JavaExpressionOptimizer method visitMethodCall.

@Override
protected JavaExpression visitMethodCall(MethodCall methodCallExpr, Void unused) {
    JavaExpression optReceiver = convert(methodCallExpr.getReceiver());
    List<JavaExpression> optArguments = convert(methodCallExpr.getArguments());
    // Length of string literal: convert it to an integer literal.
    if (methodCallExpr.getElement().getSimpleName().contentEquals("length") && optReceiver instanceof ValueLiteral) {
        Object value = ((ValueLiteral) optReceiver).getValue();
        if (value instanceof String) {
            return new ValueLiteral(factory.types.getPrimitiveType(TypeKind.INT), ((String) value).length());
        }
    }
    return new MethodCall(methodCallExpr.getType(), methodCallExpr.getElement(), optReceiver, optArguments);
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ValueLiteral(org.checkerframework.dataflow.expression.ValueLiteral) MethodCall(org.checkerframework.dataflow.expression.MethodCall)

Example 3 with ValueLiteral

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

the class UpperBoundVisitor method checkEffectivelyFinalAndParsable.

/**
 * Reports an error if the Java expression named by s is not effectively final when parsed at the
 * declaration of the given class.
 *
 * @param s a Java expression
 * @param classTree the expression is parsed with respect to this class
 * @param whereToReportError the tree at which to possibly report an error
 */
private void checkEffectivelyFinalAndParsable(String s, ClassTree classTree, Tree whereToReportError) {
    JavaExpression je;
    try {
        je = StringToJavaExpression.atTypeDecl(s, TreeUtils.elementFromDeclaration(classTree), checker);
    } catch (JavaExpressionParseException e) {
        checker.report(whereToReportError, e.getDiagMessage());
        return;
    }
    Element element = null;
    if (je instanceof LocalVariable) {
        element = ((LocalVariable) je).getElement();
    } else if (je instanceof FieldAccess) {
        element = ((FieldAccess) je).getField();
    } else if (je instanceof ThisReference || je instanceof ValueLiteral) {
        return;
    }
    if (element == null || !ElementUtils.isEffectivelyFinal(element)) {
        checker.reportError(whereToReportError, NOT_FINAL, je);
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) Element(javax.lang.model.element.Element) JavaExpressionParseException(org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException) LocalVariable(org.checkerframework.dataflow.expression.LocalVariable) ValueLiteral(org.checkerframework.dataflow.expression.ValueLiteral) FieldAccess(org.checkerframework.dataflow.expression.FieldAccess) ThisReference(org.checkerframework.dataflow.expression.ThisReference)

Example 4 with ValueLiteral

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

the class JavaExpressionOptimizer method visitLocalVariable.

@Override
protected JavaExpression visitLocalVariable(LocalVariable localVarExpr, Void unused) {
    if (factory instanceof ValueAnnotatedTypeFactory) {
        Element element = localVarExpr.getElement();
        Long exactValue = ValueCheckerUtils.getExactValue(element, (ValueAnnotatedTypeFactory) factory);
        if (exactValue != null) {
            return new ValueLiteral(localVarExpr.getType(), exactValue.intValue());
        }
    }
    return super.visitLocalVariable(localVarExpr, unused);
}
Also used : Element(javax.lang.model.element.Element) ValueLiteral(org.checkerframework.dataflow.expression.ValueLiteral)

Aggregations

ValueLiteral (org.checkerframework.dataflow.expression.ValueLiteral)4 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)3 Element (javax.lang.model.element.Element)2 JavaExpressionParseException (org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException)2 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 FieldAccess (org.checkerframework.dataflow.expression.FieldAccess)1 LocalVariable (org.checkerframework.dataflow.expression.LocalVariable)1 MethodCall (org.checkerframework.dataflow.expression.MethodCall)1 ThisReference (org.checkerframework.dataflow.expression.ThisReference)1 StringToJavaExpression (org.checkerframework.framework.util.StringToJavaExpression)1