use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class OptionalVisitor method sameExpression.
/**
* Returns true if the two trees represent the same expression.
*
* @param tree1 the first tree
* @param tree2 the second tree
* @return true if the two trees represent the same expression
*/
private boolean sameExpression(ExpressionTree tree1, ExpressionTree tree2) {
JavaExpression r1 = JavaExpression.fromTree(tree1);
JavaExpression r2 = JavaExpression.fromTree(tree1);
if (r1 != null && !r1.containsUnknown() && r2 != null && !r2.containsUnknown()) {
return r1.equals(r2);
} else {
return tree1.toString().equals(tree2.toString());
}
}
use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class DependentTypesHelper method buildAnnotation.
/**
* Create a new annotation of the same type as {@code originalAnno} using the provided {@code
* elementMap}.
*
* @param originalAnno the annotation passed to {@link
* #convertAnnotationMirror(StringToJavaExpression, AnnotationMirror)} (this method is a
* helper method for {@link #convertAnnotationMirror(StringToJavaExpression,
* AnnotationMirror)})
* @param elementMap a mapping from element of {@code originalAnno} to {@code JavaExpression}s
* @return an annotation created from {@code elementMap}
*/
protected AnnotationMirror buildAnnotation(AnnotationMirror originalAnno, Map<ExecutableElement, List<JavaExpression>> elementMap) {
AnnotationBuilder builder = new AnnotationBuilder(factory.getProcessingEnv(), AnnotationUtils.annotationName(originalAnno));
builder.copyElementValuesFromAnnotation(originalAnno, elementMap.keySet());
for (Map.Entry<ExecutableElement, List<JavaExpression>> entry : elementMap.entrySet()) {
List<String> strings = CollectionsPlume.mapList(JavaExpression::toString, entry.getValue());
builder.setValue(entry.getKey(), strings);
}
return builder.build();
}
use of org.checkerframework.dataflow.expression.JavaExpression 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;
}
use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class StringToJavaExpression method atMethodInvocation.
/**
* 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.
*
* @param expression a Java expression to parse
* @param methodInvocationNode method invocation node
* @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 atMethodInvocation(String expression, MethodInvocationNode methodInvocationNode, SourceChecker checker) throws JavaExpressionParseException {
ExecutableElement ee = TreeUtils.elementFromUse(methodInvocationNode.getTree());
JavaExpression javaExpr = StringToJavaExpression.atMethodDecl(expression, ee, checker);
return javaExpr.atMethodInvocation(methodInvocationNode);
}
use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class StringToJavaExpression method atFieldAccess.
/**
* 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.
*
* @param expression a Java expression to parse
* @param fieldAccess the field access tree
* @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 atFieldAccess(String expression, MemberSelectTree fieldAccess, SourceChecker checker) throws JavaExpressionParseException {
Element ele = TreeUtils.elementFromUse(fieldAccess);
if (ele.getKind() != ElementKind.FIELD && ele.getKind() != ElementKind.ENUM_CONSTANT) {
throw new BugInCF("Expected a field, but found %s for %s", ele.getKind(), fieldAccess);
}
VariableElement fieldEle = (VariableElement) ele;
JavaExpression receiver = JavaExpression.fromTree(fieldAccess.getExpression());
JavaExpression javaExpr = StringToJavaExpression.atFieldDecl(expression, fieldEle, checker);
return javaExpr.atFieldAccess(receiver);
}
Aggregations