Search in sources :

Example 61 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method createArrayLenRangeAnnotation.

/**
 * Create an {@code @ArrayLenRange} annotation from the two (inclusive) bounds. Does not return
 * BOTTOMVAL or UNKNOWNVAL.
 */
public AnnotationMirror createArrayLenRangeAnnotation(int from, int to) {
    assert from <= to;
    AnnotationBuilder builder = new AnnotationBuilder(processingEnv, ArrayLenRange.class);
    builder.setValue("from", from);
    builder.setValue("to", to);
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 62 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method createStringAnnotation.

/**
 * Returns a {@link StringVal} annotation using the values. If {@code values} is null, then
 * UnknownVal is returned; if {@code values} is empty, then bottom is returned. The values are
 * sorted and duplicates are removed before the annotation is created. If values is larger than
 * the max number of values allowed (10 by default), then an {@link ArrayLen} or an {@link
 * ArrayLenRange} annotation is returned.
 *
 * @param values list of strings; duplicates are allowed and the values may be in any order
 * @return a {@link StringVal} annotation using the values
 */
public AnnotationMirror createStringAnnotation(List<String> values) {
    if (values == null) {
        return UNKNOWNVAL;
    }
    if (values.isEmpty()) {
        return BOTTOMVAL;
    }
    values = ValueCheckerUtils.removeDuplicates(values);
    if (values.size() > MAX_VALUES) {
        // Too many strings are replaced by their lengths
        List<Integer> lengths = ValueCheckerUtils.getLengthsForStringValues(values);
        return createArrayLenAnnotation(lengths);
    } else {
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, StringVal.class);
        builder.setValue("value", values);
        return builder.build();
    }
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 63 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method createIntRangeAnnotation.

/**
 * Create an {@code @IntRange} annotation from the two (inclusive) bounds. Does not return
 * BOTTOMVAL or UNKNOWNVAL.
 */
private AnnotationMirror createIntRangeAnnotation(long from, long to) {
    assert from <= to;
    AnnotationBuilder builder = new AnnotationBuilder(processingEnv, IntRange.class);
    builder.setValue("from", from);
    builder.setValue("to", to);
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Aggregations

AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)63 Test (org.junit.Test)30 AnnotationMirror (javax.lang.model.element.AnnotationMirror)11 ArrayList (java.util.ArrayList)3 InvalidFormat (org.checkerframework.checker.formatter.qual.InvalidFormat)3 I18nInvalidFormat (org.checkerframework.checker.i18nformatter.qual.I18nInvalidFormat)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Elements (javax.lang.model.util.Elements)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)2 NodeList (com.github.javaparser.ast.NodeList)1 Expression (com.github.javaparser.ast.expr.Expression)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)1 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)1 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)1 Annotation (java.lang.annotation.Annotation)1 List (java.util.List)1 TypeElement (javax.lang.model.element.TypeElement)1