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.
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 JavaExpressionatConstructorInvocation(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 JavaExpressionatFieldAccess(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 JavaExpressionatFieldDecl(String expression, VariableElement fieldElement, SourceChecker checker) Parses a string to aJavaExpressionas if it were written atfieldElement.static JavaExpressionatLambdaParameter(String expression, LambdaExpressionTree lambdaTree, TreePath parentPath, SourceChecker checker) Parses a string as if it were written at one of the parameters oflambdaTree.static JavaExpressionatMethodBody(String expression, MethodTree methodTree, SourceChecker checker) Parses a string to aJavaExpressionas if it were written atmethodTree.static JavaExpressionatMethodDecl(String expression, ExecutableElement method, SourceChecker checker) Parses a string to aJavaExpressionas if it were written atmethod.static JavaExpressionatMethodInvocation(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 JavaExpressionatMethodInvocation(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 JavaExpressionatPath(String expression, TreePath localVarPath, SourceChecker checker) Parses a string as if it were written atlocalVarPath.static JavaExpressionatTypeDecl(String expression, TypeElement typeElement, SourceChecker checker) Parses a string to aJavaExpressionas if it were written attypeElement.toJavaExpression(String stringExpr) Convert a string to aJavaExpression.
-
Method Details
-
toJavaExpression
Convert a string to aJavaExpression. Returnsnullif no conversion exists.Conversion includes parsing
stringExprto aJavaExpressionand optionally transforming the result of parsing into anotherJavaExpression. An example of transformation is viewpoint adaptation.- Parameters:
stringExpr- a Java expression- Returns:
- a
JavaExpressionornullif no conversion fromstringExprexists - Throws:
JavaExpressionParseException- ifstringExprcannot be parsed to aJavaExpression
-
atTypeDecl
static JavaExpression atTypeDecl(String expression, TypeElement typeElement, SourceChecker checker) throws JavaExpressionParseException Parses a string to aJavaExpressionas if it were written attypeElement.- Parameters:
expression- a Java expression to parsetypeElement- type element at whichexpressionis parsedchecker- checker used to get theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atFieldDecl
static JavaExpression atFieldDecl(String expression, VariableElement fieldElement, SourceChecker checker) throws JavaExpressionParseException Parses a string to aJavaExpressionas if it were written atfieldElement.- Parameters:
expression- a Java expression to parsefieldElement- variable element at whichexpressionis parsedchecker- checker used to get theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atMethodDecl
static JavaExpression atMethodDecl(String expression, ExecutableElement method, SourceChecker checker) throws JavaExpressionParseException Parses a string to aJavaExpressionas if it were written atmethod. The returnedJavaExpressionusesFormalParameters to represent parameters. UseatMethodBody(String, MethodTree, SourceChecker)if parameters should beLocalVariables instead.- Parameters:
expression- a Java expression to parsemethod- method element at whichexpressionis parsedchecker- checker used to get theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atMethodBody
static JavaExpression atMethodBody(String expression, MethodTree methodTree, SourceChecker checker) throws JavaExpressionParseException Parses a string to aJavaExpressionas if it were written atmethodTree. The returnedJavaExpressionusesLocalVariables to represent parameters. UseatMethodDecl(String, ExecutableElement, SourceChecker)if parameters should beFormalParameters instead.- Parameters:
expression- a Java expression to parsemethodTree- method declaration tree at whichexpressionis parsedchecker- checker used to get theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atMethodInvocation
static JavaExpression atMethodInvocation(String expression, MethodInvocationTree methodInvocationTree, SourceChecker checker) throws 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 theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atMethodInvocation
static JavaExpression atMethodInvocation(String expression, MethodInvocationNode methodInvocationNode, SourceChecker checker) throws 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 theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atConstructorInvocation
static JavaExpression atConstructorInvocation(String expression, NewClassTree newClassTree, SourceChecker checker) throws 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 theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atFieldAccess
static JavaExpression atFieldAccess(String expression, MemberSelectTree fieldAccess, SourceChecker checker) throws 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 theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atLambdaParameter
static JavaExpression atLambdaParameter(String expression, LambdaExpressionTree lambdaTree, TreePath parentPath, SourceChecker checker) throws JavaExpressionParseException Parses a string as if it were written at one of the parameters oflambdaTree. Parameters of the lambda are expressed asLocalVariables.- 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 theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-
atPath
static JavaExpression atPath(String expression, TreePath localVarPath, SourceChecker checker) throws JavaExpressionParseException Parses a string as if it were written atlocalVarPath.If the leaf of
localVarPathis an artificial tree, one that the CFG builder created,expressionis parsed in the scope of the construct that the artificial tree was created for. An artificial tree is not in the compilation unit, so the scope at it cannot be computed; seeTreePathCacher.addArtificialPath(com.sun.source.tree.Tree, com.sun.source.util.TreePath).- Parameters:
expression- a Java expression to parselocalVarPath- location at whichexpressionis parsedchecker- checker used to get theProcessingEnvironmentand currentCompilationUnitTree- Returns:
- a
JavaExpressionforexpression - Throws:
JavaExpressionParseException- ifexpressioncannot be parsed
-