Example usage for org.springframework.core.annotation AnnotationAttributes annotationType

List of usage examples for org.springframework.core.annotation AnnotationAttributes annotationType

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationAttributes annotationType.

Prototype

Class annotationType

To view the source code for org.springframework.core.annotation AnnotationAttributes annotationType.

Click Source Link

Usage

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Register the annotation-declared default values for the given attributes,
 * if available./*w  w w  . j av a 2s .c  o  m*/
 * @param attributes the annotation attributes to process
 * @since 4.3.2
 */
public static void registerDefaultValues(AnnotationAttributes attributes) {
    // Only do defaults scanning for public annotations; we'd run into
    // IllegalAccessExceptions otherwise, and we don't want to mess with
    // accessibility in a SecurityManager environment.
    Class<? extends Annotation> annotationType = attributes.annotationType();
    if (annotationType != null && Modifier.isPublic(annotationType.getModifiers())) {
        // Check declared default values of attributes in the annotation type.
        for (Method annotationAttribute : getAttributeMethods(annotationType)) {
            String attributeName = annotationAttribute.getName();
            Object defaultValue = annotationAttribute.getDefaultValue();
            if (defaultValue != null && !attributes.containsKey(attributeName)) {
                if (defaultValue instanceof Annotation) {
                    defaultValue = getAnnotationAttributes((Annotation) defaultValue, false, true);
                } else if (defaultValue instanceof Annotation[]) {
                    Annotation[] realAnnotations = (Annotation[]) defaultValue;
                    AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length];
                    for (int i = 0; i < realAnnotations.length; i++) {
                        mappedAnnotations[i] = getAnnotationAttributes(realAnnotations[i], false, true);
                    }
                    defaultValue = mappedAnnotations;
                }
                attributes.put(attributeName, new DefaultValueHolder(defaultValue));
            }
        }
    }
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Post-process the supplied {@link AnnotationAttributes}.
 * <p>Specifically, this method enforces <em>attribute alias</em> semantics
 * for annotation attributes that are annotated with {@link AliasFor @AliasFor}
 * and replaces default value placeholders with their original default values.
 * @param annotatedElement the element that is annotated with an annotation or
 * annotation hierarchy from which the supplied attributes were created;
 * may be {@code null} if unknown//from   w w w  .ja  v  a  2 s .  c om
 * @param attributes the annotation attributes to post-process
 * @param classValuesAsString whether to convert Class references into Strings (for
 * compatibility with {@link org.springframework.core.type.AnnotationMetadata})
 * or to preserve them as Class references
 * @param nestedAnnotationsAsMap whether to convert nested annotations into
 * {@link AnnotationAttributes} maps (for compatibility with
 * {@link org.springframework.core.type.AnnotationMetadata}) or to preserve them as
 * {@code Annotation} instances
 * @since 4.2
 * @see #retrieveAnnotationAttributes(Object, Annotation, boolean, boolean)
 * @see #getDefaultValue(Class, String)
 */
static void postProcessAnnotationAttributes(@Nullable Object annotatedElement,
        @Nullable AnnotationAttributes attributes, boolean classValuesAsString,
        boolean nestedAnnotationsAsMap) {

    if (attributes == null) {
        return;
    }

    Class<? extends Annotation> annotationType = attributes.annotationType();

    // Track which attribute values have already been replaced so that we can short
    // circuit the search algorithms.
    Set<String> valuesAlreadyReplaced = new HashSet<>();

    if (!attributes.validated) {
        // Validate @AliasFor configuration
        Map<String, List<String>> aliasMap = getAttributeAliasMap(annotationType);
        for (String attributeName : aliasMap.keySet()) {
            if (valuesAlreadyReplaced.contains(attributeName)) {
                continue;
            }
            Object value = attributes.get(attributeName);
            boolean valuePresent = (value != null && !(value instanceof DefaultValueHolder));

            for (String aliasedAttributeName : aliasMap.get(attributeName)) {
                if (valuesAlreadyReplaced.contains(aliasedAttributeName)) {
                    continue;
                }

                Object aliasedValue = attributes.get(aliasedAttributeName);
                boolean aliasPresent = (aliasedValue != null && !(aliasedValue instanceof DefaultValueHolder));

                // Something to validate or replace with an alias?
                if (valuePresent || aliasPresent) {
                    if (valuePresent && aliasPresent) {
                        // Since annotation attributes can be arrays, we must use ObjectUtils.nullSafeEquals().
                        if (!ObjectUtils.nullSafeEquals(value, aliasedValue)) {
                            String elementAsString = (annotatedElement != null ? annotatedElement.toString()
                                    : "unknown element");
                            throw new AnnotationConfigurationException(String.format(
                                    "In AnnotationAttributes for annotation [%s] declared on %s, "
                                            + "attribute '%s' and its alias '%s' are declared with values of [%s] and [%s], "
                                            + "but only one is permitted.",
                                    attributes.displayName, elementAsString, attributeName,
                                    aliasedAttributeName, ObjectUtils.nullSafeToString(value),
                                    ObjectUtils.nullSafeToString(aliasedValue)));
                        }
                    } else if (aliasPresent) {
                        // Replace value with aliasedValue
                        attributes.put(attributeName, adaptValue(annotatedElement, aliasedValue,
                                classValuesAsString, nestedAnnotationsAsMap));
                        valuesAlreadyReplaced.add(attributeName);
                    } else {
                        // Replace aliasedValue with value
                        attributes.put(aliasedAttributeName, adaptValue(annotatedElement, value,
                                classValuesAsString, nestedAnnotationsAsMap));
                        valuesAlreadyReplaced.add(aliasedAttributeName);
                    }
                }
            }
        }
        attributes.validated = true;
    }

    // Replace any remaining placeholders with actual default values
    for (String attributeName : attributes.keySet()) {
        if (valuesAlreadyReplaced.contains(attributeName)) {
            continue;
        }
        Object value = attributes.get(attributeName);
        if (value instanceof DefaultValueHolder) {
            value = ((DefaultValueHolder) value).defaultValue;
            attributes.put(attributeName,
                    adaptValue(annotatedElement, value, classValuesAsString, nestedAnnotationsAsMap));
        }
    }
}