Search in sources :

Example 1 with AnnotationFieldType

use of scenelib.annotations.field.AnnotationFieldType in project checker-framework by typetools.

the class AnnotationConverter method getAnnotationFieldType.

/**
 * Returns an AnnotationFieldType given an ExecutableElement or value.
 */
protected static AnnotationFieldType getAnnotationFieldType(ExecutableElement ee, Object value) {
    if (value instanceof List<?>) {
        AnnotationValue defaultValue = ee.getDefaultValue();
        if (defaultValue == null || ((ArrayType) ((Array) defaultValue).type) == null) {
            List<?> listV = (List<?>) value;
            if (!listV.isEmpty()) {
                ScalarAFT scalarAFT = (ScalarAFT) getAnnotationFieldType(ee, ((AnnotationValue) listV.get(0)).getValue());
                if (scalarAFT != null) {
                    return new ArrayAFT(scalarAFT);
                }
            }
            return null;
        }
        Type elemType = ((ArrayType) ((Array) defaultValue).type).elemtype;
        try {
            return new ArrayAFT(BasicAFT.forType(Class.forName(elemType.toString())));
        } catch (ClassNotFoundException e) {
            ErrorReporter.errorAbort(e.getMessage());
        }
    } else if (value instanceof Boolean) {
        return BasicAFT.forType(boolean.class);
    } else if (value instanceof Character) {
        return BasicAFT.forType(char.class);
    } else if (value instanceof Double) {
        return BasicAFT.forType(double.class);
    } else if (value instanceof Float) {
        return BasicAFT.forType(float.class);
    } else if (value instanceof Integer) {
        return BasicAFT.forType(int.class);
    } else if (value instanceof Long) {
        return BasicAFT.forType(long.class);
    } else if (value instanceof Short) {
        return BasicAFT.forType(short.class);
    } else if (value instanceof String) {
        return BasicAFT.forType(String.class);
    }
    return null;
}
Also used : Array(com.sun.tools.javac.code.Attribute.Array) ArrayType(com.sun.tools.javac.code.Type.ArrayType) AnnotationFieldType(scenelib.annotations.field.AnnotationFieldType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) Type(com.sun.tools.javac.code.Type) ScalarAFT(scenelib.annotations.field.ScalarAFT) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) ArrayAFT(scenelib.annotations.field.ArrayAFT)

Example 2 with AnnotationFieldType

use of scenelib.annotations.field.AnnotationFieldType in project j2objc by google.

the class ExternalAnnotationInjector method generateAnnotationMirror.

private GeneratedAnnotationMirror generateAnnotationMirror(Annotation annotation) {
    TypeElement element = typeUtil.resolveJavaType(annotation.def.name);
    if (element == null) {
        reportNoSuchClass(annotation);
        return null;
    }
    DeclaredType type = (DeclaredType) element.asType();
    GeneratedAnnotationMirror annotationMirror = new GeneratedAnnotationMirror(type);
    for (Map.Entry<String, Object> entry : annotation.fieldValues.entrySet()) {
        String fieldName = entry.getKey();
        // For our uses cases, the scenelib library encodes the annotation value as a string.
        String fieldValue = (String) entry.getValue();
        AnnotationFieldType fieldType = annotation.def.fieldTypes.get(fieldName);
        AnnotationField field = generateAnnotationField(annotation, fieldType, fieldName, fieldValue);
        annotationMirror.addElementValue(field.element, field.value);
    }
    return annotationMirror;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) AnnotationFieldType(scenelib.annotations.field.AnnotationFieldType) GeneratedAnnotationMirror(com.google.devtools.j2objc.types.GeneratedAnnotationMirror) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType)

Example 3 with AnnotationFieldType

use of scenelib.annotations.field.AnnotationFieldType in project checker-framework by typetools.

the class AnnotationConverter method typeMirrorToAnnotationFieldType.

/**
 * Converts a TypeMirror to an AnnotationFieldType.
 *
 * @param tm a type for an annotation element/field: primitive, String, class, enum constant, or
 *     array thereof
 * @return an AnnotationFieldType corresponding to the argument
 */
protected static AnnotationFieldType typeMirrorToAnnotationFieldType(TypeMirror tm) {
    switch(tm.getKind()) {
        case BOOLEAN:
            return BasicAFT.forType(boolean.class);
        // Primitves
        case BYTE:
            return BasicAFT.forType(byte.class);
        case CHAR:
            return BasicAFT.forType(char.class);
        case DOUBLE:
            return BasicAFT.forType(double.class);
        case FLOAT:
            return BasicAFT.forType(float.class);
        case INT:
            return BasicAFT.forType(int.class);
        case LONG:
            return BasicAFT.forType(long.class);
        case SHORT:
            return BasicAFT.forType(short.class);
        case ARRAY:
            TypeMirror componentType = ((ArrayType) tm).getComponentType();
            AnnotationFieldType componentAFT = typeMirrorToAnnotationFieldType(componentType);
            return new ArrayAFT((ScalarAFT) componentAFT);
        case DECLARED:
            String className = TypesUtils.getQualifiedName((DeclaredType) tm);
            if (className.equals("java.lang.String")) {
                return BasicAFT.forType(String.class);
            } else if (className.equals("java.lang.Class")) {
                return ClassTokenAFT.ctaft;
            } else {
                // This must be an enum constant.
                return new EnumAFT(className);
            }
        default:
            throw new BugInCF("typeMirrorToAnnotationFieldType: unexpected argument %s [%s %s]", tm, tm.getKind(), tm.getClass());
    }
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationFieldType(scenelib.annotations.field.AnnotationFieldType) EnumAFT(scenelib.annotations.field.EnumAFT) ArrayAFT(scenelib.annotations.field.ArrayAFT) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 4 with AnnotationFieldType

use of scenelib.annotations.field.AnnotationFieldType 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}.
 *
 * @param am the AnnotationMirror
 * @return the Annotation
 */
public static Annotation annotationMirrorToAnnotation(AnnotationMirror am) {
    // TODO: bug for inner classes
    @SuppressWarnings("signature:argument") AnnotationDef def = new AnnotationDef(AnnotationUtils.annotationName(am), String.format("annotationMirrorToAnnotation %s [%s] keyset=%s", am, am.getClass(), am.getElementValues().keySet()));
    Map<String, AnnotationFieldType> fieldTypes = new HashMap<>(am.getElementValues().size());
    // Handling cases where there are fields in annotations.
    for (ExecutableElement ee : am.getElementValues().keySet()) {
        AnnotationFieldType aft = getAnnotationFieldType(ee);
        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<>(values.size());
    for (ExecutableElement ee : values.keySet()) {
        Object value = values.get(ee).getValue();
        if (value instanceof List) {
            // If we have a List here, then it is a List of AnnotationValue.
            // Convert each AnnotationValue to its respective Java type.
            @SuppressWarnings("unchecked") List<AnnotationValue> valueList = (List<AnnotationValue>) value;
            value = CollectionsPlume.mapList(AnnotationValue::getValue, valueList);
        } else if (value instanceof TypeMirror) {
            try {
                value = Class.forName(TypesUtils.binaryName((TypeMirror) value));
            } catch (ClassNotFoundException e) {
                throw new BugInCF(e, "value = %s [%s]", value, value.getClass());
            }
        }
        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) BugInCF(org.checkerframework.javacutil.BugInCF) Annotation(scenelib.annotations.Annotation) AnnotationDef(scenelib.annotations.el.AnnotationDef) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationFieldType(scenelib.annotations.field.AnnotationFieldType) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List)

Example 5 with AnnotationFieldType

use of scenelib.annotations.field.AnnotationFieldType in project checker-framework by typetools.

the class SceneToStubWriter method formatAnnotation.

/**
 * Returns the String representation of an annotation in Java source format.
 *
 * @param a the annotation to print
 * @return the formatted annotation
 */
public static String formatAnnotation(Annotation a) {
    String fullAnnoName = a.def().name;
    String simpleAnnoName = fullAnnoName.substring(fullAnnoName.lastIndexOf('.') + 1);
    if (a.fieldValues.isEmpty()) {
        return "@" + simpleAnnoName;
    }
    StringJoiner sj = new StringJoiner(", ", "@" + simpleAnnoName + "(", ")");
    if (a.fieldValues.size() == 1 && a.fieldValues.containsKey("value")) {
        AnnotationFieldType aft = a.def().fieldTypes.get("value");
        sj.add(aft.format(a.fieldValues.get("value")));
    } else {
        for (Map.Entry<String, Object> f : a.fieldValues.entrySet()) {
            AnnotationFieldType aft = a.def().fieldTypes.get(f.getKey());
            sj.add(f.getKey() + "=" + aft.format(f.getValue()));
        }
    }
    return sj.toString();
}
Also used : AnnotationFieldType(scenelib.annotations.field.AnnotationFieldType) Map(java.util.Map) StringJoiner(java.util.StringJoiner)

Aggregations

AnnotationFieldType (scenelib.annotations.field.AnnotationFieldType)5 ArrayType (com.sun.tools.javac.code.Type.ArrayType)2 List (java.util.List)2 Map (java.util.Map)2 AnnotationValue (javax.lang.model.element.AnnotationValue)2 TypeMirror (javax.lang.model.type.TypeMirror)2 BugInCF (org.checkerframework.javacutil.BugInCF)2 ArrayAFT (scenelib.annotations.field.ArrayAFT)2 GeneratedAnnotationMirror (com.google.devtools.j2objc.types.GeneratedAnnotationMirror)1 Array (com.sun.tools.javac.code.Attribute.Array)1 Type (com.sun.tools.javac.code.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StringJoiner (java.util.StringJoiner)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 Annotation (scenelib.annotations.Annotation)1 AnnotationDef (scenelib.annotations.el.AnnotationDef)1 EnumAFT (scenelib.annotations.field.EnumAFT)1