Search in sources :

Example 81 with AnnotationBuilder

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();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 82 with AnnotationBuilder

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();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 83 with AnnotationBuilder

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;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 84 with AnnotationBuilder

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);
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) BugInCF(org.checkerframework.javacutil.BugInCF) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Expression(com.github.javaparser.ast.expr.Expression) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) CanonicalName(org.checkerframework.checker.signature.qual.CanonicalName) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 85 with AnnotationBuilder

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();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Aggregations

AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)117 Test (org.junit.Test)60 AnnotationMirror (javax.lang.model.element.AnnotationMirror)21 Nullable (org.checkerframework.checker.nullness.qual.Nullable)6 ArrayList (java.util.ArrayList)5 Elements (javax.lang.model.util.Elements)4 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)4 List (java.util.List)3 TypeElement (javax.lang.model.element.TypeElement)3 NodeList (com.github.javaparser.ast.NodeList)2 Expression (com.github.javaparser.ast.expr.Expression)2 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)2 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)2 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)2 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)2 TreeSet (java.util.TreeSet)2 TypeMirror (javax.lang.model.type.TypeMirror)2 FormatterTreeUtil (org.checkerframework.checker.formatter.FormatterTreeUtil)2 ConversionCategory (org.checkerframework.checker.formatter.qual.ConversionCategory)2 InvalidFormat (org.checkerframework.checker.formatter.qual.InvalidFormat)2