Allow single element to override array in synthesized annotation

This commit picks up where 8ff9e818a5
left off.

Specifically, this commit introduces support that allows a single
element attribute to override an array attribute with a matching
component type when synthesizing annotations (e.g., in annotations
synthesized from attributes that have been merged from the annotation
hierarchy above a composed annotation).

Issue: SPR-13972
This commit is contained in:
Sam Brannen
2016-03-07 13:27:27 +01:00
parent e086a5d58b
commit eb654dc177
4 changed files with 259 additions and 74 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.core.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
@@ -132,8 +133,16 @@ class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttrib
if (!ClassUtils.isAssignable(requiredReturnType, actualReturnType)) {
boolean converted = false;
// Single element overriding an array of the same type?
if (requiredReturnType.isArray() && requiredReturnType.getComponentType() == actualReturnType) {
Object array = Array.newInstance(requiredReturnType.getComponentType(), 1);
Array.set(array, 0, attributeValue);
attributes.put(attributeName, array);
converted = true;
}
// Nested map representing a single annotation?
if (Annotation.class.isAssignableFrom(requiredReturnType) &&
else if (Annotation.class.isAssignableFrom(requiredReturnType) &&
Map.class.isAssignableFrom(actualReturnType)) {
Class<? extends Annotation> nestedAnnotationType =
(Class<? extends Annotation>) requiredReturnType;