use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class UnitsRelationsTools method buildAnnoMirrorWithSpecificPrefix.
/**
* Creates an AnnotationMirror representing a unit defined by annoClass, with the specific Prefix
* p.
*
* @param env the Checker Processing Environment, provided as a parameter in init() of a
* UnitsRelations implementation
* @param annoClass the fully-qualified name of an Annotation representing a Unit (eg m.class for
* meters)
* @param p a Prefix value
* @return an AnnotationMirror of the Unit with the Prefix p, or null if it cannot be constructed
*/
@Nullable
public static AnnotationMirror buildAnnoMirrorWithSpecificPrefix(final ProcessingEnvironment env, @FullyQualifiedName final CharSequence annoClass, final Prefix p) {
AnnotationBuilder builder = new AnnotationBuilder(env, annoClass);
builder.setValue("value", p);
return builder.build();
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class FormatterTreeUtil method stringToInvalidFormatAnnotation.
/**
* Creates an {@link InvalidFormat} annotation with the given string as its value.
*
* @param invalidFormatString an invalid formatter string
* @return an {@link InvalidFormat} annotation with the given string as its value
*/
// package-private
AnnotationMirror stringToInvalidFormatAnnotation(String invalidFormatString) {
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, InvalidFormat.class);
builder.setValue("value", invalidFormatString);
return builder.build();
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class CalledMethodsAnnotatedTypeFactory method ensuresCMAnno.
/**
* Returns a {@code @EnsuresCalledMethods("...")} annotation for the given expression.
*
* @param expression the expression to put in the value field of the EnsuresCalledMethods
* annotation
* @param calledMethods the methods that were definitely called on the expression
* @return a {@code @EnsuresCalledMethods("...")} annotation for the given expression
*/
private AnnotationMirror ensuresCMAnno(String expression, List<String> calledMethods) {
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, EnsuresCalledMethods.class);
builder.setValue("value", new String[] { expression });
builder.setValue("methods", calledMethods.toArray(new String[0]));
AnnotationMirror am = builder.build();
return am;
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class AnnotationFileParser method getAnnotation.
/**
* Convert {@code annotation} into an AnnotationMirror. Returns null if the annotation isn't
* supported by the checker or if some error occurred while converting it.
*
* @param annotation syntax tree for an annotation
* @param allAnnotations map from simple name to annotation definition; side-effected by this
* method
* @return the AnnotationMirror for the annotation, or null if it cannot be built
*/
@Nullable
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, TypeElement> allAnnotations) {
// https://tinyurl.com/cfissue/3094
@SuppressWarnings("signature") @FullyQualifiedName String annoNameFq = annotation.getNameAsString();
TypeElement annoTypeElt = allAnnotations.get(annoNameFq);
if (annoTypeElt == null) {
// If the annotation was not imported, then #getImportedAnnotations did not add it to the
// allAnnotations field. This code adds the annotation when it is encountered (i.e. here).
// Note that this does not call AnnotationFileParser#getTypeElement to avoid a spurious
// diagnostic if the annotation is actually unknown.
annoTypeElt = elements.getTypeElement(annoNameFq);
if (annoTypeElt == null) {
// Not a supported annotation -> ignore
return null;
}
putAllNew(allAnnotations, createNameToAnnotationMap(Collections.singletonList(annoTypeElt)));
}
// not anonymous, so name is not empty
@SuppressWarnings("signature") @CanonicalName String annoName = annoTypeElt.getQualifiedName().toString();
if (annotation instanceof MarkerAnnotationExpr) {
return AnnotationBuilder.fromName(elements, annoName);
} else if (annotation instanceof NormalAnnotationExpr) {
NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
List<MemberValuePair> pairs = nrmanno.getPairs();
if (pairs != null) {
for (MemberValuePair mvp : pairs) {
String member = mvp.getNameAsString();
Expression exp = mvp.getValue();
try {
builderAddElement(builder, member, exp);
} catch (AnnotationFileParserException e) {
warn(exp, "For annotation %s, could not add %s=%s because %s", annotation, member, exp, e.getMessage());
return null;
}
}
}
return builder.build();
} else if (annotation instanceof SingleMemberAnnotationExpr) {
SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
Expression valExpr = sglanno.getMemberValue();
try {
builderAddElement(builder, "value", valExpr);
} catch (AnnotationFileParserException e) {
warn(valExpr, "For annotation %s, could not add value=%s because %s", annotation, valExpr, e.getMessage());
return null;
}
return builder.build();
} else {
throw new BugInCF("AnnotationFileParser: unknown annotation type: " + annotation);
}
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class AccumulationAnnotatedTypeFactory method createAccumulatorAnnotation.
/**
* Creates a new instance of the accumulator annotation that contains the elements of {@code
* values}.
*
* @param values the arguments to the annotation. The values can contain duplicates and can be in
* any order.
* @return an annotation mirror representing the accumulator annotation with {@code values}'s
* arguments; this is top if {@code values} is empty
*/
public AnnotationMirror createAccumulatorAnnotation(List<String> values) {
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, accumulator);
builder.setValue("value", CollectionsPlume.withoutDuplicates(values));
return builder.build();
}
Aggregations