public class JavaExpressionParseUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
JavaExpressionParseUtil.JavaExpressionContext
Context used to parse and viewpoint-adapt a Java expression.
|
static class |
JavaExpressionParseUtil.JavaExpressionParseException
An exception that indicates a parse error.
|
Modifier and Type | Field and Description |
---|---|
protected static Pattern |
ANCHORED_PARAMETER_PATTERN
Anchored pattern for a formal parameter use.
|
protected static String |
PARAMETER_REGEX
Regular expression for a formal parameter use.
|
protected static Pattern |
UNANCHORED_PARAMETER_PATTERN
Unanchored; can be used to find all formal parameter uses.
|
Constructor and Description |
---|
JavaExpressionParseUtil() |
Modifier and Type | Method and Description |
---|---|
static int |
parameterIndex(String s)
Returns the 1-based index of the formal parameter that occurs in
s or -1 if no formal
parameter occurs. |
static List<Integer> |
parameterIndices(String s)
Returns a list of 1-based indices of all formal parameters that occur in
s . |
static JavaExpression |
parse(String expression,
JavaExpressionParseUtil.JavaExpressionContext context)
Parse a string and viewpoint-adapt it to the given
context . |
static JavaExpression |
parse(String expression,
JavaExpressionParseUtil.JavaExpressionContext context,
@Nullable TreePath localPath)
Parse a string with respect to
localPath and viewpoint-adapt it to the given context . |
protected static final String PARAMETER_REGEX
protected static final Pattern ANCHORED_PARAMETER_PATTERN
protected static final Pattern UNANCHORED_PARAMETER_PATTERN
public static JavaExpression parse(String expression, JavaExpressionParseUtil.JavaExpressionContext context) throws JavaExpressionParseUtil.JavaExpressionParseException
context
. Return its representation
as a JavaExpression
, or throw a JavaExpressionParseUtil.JavaExpressionParseException
.expression
- a Java expression to parsecontext
- information about any receiver and arguments; also has a reference to the
checkerJavaExpressionParseUtil.JavaExpressionParseException
- if the string cannot be parsedpublic static JavaExpression parse(String expression, JavaExpressionParseUtil.JavaExpressionContext context, @Nullable TreePath localPath) throws JavaExpressionParseUtil.JavaExpressionParseException
localPath
and viewpoint-adapt it to the given context
. Return its representation as a JavaExpression
, or throw a JavaExpressionParseUtil.JavaExpressionParseException
.
If localPath
is non-null, then identifiers are parsed as if the expression was
written at the location of localPath
. This means identifiers will be parsed to local
variables in scope at localPath
when possible. If localPath
is null, then no
identifier can be parsed to a local variable. In either case, the parameter syntax, e.g. #1,
is always parsed to the arguments in context
. This is because a parameter of a lambda
can refer both to local variables in scope at its declaration and to a parameter of the
lambda.
expression
- a Java expression to parsecontext
- information about any receiver and arguments; also has a reference to the
checkerlocalPath
- if non-null, the expression is parsed as if it were written at this locationJavaExpressionParseUtil.JavaExpressionParseException
- if the string cannot be parsedpublic static List<Integer> parameterIndices(String s)
s
. Each
formal parameter occurs in s as a string like "#1" or "#4". This routine does not do proper
parsing; for instance, if "#2" appears within a string in s, then 2 is in the result list.
The result may contain duplicates.s
- a Java expressions
public static int parameterIndex(String s)
s
or -1 if no formal
parameter occurs.s
- a Java expressions
or -1