Search in sources :

Example 56 with JavaExpression

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

the class StringToJavaExpression method atPath.

/**
 * Parses a string as if it were written at {@code localVarPath}.
 *
 * @param expression a Java expression to parse
 * @param localVarPath location at which {@code expression} is parsed
 * @param checker checker used to get the {@link
 *     javax.annotation.processing.ProcessingEnvironment} and current {@link
 *     com.sun.source.tree.CompilationUnitTree}
 * @return a {@code JavaExpression} for {@code expression}
 * @throws JavaExpressionParseException if {@code expression} cannot be parsed
 */
static JavaExpression atPath(String expression, TreePath localVarPath, SourceChecker checker) throws JavaExpressionParseException {
    TypeMirror enclosingType = TreeUtils.typeOf(TreePathUtil.enclosingClass(localVarPath));
    ThisReference thisReference = TreePathUtil.isTreeInStaticScope(localVarPath) ? null : new ThisReference(enclosingType);
    MethodTree methodTree = TreePathUtil.enclosingMethod(localVarPath);
    if (methodTree == null) {
        return JavaExpressionParseUtil.parse(expression, enclosingType, thisReference, null, localVarPath, checker.getPathToCompilationUnit(), checker.getProcessingEnvironment());
    }
    ExecutableElement methodEle = TreeUtils.elementFromDeclaration(methodTree);
    List<FormalParameter> parameters = JavaExpression.getFormalParameters(methodEle);
    JavaExpression javaExpr = JavaExpressionParseUtil.parse(expression, enclosingType, thisReference, parameters, localVarPath, checker.getPathToCompilationUnit(), checker.getProcessingEnvironment());
    List<JavaExpression> paramsAsLocals = JavaExpression.getParametersAsLocalVariables(methodEle);
    return ViewpointAdaptJavaExpression.viewpointAdapt(javaExpr, paramsAsLocals);
}
Also used : FormalParameter(org.checkerframework.dataflow.expression.FormalParameter) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ViewpointAdaptJavaExpression(org.checkerframework.dataflow.expression.ViewpointAdaptJavaExpression) TypeMirror(javax.lang.model.type.TypeMirror) MethodTree(com.sun.source.tree.MethodTree) ExecutableElement(javax.lang.model.element.ExecutableElement) ThisReference(org.checkerframework.dataflow.expression.ThisReference)

Example 57 with JavaExpression

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

the class StringToJavaExpression method atConstructorInvocation.

/**
 * Parses a string as if it were written at the declaration of the invoked constructor and then
 * viewpoint-adapts the result to the call site.
 *
 * @param expression a Java expression to parse
 * @param newClassTree constructor invocation
 * @param checker checker used to get the {@link
 *     javax.annotation.processing.ProcessingEnvironment} and current {@link
 *     com.sun.source.tree.CompilationUnitTree}
 * @return a {@code JavaExpression} for {@code expression}
 * @throws JavaExpressionParseException if {@code expression} cannot be parsed
 */
static JavaExpression atConstructorInvocation(String expression, NewClassTree newClassTree, SourceChecker checker) throws JavaExpressionParseException {
    ExecutableElement ee = TreeUtils.elementFromUse(newClassTree);
    JavaExpression javaExpr = StringToJavaExpression.atMethodDecl(expression, ee, checker);
    return javaExpr.atConstructorInvocation(newClassTree);
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ViewpointAdaptJavaExpression(org.checkerframework.dataflow.expression.ViewpointAdaptJavaExpression) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 58 with JavaExpression

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

the class StringToJavaExpression method atMethodInvocation.

/**
 * Parses a string as if it were written at the declaration of the invoked method and then
 * viewpoint-adapts the result to the call site.
 *
 * @param expression a Java expression to parse
 * @param methodInvocationTree method invocation tree
 * @param checker checker used to get the {@link
 *     javax.annotation.processing.ProcessingEnvironment} and current {@link
 *     com.sun.source.tree.CompilationUnitTree}
 * @return a {@code JavaExpression} for {@code expression}
 * @throws JavaExpressionParseException if {@code expression} cannot be parsed
 */
static JavaExpression atMethodInvocation(String expression, MethodInvocationTree methodInvocationTree, SourceChecker checker) throws JavaExpressionParseException {
    ExecutableElement ee = TreeUtils.elementFromUse(methodInvocationTree);
    JavaExpression javaExpr = StringToJavaExpression.atMethodDecl(expression, ee, checker);
    return javaExpr.atMethodInvocation(methodInvocationTree);
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ViewpointAdaptJavaExpression(org.checkerframework.dataflow.expression.ViewpointAdaptJavaExpression) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 59 with JavaExpression

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

the class LowerBoundTransfer method notEqualToValue.

/**
 * Refines GTEN1 to NN if it is not equal to -1, and NN to Pos if it is not equal to 0. Implements
 * case 7.
 *
 * @param mLiteral a potential literal
 * @param otherNode the node on the other side of the ==/!=
 * @param otherAnno the annotation of the other side of the ==/!=
 */
private void notEqualToValue(Node mLiteral, Node otherNode, AnnotationMirror otherAnno, CFStore store) {
    Long integerLiteral = ValueCheckerUtils.getExactValue(mLiteral.getTree(), aTypeFactory.getValueAnnotatedTypeFactory());
    if (integerLiteral == null) {
        return;
    }
    long intLiteral = integerLiteral.longValue();
    if (intLiteral == 0) {
        if (aTypeFactory.areSameByClass(otherAnno, NonNegative.class)) {
            List<Node> internals = splitAssignments(otherNode);
            for (Node internal : internals) {
                JavaExpression je = JavaExpression.fromNode(internal);
                store.insertValue(je, POS);
            }
        }
    } else if (intLiteral == -1) {
        if (aTypeFactory.areSameByClass(otherAnno, GTENegativeOne.class)) {
            List<Node> internals = splitAssignments(otherNode);
            for (Node internal : internals) {
                JavaExpression je = JavaExpression.fromNode(internal);
                store.insertValue(je, NN);
            }
        }
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) BinaryOperationNode(org.checkerframework.dataflow.cfg.node.BinaryOperationNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) Node(org.checkerframework.dataflow.cfg.node.Node) GTENegativeOne(org.checkerframework.checker.index.qual.GTENegativeOne) List(java.util.List)

Example 60 with JavaExpression

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

the class LowerBoundTransfer method addInformationFromPreconditions.

/**
 * Adds a default NonNegative annotation to every character. Implements case 33.
 */
@Override
protected void addInformationFromPreconditions(CFStore info, AnnotatedTypeFactory factory, UnderlyingAST.CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
    super.addInformationFromPreconditions(info, factory, method, methodTree, methodElement);
    List<? extends VariableTree> paramTrees = methodTree.getParameters();
    for (VariableTree variableTree : paramTrees) {
        if (TreeUtils.typeOf(variableTree).getKind() == TypeKind.CHAR) {
            JavaExpression je = JavaExpression.fromVariableTree(variableTree);
            info.insertValuePermitNondeterministic(je, aTypeFactory.NN);
        }
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) VariableTree(com.sun.source.tree.VariableTree)

Aggregations

JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)87 AnnotationMirror (javax.lang.model.element.AnnotationMirror)39 Node (org.checkerframework.dataflow.cfg.node.Node)23 StringToJavaExpression (org.checkerframework.framework.util.StringToJavaExpression)21 CFValue (org.checkerframework.framework.flow.CFValue)20 ExecutableElement (javax.lang.model.element.ExecutableElement)19 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)19 ArrayList (java.util.ArrayList)15 CFStore (org.checkerframework.framework.flow.CFStore)15 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)13 JavaExpressionParseException (org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException)13 Tree (com.sun.source.tree.Tree)12 List (java.util.List)11 FieldAccess (org.checkerframework.dataflow.expression.FieldAccess)11 MethodTree (com.sun.source.tree.MethodTree)10 TreePath (com.sun.source.util.TreePath)10 HashMap (java.util.HashMap)10 Map (java.util.Map)9 Element (javax.lang.model.element.Element)9 VariableElement (javax.lang.model.element.VariableElement)9