use of org.checkerframework.framework.util.StringToJavaExpression 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.framework.util.StringToJavaExpression in project checker-framework by typetools.
the class DependentTypesHelper method atExpression.
/**
* Standardize the Java expressions in annotations in written in the {@code expressionTree}. Also,
* converts the parameter syntax, e.g. "#1", to the parameter name.
*
* <p>{@code expressionTree} must be an expressions which can contain explicitly written
* annotations, namely a {@link NewClassTree}, {@link com.sun.source.tree.NewArrayTree}, or {@link
* com.sun.source.tree.TypeCastTree}. For example, this method standardizes the {@code KeyFor}
* annotation in {@code (@KeyFor("map") String) key }.
*
* @param annotatedType its type; is side-effected by this method
* @param expressionTree a {@link NewClassTree}, {@link com.sun.source.tree.NewArrayTree}, or
* {@link com.sun.source.tree.TypeCastTree}
*/
public void atExpression(AnnotatedTypeMirror annotatedType, ExpressionTree expressionTree) {
if (!hasDependentType(annotatedType)) {
return;
}
TreePath path = factory.getPath(expressionTree);
if (path == null) {
return;
}
StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atPath(stringExpr, path, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atExpression(%s, %s) created %s%n", annotatedType, TreeUtils.toStringTruncated(expressionTree, 65), stringToJavaExpr);
}
convertAnnotatedTypeMirror(stringToJavaExpr, annotatedType);
}
use of org.checkerframework.framework.util.StringToJavaExpression in project checker-framework by typetools.
the class DependentTypesHelper method atInvocation.
/**
* Viewpoint-adapts a method or constructor invocation.
*
* <p>{@code methodType} has been viewpoint-adapted to the call site, except for any dependent
* type annotations. (For example, type variables have been substituted and polymorphic qualifiers
* have been resolved.) This method viewpoint-adapts the dependent type annotations.
*
* @param methodType type of the method or constructor invocation; is side-effected by this method
* @param tree invocation of the method or constructor
*/
private void atInvocation(AnnotatedExecutableType methodType, ExpressionTree tree) {
assert hasDependentAnnotations();
Element methodElt = TreeUtils.elementFromUse(tree);
// Because methodType is the type post type variable substitution, it has annotations from
// both the method declaration and the type arguments at the use of the method. Annotations
// from type arguments must not be viewpoint-adapted to the call site. For example:
// Map<String, String> map = ...;
// List<@KeyFor("this.map") String> list = ...;
// list.get(0)
//
// methodType is @KeyFor("this.map") String get(int)
// "this.map" must not be viewpoint-adapted to the invocation because it is not from
// the method declaration, but added during type variable substitution.
//
// So this implementation gets the declared type of the method, declaredMethodType,
// viewpoint-adapts all dependent type annotations in declaredMethodType to the call site,
// and then copies the viewpoint-adapted annotations from methodType except for types that
// are replaced by type variable substitution. (Those annotations are viewpoint-adapted
// before type variable substitution.)
// The annotations on `declaredMethodType` will be copied to `methodType`.
AnnotatedExecutableType declaredMethodType = (AnnotatedExecutableType) factory.getAnnotatedType(methodElt);
if (!hasDependentType(declaredMethodType)) {
return;
}
StringToJavaExpression stringToJavaExpr;
if (tree instanceof MethodInvocationTree) {
stringToJavaExpr = stringExpr -> StringToJavaExpression.atMethodInvocation(stringExpr, (MethodInvocationTree) tree, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atInvocation(%s, %s) 1 created %s%n", methodType, TreeUtils.toStringTruncated(tree, 65), stringToJavaExpr);
}
} else if (tree instanceof NewClassTree) {
stringToJavaExpr = stringExpr -> StringToJavaExpression.atConstructorInvocation(stringExpr, (NewClassTree) tree, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atInvocation(%s, %s) 2 created %s%n", methodType, TreeUtils.toStringTruncated(tree, 65), stringToJavaExpr);
}
} else {
throw new BugInCF("Unexpected tree: %s kind: %s", tree, tree.getKind());
}
convertAnnotatedTypeMirror(stringToJavaExpr, declaredMethodType);
this.viewpointAdaptedCopier.visit(declaredMethodType, methodType);
}
use of org.checkerframework.framework.util.StringToJavaExpression in project checker-framework by typetools.
the class DependentTypesHelper method atParameterizedTypeUse.
/**
* Viewpoint-adapts the dependent type annotations on the bounds of the type parameters of the
* declaration of {@code typeUse} to {@code typeUse}.
*
* @param bounds annotated types of the bounds of the type parameters; its elements are
* side-effected by this method (but the list itself is not side-effected)
* @param typeUse a use of a type with type parameter bounds {@code bounds}
*/
public void atParameterizedTypeUse(List<AnnotatedTypeParameterBounds> bounds, TypeElement typeUse) {
if (!hasDependentAnnotations()) {
return;
}
StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atTypeDecl(stringExpr, typeUse, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atParameterizedTypeUse(%s, %s) created %s%n", bounds, typeUse, stringToJavaExpr);
}
for (AnnotatedTypeParameterBounds bound : bounds) {
convertAnnotatedTypeMirror(stringToJavaExpr, bound.getUpperBound());
convertAnnotatedTypeMirror(stringToJavaExpr, bound.getLowerBound());
}
}
use of org.checkerframework.framework.util.StringToJavaExpression in project checker-framework by typetools.
the class DependentTypesHelper method atMethodBody.
/**
* Viewpoint-adapts the Java expressions in annotations written on the signature of the method
* declaration (for example, a return type) to the body of the method. This means the parameter
* syntax, e.g. "#2", is converted to the names of the parameter.
*
* @param atm a type at the method signature; is side-effected by this method
* @param methodDeclTree a method declaration
*/
public void atMethodBody(AnnotatedTypeMirror atm, MethodTree methodDeclTree) {
if (!hasDependentType(atm)) {
return;
}
StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atMethodBody(stringExpr, methodDeclTree, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atMethodBody(%s, %s) 1 created %s%n", atm, TreeUtils.toStringTruncated(methodDeclTree, 65), stringToJavaExpr);
}
convertAnnotatedTypeMirror(stringToJavaExpr, atm);
}
Aggregations