Interface StringToJavaExpression
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This interface is both a functional interface, see
toJavaExpression(String)
, and also a
collection of static methods that convert a string to a JavaExpression at common locations.
Some conversion routines merely do parsing. Other conversion routines parse and then transform
the result of parsing into another JavaExpression
; for all the static methods, this
transformation is viewpoint-adaptation.
To parse a string "at a location" means to parse it as if it were written in an annotation that is written on that location.
-
Method Summary
Modifier and TypeMethodDescriptionstatic JavaExpression
atConstructorInvocation
(String expression, NewClassTree newClassTree, SourceChecker checker) 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.static JavaExpression
atFieldAccess
(String expression, MemberSelectTree fieldAccess, SourceChecker checker) 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.static JavaExpression
atFieldDecl
(String expression, VariableElement fieldElement, SourceChecker checker) Parses a string to aJavaExpression
as if it were written atfieldElement
.static JavaExpression
atLambdaParameter
(String expression, LambdaExpressionTree lambdaTree, TreePath parentPath, SourceChecker checker) Parses a string as if it were written at one of the parameters oflambdaTree
.static JavaExpression
atMethodBody
(String expression, MethodTree methodTree, SourceChecker checker) Parses a string to aJavaExpression
as if it were written atmethodTree
.static JavaExpression
atMethodDecl
(String expression, ExecutableElement method, SourceChecker checker) Parses a string to aJavaExpression
as if it were written atmethod
.static JavaExpression
atMethodInvocation
(String expression, MethodInvocationTree methodInvocationTree, SourceChecker checker) 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.static JavaExpression
atMethodInvocation
(String expression, MethodInvocationNode methodInvocationNode, SourceChecker checker) 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.static JavaExpression
atPath
(String expression, TreePath localVarPath, SourceChecker checker) Parses a string as if it were written atlocalVarPath
.static JavaExpression
atTypeDecl
(String expression, TypeElement typeElement, SourceChecker checker) Parses a string to aJavaExpression
as if it were written attypeElement
.toJavaExpression
(String stringExpr) Convert a string to aJavaExpression
.
-
Method Details
-
toJavaExpression
@Nullable JavaExpression toJavaExpression(String stringExpr) throws JavaExpressionParseUtil.JavaExpressionParseException Convert a string to aJavaExpression
. Returnsnull
if no conversion exists.Conversion includes parsing
stringExpr
to aJavaExpression
and optionally transforming the result of parsing into anotherJavaExpression
. An example of transformation is viewpoint adaptation.- Parameters:
stringExpr
- a Java expression- Returns:
- a
JavaExpression
ornull
if no conversion fromstringExpr
exists - Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifstringExpr
cannot be parsed to aJavaExpression
-
atTypeDecl
static JavaExpression atTypeDecl(String expression, TypeElement typeElement, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException Parses a string to aJavaExpression
as if it were written attypeElement
.- Parameters:
expression
- a Java expression to parsetypeElement
- type element at whichexpression
is parsedchecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atFieldDecl
static JavaExpression atFieldDecl(String expression, VariableElement fieldElement, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException Parses a string to aJavaExpression
as if it were written atfieldElement
.- Parameters:
expression
- a Java expression to parsefieldElement
- variable element at whichexpression
is parsedchecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atMethodDecl
static JavaExpression atMethodDecl(String expression, ExecutableElement method, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException Parses a string to aJavaExpression
as if it were written atmethod
. The returnedJavaExpression
usesFormalParameter
s to represent parameters. UseatMethodBody(String, MethodTree, SourceChecker)
if parameters should beLocalVariable
s instead.- Parameters:
expression
- a Java expression to parsemethod
- method element at whichexpression
is parsedchecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atMethodBody
static JavaExpression atMethodBody(String expression, MethodTree methodTree, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException Parses a string to aJavaExpression
as if it were written atmethodTree
. The returnedJavaExpression
usesLocalVariable
s to represent parameters. UseatMethodDecl(String, ExecutableElement, SourceChecker)
if parameters should beFormalParameter
s instead.- Parameters:
expression
- a Java expression to parsemethodTree
- method declaration tree at whichexpression
is parsedchecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atMethodInvocation
static JavaExpression atMethodInvocation(String expression, MethodInvocationTree methodInvocationTree, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException 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.- Parameters:
expression
- a Java expression to parsemethodInvocationTree
- method invocation treechecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atMethodInvocation
static JavaExpression atMethodInvocation(String expression, MethodInvocationNode methodInvocationNode, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException 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.- Parameters:
expression
- a Java expression to parsemethodInvocationNode
- method invocation nodechecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atConstructorInvocation
static JavaExpression atConstructorInvocation(String expression, NewClassTree newClassTree, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException 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.- Parameters:
expression
- a Java expression to parsenewClassTree
- constructor invocationchecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atFieldAccess
static JavaExpression atFieldAccess(String expression, MemberSelectTree fieldAccess, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException 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.- Parameters:
expression
- a Java expression to parsefieldAccess
- the field access treechecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atLambdaParameter
static JavaExpression atLambdaParameter(String expression, LambdaExpressionTree lambdaTree, TreePath parentPath, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException Parses a string as if it were written at one of the parameters oflambdaTree
. Parameters of the lambda are expressed asLocalVariable
s.- Parameters:
expression
- a Java expression to parselambdaTree
- the lambda treeparentPath
- path to the parent oflambdaTree
; required because the expression can reference final local variables of the enclosing methodchecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-
atPath
static JavaExpression atPath(String expression, TreePath localVarPath, SourceChecker checker) throws JavaExpressionParseUtil.JavaExpressionParseException Parses a string as if it were written atlocalVarPath
.- Parameters:
expression
- a Java expression to parselocalVarPath
- location at whichexpression
is parsedchecker
- checker used to get theProcessingEnvironment
and currentCompilationUnitTree
- Returns:
- a
JavaExpression
forexpression
- Throws:
JavaExpressionParseUtil.JavaExpressionParseException
- ifexpression
cannot be parsed
-