Search in sources :

Example 51 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.

the class ContractsUtils method makeArgumentMap.

/**
 * Makes a map from element names of a contract annotation to qualifier argument names, as
 * defined by {@link QualifierArgument}.
 *
 * <p>Each element of {@code contractAnnoElement} that is annotated by {@link QualifierArgument}
 * is mapped to the name specified by the value of {@link QualifierArgument}. If the value is
 * not specified or is an empty string, then the element is mapped to an argument of the same
 * name.
 *
 * @param contractAnnoElement the declaration of the contract annotation containing the elements
 * @return map from the names of elements of {@code sourceArgumentNames} to the corresponding
 *     qualifier argument names
 * @see QualifierArgument
 */
private Map<String, String> makeArgumentMap(Element contractAnnoElement) {
    HashMap<String, String> argumentMap = new HashMap<>();
    for (ExecutableElement meth : ElementFilter.methodsIn(contractAnnoElement.getEnclosedElements())) {
        AnnotationMirror argumentAnnotation = factory.getDeclAnnotationNoAliases(meth, QualifierArgument.class);
        if (argumentAnnotation != null) {
            String sourceName = meth.getSimpleName().toString();
            String targetName = AnnotationUtils.getElementValue(argumentAnnotation, "value", String.class, false);
            if (targetName == null || targetName.isEmpty()) {
                targetName = sourceName;
            }
            argumentMap.put(sourceName, targetName);
        }
    }
    return argumentMap;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashMap(java.util.HashMap) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 52 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.

the class ContractsUtils method getPreconditions.

/**
 * Returns the set of preconditions on the element {@code element}.
 */
public Set<Precondition> getPreconditions(Element element) {
    Set<Precondition> result = new LinkedHashSet<>();
    // Check for a single contract.
    AnnotationMirror requiresAnnotation = factory.getDeclAnnotation(element, RequiresQualifier.class);
    result.addAll(getPrecondition(requiresAnnotation));
    // Check for multiple contracts.
    AnnotationMirror requiresAnnotations = factory.getDeclAnnotation(element, RequiresQualifiers.class);
    if (requiresAnnotations != null) {
        List<AnnotationMirror> annotations = AnnotationUtils.getElementValueArray(requiresAnnotations, "value", AnnotationMirror.class, false);
        for (AnnotationMirror a : annotations) {
            result.addAll(getPrecondition(a));
        }
    }
    // Check type-system specific annotations.
    Class<PreconditionAnnotation> metaAnnotation = PreconditionAnnotation.class;
    List<Pair<AnnotationMirror, AnnotationMirror>> declAnnotations = factory.getDeclAnnotationWithMetaAnnotation(element, metaAnnotation);
    for (Pair<AnnotationMirror, AnnotationMirror> r : declAnnotations) {
        AnnotationMirror anno = r.first;
        AnnotationMirror metaAnno = r.second;
        List<String> expressions = AnnotationUtils.getElementValueArray(anno, "value", String.class, false);
        AnnotationMirror precondtionAnno = getAnnotationMirrorOfMetaAnnotation(metaAnno, anno);
        if (precondtionAnno == null) {
            continue;
        }
        for (String expr : expressions) {
            result.add(new Precondition(expr, precondtionAnno));
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PreconditionAnnotation(org.checkerframework.framework.qual.PreconditionAnnotation) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Pair(org.checkerframework.javacutil.Pair)

Example 53 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.

the class GraphQualifierHierarchy method finish.

// private final AnnotationMirror bottom;
@Override
protected void finish(QualifierHierarchy qualHierarchy, Map<AnnotationMirror, Set<AnnotationMirror>> fullMap, Map<AnnotationMirror, AnnotationMirror> polyQualifiers, Set<AnnotationMirror> tops, Set<AnnotationMirror> bottoms, Object... args) {
    // Careful, when this method is called, a field this.bottom would not be set yet.
    if (args != null && args[0] != null) {
        AnnotationMirror thebottom = (AnnotationMirror) args[0];
        // A special bottom qualifier was provided; go through the existing
        // bottom qualifiers and tie them all to this bottom qualifier.
        // Set<AnnotationMirror> bottoms = findBottoms(supertypes);
        Set<AnnotationMirror> allQuals = AnnotationUtils.createAnnotationSet();
        allQuals.addAll(fullMap.keySet());
        allQuals.remove(thebottom);
        AnnotationUtils.updateMappingToImmutableSet(fullMap, thebottom, allQuals);
        // thebottom is initially a top qualifier
        tops.remove(thebottom);
        // thebottom is now the single bottom qualifier
        bottoms.clear();
        bottoms.add(thebottom);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Example 54 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.

the class GraphQualifierHierarchy method isSubtype.

@Override
public boolean isSubtype(Collection<? extends AnnotationMirror> rhs, Collection<? extends AnnotationMirror> lhs) {
    rhs = replacePolyAll(rhs);
    lhs = replacePolyAll(lhs);
    if (lhs.isEmpty() || rhs.isEmpty()) {
        ErrorReporter.errorAbort("GraphQualifierHierarchy: Empty annotations in lhs: " + lhs + " or rhs: " + rhs);
    }
    if (lhs.size() > 1) {
        ErrorReporter.errorAbort("GraphQualifierHierarchy: Type with more than one annotation found: " + lhs);
    }
    if (rhs.size() > 1) {
        ErrorReporter.errorAbort("GraphQualifierHierarchy: Type with more than one annotation found: " + rhs);
    }
    for (AnnotationMirror lhsAnno : lhs) {
        for (AnnotationMirror rhsAnno : rhs) {
            if (isSubtype(rhsAnno, lhsAnno)) {
                return true;
            }
        }
    }
    return false;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Example 55 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.

the class MultiGraphQualifierHierarchy method calculateLubs.

private Map<AnnotationPair, AnnotationMirror> calculateLubs() {
    Map<AnnotationPair, AnnotationMirror> newlubs = new HashMap<>();
    for (AnnotationMirror a1 : typeQualifiers) {
        for (AnnotationMirror a2 : typeQualifiers) {
            if (AnnotationUtils.areSameIgnoringValues(a1, a2)) {
                continue;
            }
            if (!AnnotationUtils.areSame(getTopAnnotation(a1), getTopAnnotation(a2))) {
                continue;
            }
            AnnotationPair pair = new AnnotationPair(a1, a2);
            if (newlubs.containsKey(pair)) {
                continue;
            }
            AnnotationMirror lub = findLub(a1, a2);
            newlubs.put(pair, lub);
        }
    }
    return newlubs;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashMap(java.util.HashMap)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)665 TypeElement (javax.lang.model.element.TypeElement)159 ExecutableElement (javax.lang.model.element.ExecutableElement)112 TypeMirror (javax.lang.model.type.TypeMirror)99 ArrayList (java.util.ArrayList)92 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)90 Element (javax.lang.model.element.Element)87 VariableElement (javax.lang.model.element.VariableElement)79 AnnotationValue (javax.lang.model.element.AnnotationValue)73 DeclaredType (javax.lang.model.type.DeclaredType)58 List (java.util.List)52 CFValue (org.checkerframework.framework.flow.CFValue)52 Map (java.util.Map)46 HashSet (java.util.HashSet)40 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)39 CFStore (org.checkerframework.framework.flow.CFStore)39 HashMap (java.util.HashMap)37 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)36 ExpressionTree (com.sun.source.tree.ExpressionTree)30 MethodTree (com.sun.source.tree.MethodTree)29