Search in sources :

Example 1 with AnnotationDef

use of scenelib.annotations.el.AnnotationDef in project checker-framework by typetools.

the class AnnotationConverter method annotationMirrorToAnnotation.

/**
 * Converts an {@link javax.lang.model.element.AnnotationMirror} into an {@link
 * scenelib.annotations.Annotation}.
 */
protected static Annotation annotationMirrorToAnnotation(AnnotationMirror am) {
    AnnotationDef def = new AnnotationDef(AnnotationUtils.annotationName(am));
    Map<String, AnnotationFieldType> fieldTypes = new HashMap<>();
    // Handling cases where there are fields in annotations.
    for (ExecutableElement ee : am.getElementValues().keySet()) {
        AnnotationFieldType aft = getAnnotationFieldType(ee, am.getElementValues().get(ee).getValue());
        if (aft == null)
            return null;
        // Here we just add the type of the field into fieldTypes.
        fieldTypes.put(ee.getSimpleName().toString(), aft);
    }
    def.setFieldTypes(fieldTypes);
    // Now, we handle the values of those types below
    Map<? extends ExecutableElement, ? extends AnnotationValue> values = am.getElementValues();
    Map<String, Object> newValues = new HashMap<>();
    for (ExecutableElement ee : values.keySet()) {
        Object value = values.get(ee).getValue();
        if (value instanceof List) {
            @SuppressWarnings("unchecked") List<Object> valueList = (List<Object>) value;
            List<Object> newList = new ArrayList<>();
            // Converting each AnnotatedValue to its respective Java type:
            for (Object o : valueList) {
                newList.add(((AnnotationValue) o).getValue());
            }
            value = newList;
        }
        newValues.put(ee.getSimpleName().toString(), value);
    }
    Annotation out = new Annotation(def, newValues);
    return out;
}
Also used : HashMap(java.util.HashMap) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Annotation(scenelib.annotations.Annotation) AnnotationDef(scenelib.annotations.el.AnnotationDef) AnnotationFieldType(scenelib.annotations.field.AnnotationFieldType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with AnnotationDef

use of scenelib.annotations.el.AnnotationDef in project checker-framework by typetools.

the class ToIndexFileConverter method extractAnnotation.

/**
 * Builds simplified annotation from its declaration. Only the name is included, because
 * stubfiles do not generally have access to the full definitions of annotations.
 */
private static Annotation extractAnnotation(AnnotationExpr expr) {
    // leave off leading '@'
    String exprName = expr.toString().substring(1);
    // the JDK adds, apparently for profiling.
    if (exprName.contains("+")) {
        return null;
    }
    AnnotationDef def = new AnnotationDef(exprName);
    def.setFieldTypes(Collections.emptyMap());
    return new Annotation(def, Collections.emptyMap());
}
Also used : Annotation(scenelib.annotations.Annotation) AnnotationDef(scenelib.annotations.el.AnnotationDef)

Aggregations

Annotation (scenelib.annotations.Annotation)2 AnnotationDef (scenelib.annotations.el.AnnotationDef)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 AnnotationFieldType (scenelib.annotations.field.AnnotationFieldType)1