Search in sources :

Example 1 with ArrayAFT

use of scenelib.annotations.field.ArrayAFT 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 ArrayAFT

use of scenelib.annotations.field.ArrayAFT 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)

Aggregations

ArrayType (com.sun.tools.javac.code.Type.ArrayType)2 AnnotationFieldType (scenelib.annotations.field.AnnotationFieldType)2 ArrayAFT (scenelib.annotations.field.ArrayAFT)2 Array (com.sun.tools.javac.code.Attribute.Array)1 Type (com.sun.tools.javac.code.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 TypeMirror (javax.lang.model.type.TypeMirror)1 BugInCF (org.checkerframework.javacutil.BugInCF)1 EnumAFT (scenelib.annotations.field.EnumAFT)1 ScalarAFT (scenelib.annotations.field.ScalarAFT)1