Search in sources :

Example 1 with Type

use of org.springframework.asm.Type in project spring-framework by spring-projects.

the class AnnotationReadingVisitorUtils method convertClassValues.

public static AnnotationAttributes convertClassValues(Object annotatedElement, ClassLoader classLoader, AnnotationAttributes original, boolean classValuesAsString) {
    if (original == null) {
        return null;
    }
    AnnotationAttributes result = new AnnotationAttributes(original);
    AnnotationUtils.postProcessAnnotationAttributes(annotatedElement, result, classValuesAsString);
    for (Map.Entry<String, Object> entry : result.entrySet()) {
        try {
            Object value = entry.getValue();
            if (value instanceof AnnotationAttributes) {
                value = convertClassValues(annotatedElement, classLoader, (AnnotationAttributes) value, classValuesAsString);
            } else if (value instanceof AnnotationAttributes[]) {
                AnnotationAttributes[] values = (AnnotationAttributes[]) value;
                for (int i = 0; i < values.length; i++) {
                    values[i] = convertClassValues(annotatedElement, classLoader, values[i], classValuesAsString);
                }
                value = values;
            } else if (value instanceof Type) {
                value = (classValuesAsString ? ((Type) value).getClassName() : classLoader.loadClass(((Type) value).getClassName()));
            } else if (value instanceof Type[]) {
                Type[] array = (Type[]) value;
                Object[] convArray = (classValuesAsString ? new String[array.length] : new Class<?>[array.length]);
                for (int i = 0; i < array.length; i++) {
                    convArray[i] = (classValuesAsString ? array[i].getClassName() : classLoader.loadClass(array[i].getClassName()));
                }
                value = convArray;
            } else if (classValuesAsString) {
                if (value instanceof Class) {
                    value = ((Class<?>) value).getName();
                } else if (value instanceof Class[]) {
                    Class<?>[] clazzArray = (Class<?>[]) value;
                    String[] newValue = new String[clazzArray.length];
                    for (int i = 0; i < clazzArray.length; i++) {
                        newValue[i] = clazzArray[i].getName();
                    }
                    value = newValue;
                }
            }
            entry.setValue(value);
        } catch (Throwable ex) {
            // Class not found - can't resolve class reference in annotation attribute.
            result.put(entry.getKey(), ex);
        }
    }
    return result;
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) Type(org.springframework.asm.Type) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Aggregations

Map (java.util.Map)1 Type (org.springframework.asm.Type)1 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1