Search in sources :

Example 1 with EnsuresQualifier

use of org.checkerframework.framework.qual.EnsuresQualifier in project checker-framework by typetools.

the class GenericAnnotatedTypeFactory method createRequiresOrEnsuresQualifier.

/**
 * Creates a {@code RequiresQualifier("...")} or {@code EnsuresQualifier("...")} annotation for
 * the given expression.
 *
 * <p>This is of the form {@code @RequiresQualifier(expression="expression",
 * qualifier=MyQual.class)} or {@code @EnsuresQualifier(expression="expression",
 * qualifier=MyQual.class)}, where "expression" is exactly the string {@code expression} and
 * MyQual is the annotation represented by {@code qualifier}.
 *
 * <p>Returns null if the expression is invalid when combined with the kind of annotation: for
 * example, precondition annotations on "this" and parameters ("#1", etc.) are not supported,
 * because receiver/parameter annotations should be inferred instead.
 *
 * <p>This implementation returns null if no annotation can be created, because the qualifier has
 * elements/arguments, which {@code @RequiresQualifier} and {@code @EnsuresQualifier} do not
 * support. Subclasses may override this method to return qualifiers that do have arguments
 * instead of returning null.
 *
 * @param expression the expression to which the qualifier applies
 * @param qualifier the qualifier that must be present
 * @param declaredType the declared type of the expression, which is used to avoid inferring
 *     redundant pre- or postcondition annotations
 * @param preOrPost whether to return a precondition or postcondition annotation
 * @param preconds the list of precondition annotations; used to suppress redundant
 *     postconditions; non-null exactly when {@code preOrPost} is {@code BeforeOrAfter.BEFORE}
 * @return a {@code RequiresQualifier("...")} or {@code EnsuresQualifier("...")} annotation for
 *     the given expression, or null
 */
@Nullable
protected AnnotationMirror createRequiresOrEnsuresQualifier(String expression, AnnotationMirror qualifier, AnnotatedTypeMirror declaredType, Analysis.BeforeOrAfter preOrPost, @Nullable List<AnnotationMirror> preconds) {
    // Do not generate RequiresQualifier annotations for "this" or parameter expressions.
    if (preOrPost == BeforeOrAfter.BEFORE && ("this".equals(expression) || formalParameterPattern.matcher(expression).matches())) {
        return null;
    }
    if (!qualifier.getElementValues().isEmpty()) {
        // elements/arguments.
        return null;
    }
    AnnotationBuilder builder = new AnnotationBuilder(processingEnv, preOrPost == BeforeOrAfter.BEFORE ? RequiresQualifier.class : EnsuresQualifier.class);
    builder.setValue("expression", new String[] { expression });
    builder.setValue("qualifier", AnnotationUtils.annotationMirrorToClass(qualifier));
    return builder.build();
}
Also used : RequiresQualifier(org.checkerframework.framework.qual.RequiresQualifier) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) EnsuresQualifier(org.checkerframework.framework.qual.EnsuresQualifier) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 EnsuresQualifier (org.checkerframework.framework.qual.EnsuresQualifier)1 RequiresQualifier (org.checkerframework.framework.qual.RequiresQualifier)1 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)1