Search in sources :

Example 11 with ThisReference

use of org.checkerframework.dataflow.expression.ThisReference 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 12 with ThisReference

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

the class StringToJavaExpression method atMethodDecl.

/**
 * Parses a string to a {@link JavaExpression} as if it were written at {@code method}. The
 * returned {@code JavaExpression} uses {@link FormalParameter}s to represent parameters. Use
 * {@link #atMethodBody(String, MethodTree, SourceChecker)} if parameters should be {@link
 * LocalVariable}s instead.
 *
 * @param expression a Java expression to parse
 * @param method method element 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 atMethodDecl(String expression, ExecutableElement method, SourceChecker checker) throws JavaExpressionParseException {
    TypeMirror enclosingType = ElementUtils.enclosingTypeElement(method).asType();
    ThisReference thisReference;
    if (ElementUtils.isStatic(method)) {
        // Can't use "this" on a static method
        thisReference = null;
    } else {
        thisReference = new ThisReference(enclosingType);
    }
    List<FormalParameter> parameters = JavaExpression.getFormalParameters(method);
    return JavaExpressionParseUtil.parse(expression, enclosingType, thisReference, parameters, null, checker.getPathToCompilationUnit(), checker.getProcessingEnvironment());
}
Also used : FormalParameter(org.checkerframework.dataflow.expression.FormalParameter) TypeMirror(javax.lang.model.type.TypeMirror) ThisReference(org.checkerframework.dataflow.expression.ThisReference)

Aggregations

ThisReference (org.checkerframework.dataflow.expression.ThisReference)12 FieldAccess (org.checkerframework.dataflow.expression.FieldAccess)7 LocalVariable (org.checkerframework.dataflow.expression.LocalVariable)7 ClassName (org.checkerframework.dataflow.expression.ClassName)6 FormalParameter (org.checkerframework.dataflow.expression.FormalParameter)5 TypeMirror (javax.lang.model.type.TypeMirror)4 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)4 MethodCall (org.checkerframework.dataflow.expression.MethodCall)3 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 VariableElement (javax.lang.model.element.VariableElement)2 ArrayAccess (org.checkerframework.dataflow.expression.ArrayAccess)2 ViewpointAdaptJavaExpression (org.checkerframework.dataflow.expression.ViewpointAdaptJavaExpression)2 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 MethodTree (com.sun.source.tree.MethodTree)1 VariableTree (com.sun.source.tree.VariableTree)1 ArrayList (java.util.ArrayList)1 TypeElement (javax.lang.model.element.TypeElement)1 ValueLiteral (org.checkerframework.dataflow.expression.ValueLiteral)1