Search in sources :

Example 21 with AnnotationBuilder

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

the class StubParser 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.
 */
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, AnnotationMirror> allStubAnnotations) {
    AnnotationMirror annoMirror;
    if (annotation instanceof MarkerAnnotationExpr) {
        String annoName = ((MarkerAnnotationExpr) annotation).getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
    } else if (annotation instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
        String annoName = nrmanno.getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
        if (annoMirror == null) {
            // Not a supported qualifier -> ignore
            return null;
        }
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
        List<MemberValuePair> pairs = nrmanno.getPairs();
        if (pairs != null) {
            for (MemberValuePair mvp : pairs) {
                String member = mvp.getNameAsString();
                Expression exp = mvp.getValue();
                boolean success = handleExpr(builder, member, exp);
                if (!success) {
                    stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", exp, annotation);
                    return null;
                }
            }
        }
        return builder.build();
    } else if (annotation instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
        String annoName = sglanno.getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
        if (annoMirror == null) {
            // Not a supported qualifier -> ignore
            return null;
        }
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
        Expression valexpr = sglanno.getMemberValue();
        boolean success = handleExpr(builder, "value", valexpr);
        if (!success) {
            stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", valexpr, annotation);
            return null;
        }
        return builder.build();
    } else {
        ErrorReporter.errorAbort("StubParser: unknown annotation type: " + annotation);
        // dead code
        annoMirror = null;
    }
    return annoMirror;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) AnnotationMirror(javax.lang.model.element.AnnotationMirror) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 22 with AnnotationBuilder

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

the class AnnotatedTypeFactory method aliasedAnnotation.

/**
 * Returns the canonical annotation for the passed annotation if it is an alias of a canonical
 * one in the framework. If it is not an alias, the method returns null.
 *
 * <p>A canonical annotation is the internal annotation that will be used by the Checker
 * Framework in the aliased annotation's place.
 *
 * @param a the qualifier to check for an alias
 * @return the canonical annotation or null if none exists
 */
@Nullable
public AnnotationMirror aliasedAnnotation(AnnotationMirror a) {
    TypeElement elem = (TypeElement) a.getAnnotationType().asElement();
    String qualName = elem.getQualifiedName().toString();
    AnnotationMirror canonicalAnno = aliases.get(qualName);
    if (canonicalAnno != null && a.getElementValues().size() > 0) {
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, canonicalAnno);
        if (aliasesCopyElements.contains(qualName)) {
            builder.copyElementValuesFromAnnotation(a, aliasesIgnorableElements.get(qualName));
        }
        return builder.build();
    } else {
        return canonicalAnno;
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) TypeElement(javax.lang.model.element.TypeElement) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 23 with AnnotationBuilder

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

the class ValueAnnotatedTypeFactory method createIntValAnnotation.

/**
 * Returns a {@link IntVal} or {@link IntRange} annotation using the values. If {@code values}
 * is null, then UnknownVal is returned; if {@code values} is empty, then bottom is returned. If
 * the number of {@code values} is greater than MAX_VALUES, return an {@link IntRange}. In other
 * cases, the values are sorted and duplicates are removed before an {@link IntVal} is created.
 *
 * @param values list of longs; duplicates are allowed and the values may be in any order
 * @return an annotation depends on the values
 */
public AnnotationMirror createIntValAnnotation(List<Long> values) {
    if (values == null) {
        return UNKNOWNVAL;
    }
    if (values.isEmpty()) {
        return BOTTOMVAL;
    }
    values = ValueCheckerUtils.removeDuplicates(values);
    if (values.size() > MAX_VALUES) {
        long valMin = Collections.min(values);
        long valMax = Collections.max(values);
        return createIntRangeAnnotation(valMin, valMax);
    } else {
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, IntVal.class);
        builder.setValue("value", values);
        return builder.build();
    }
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 24 with AnnotationBuilder

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

the class ValueAnnotatedTypeFactory method createDoubleValAnnotation.

/**
 * Returns a {@link DoubleVal} 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.
 *
 * @param values list of doubles; duplicates are allowed and the values may be in any order
 * @return a {@link DoubleVal} annotation using the values
 */
public AnnotationMirror createDoubleValAnnotation(List<Double> values) {
    if (values == null) {
        return UNKNOWNVAL;
    }
    if (values.isEmpty()) {
        return BOTTOMVAL;
    }
    values = ValueCheckerUtils.removeDuplicates(values);
    if (values.size() > MAX_VALUES) {
        return UNKNOWNVAL;
    } else {
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, DoubleVal.class);
        builder.setValue("value", values);
        return builder.build();
    }
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 25 with AnnotationBuilder

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

the class ValueAnnotatedTypeFactory method createBooleanAnnotation.

/**
 * Returns a {@link BoolVal} 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.
 *
 * @param values list of booleans; duplicates are allowed and the values may be in any order
 * @return a {@link BoolVal} annotation using the values
 */
public AnnotationMirror createBooleanAnnotation(List<Boolean> values) {
    if (values == null) {
        return UNKNOWNVAL;
    }
    if (values.isEmpty()) {
        return BOTTOMVAL;
    }
    values = ValueCheckerUtils.removeDuplicates(values);
    if (values.size() > MAX_VALUES) {
        return UNKNOWNVAL;
    } else {
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, BoolVal.class);
        builder.setValue("value", values);
        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