Search in sources :

Example 11 with JavaExpression

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

the class OptionalVisitor method sameExpression.

/**
 * Returns true if the two trees represent the same expression.
 *
 * @param tree1 the first tree
 * @param tree2 the second tree
 * @return true if the two trees represent the same expression
 */
private boolean sameExpression(ExpressionTree tree1, ExpressionTree tree2) {
    JavaExpression r1 = JavaExpression.fromTree(tree1);
    JavaExpression r2 = JavaExpression.fromTree(tree1);
    if (r1 != null && !r1.containsUnknown() && r2 != null && !r2.containsUnknown()) {
        return r1.equals(r2);
    } else {
        return tree1.toString().equals(tree2.toString());
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression)

Example 12 with JavaExpression

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

the class DependentTypesHelper method buildAnnotation.

/**
 * Create a new annotation of the same type as {@code originalAnno} using the provided {@code
 * elementMap}.
 *
 * @param originalAnno the annotation passed to {@link
 *     #convertAnnotationMirror(StringToJavaExpression, AnnotationMirror)} (this method is a
 *     helper method for {@link #convertAnnotationMirror(StringToJavaExpression,
 *     AnnotationMirror)})
 * @param elementMap a mapping from element of {@code originalAnno} to {@code JavaExpression}s
 * @return an annotation created from {@code elementMap}
 */
protected AnnotationMirror buildAnnotation(AnnotationMirror originalAnno, Map<ExecutableElement, List<JavaExpression>> elementMap) {
    AnnotationBuilder builder = new AnnotationBuilder(factory.getProcessingEnv(), AnnotationUtils.annotationName(originalAnno));
    builder.copyElementValuesFromAnnotation(originalAnno, elementMap.keySet());
    for (Map.Entry<ExecutableElement, List<JavaExpression>> entry : elementMap.entrySet()) {
        List<String> strings = CollectionsPlume.mapList(JavaExpression::toString, entry.getValue());
        builder.setValue(entry.getKey(), strings);
    }
    return builder.build();
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) ExecutableElement(javax.lang.model.element.ExecutableElement) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with JavaExpression

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

the class JavaExpressionParseUtil method parse.

/**
 * Parses a string to a {@link JavaExpression}.
 *
 * <p>For most uses, clients should call one of the static methods in {@link
 * StringToJavaExpression} rather than calling this method directly.
 *
 * @param expression the string expression to parse
 * @param enclosingType type of the class that encloses the JavaExpression
 * @param thisReference the JavaExpression to which to parse "this", or null if "this" should not
 *     appear in the expression
 * @param parameters list of JavaExpressions to which to parse formal parameter references such as
 *     "#2", or null if formal parameter references should not appear in the expression
 * @param localVarPath if non-null, the expression is parsed as if it were written at this
 *     location; affects only parsing of local variables
 * @param pathToCompilationUnit required to use the underlying Javac API
 * @param env the processing environment
 * @return {@code expression} as a {@code JavaExpression}
 * @throws JavaExpressionParseException if the string cannot be parsed
 */
public static JavaExpression parse(String expression, TypeMirror enclosingType, @Nullable ThisReference thisReference, @Nullable List<FormalParameter> parameters, @Nullable TreePath localVarPath, TreePath pathToCompilationUnit, ProcessingEnvironment env) throws JavaExpressionParseException {
    // Use the current source version to parse with because a JavaExpression could refer to a
    // variable named "var", which is a keyword in Java 10 and later.
    LanguageLevel currentSourceVersion = JavaParserUtil.getCurrentSourceVersion(env);
    String expressionWithParameterNames = StringsPlume.replaceAll(expression, FORMAL_PARAMETER, PARAMETER_REPLACEMENT);
    Expression expr;
    try {
        expr = JavaParserUtil.parseExpression(expressionWithParameterNames, currentSourceVersion);
    } catch (ParseProblemException e) {
        String extra = ".";
        if (!e.getProblems().isEmpty()) {
            String message = e.getProblems().get(0).getMessage();
            int newLine = message.indexOf(System.lineSeparator());
            if (newLine != -1) {
                message = message.substring(0, newLine);
            }
            extra = ". Error message: " + message;
        }
        throw constructJavaExpressionParseError(expression, "the expression did not parse" + extra);
    }
    JavaExpression result = ExpressionToJavaExpressionVisitor.convert(expr, enclosingType, thisReference, parameters, localVarPath, pathToCompilationUnit, env);
    if (result instanceof ClassName && !expression.endsWith(".class")) {
        throw constructJavaExpressionParseError(expression, String.format("a class name cannot terminate a Java expression string, where result=%s [%s]", result, result.getClass()));
    }
    return result;
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) LanguageLevel(com.github.javaparser.ParserConfiguration.LanguageLevel) Expression(com.github.javaparser.ast.expr.Expression) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ClassName(org.checkerframework.dataflow.expression.ClassName) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 14 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 methodInvocationNode method invocation node
 * @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, MethodInvocationNode methodInvocationNode, SourceChecker checker) throws JavaExpressionParseException {
    ExecutableElement ee = TreeUtils.elementFromUse(methodInvocationNode.getTree());
    JavaExpression javaExpr = StringToJavaExpression.atMethodDecl(expression, ee, checker);
    return javaExpr.atMethodInvocation(methodInvocationNode);
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ViewpointAdaptJavaExpression(org.checkerframework.dataflow.expression.ViewpointAdaptJavaExpression) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 15 with JavaExpression

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

the class StringToJavaExpression method atFieldAccess.

/**
 * uf found Parses a string as if it were written at the declaration of the field and then
 * viewpoint-adapts the result to the use.
 *
 * @param expression a Java expression to parse
 * @param fieldAccess the field access 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 atFieldAccess(String expression, MemberSelectTree fieldAccess, SourceChecker checker) throws JavaExpressionParseException {
    Element ele = TreeUtils.elementFromUse(fieldAccess);
    if (ele.getKind() != ElementKind.FIELD && ele.getKind() != ElementKind.ENUM_CONSTANT) {
        throw new BugInCF("Expected a field, but found %s for %s", ele.getKind(), fieldAccess);
    }
    VariableElement fieldEle = (VariableElement) ele;
    JavaExpression receiver = JavaExpression.fromTree(fieldAccess.getExpression());
    JavaExpression javaExpr = StringToJavaExpression.atFieldDecl(expression, fieldEle, checker);
    return javaExpr.atFieldAccess(receiver);
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ViewpointAdaptJavaExpression(org.checkerframework.dataflow.expression.ViewpointAdaptJavaExpression) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) BugInCF(org.checkerframework.javacutil.BugInCF)

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