use of com.github.javaparser.ParserConfiguration.LanguageLevel 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;
}
Aggregations