Search in sources :

Example 1 with EnumAFT

use of scenelib.annotations.field.EnumAFT 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 2 with EnumAFT

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

the class ExternalAnnotationInjector method generateAnnotationField.

private AnnotationField generateAnnotationField(Annotation annotation, AnnotationFieldType type, String name, String value) {
    AnnotationField field = new AnnotationField();
    if (type instanceof BasicAFT) {
        Class<?> enclosedType = ((BasicAFT) type).type;
        if (String.class.isAssignableFrom(enclosedType)) {
            field.element = GeneratedExecutableElement.newMethodWithSelector(name, typeUtil.getJavaString().asType(), null);
            field.value = new GeneratedAnnotationValue(value);
        } else {
            ErrorUtil.error("ExternalAnnotationInjector: unsupported field type " + type);
        }
    } else if (type instanceof EnumAFT) {
        String enumTypeString = annotation.def.name + "." + ((EnumAFT) type).typeName;
        TypeMirror enumType = typeUtil.resolveJavaType(enumTypeString).asType();
        field.element = GeneratedExecutableElement.newMethodWithSelector(name, enumType, /* enclosingElement = */
        null);
        field.value = new GeneratedAnnotationValue(GeneratedVariableElement.newParameter(value, enumType, /* enclosingElement = */
        null));
    } else {
        ErrorUtil.error("ExternalAnnotationInjector: unsupported field type " + type);
    }
    return field;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) GeneratedAnnotationValue(com.google.devtools.j2objc.types.GeneratedAnnotationValue) EnumAFT(scenelib.annotations.field.EnumAFT) BasicAFT(scenelib.annotations.field.BasicAFT)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)2 EnumAFT (scenelib.annotations.field.EnumAFT)2 GeneratedAnnotationValue (com.google.devtools.j2objc.types.GeneratedAnnotationValue)1 ArrayType (com.sun.tools.javac.code.Type.ArrayType)1 BugInCF (org.checkerframework.javacutil.BugInCF)1 AnnotationFieldType (scenelib.annotations.field.AnnotationFieldType)1 ArrayAFT (scenelib.annotations.field.ArrayAFT)1 BasicAFT (scenelib.annotations.field.BasicAFT)1