diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index 199988e9c6..ab014d3fe8 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -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.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -926,10 +927,7 @@ public abstract class AnnotationUtils { return mappedAnnotations; } else { - for (int i = 0; i < annotations.length; i++) { - annotations[i] = synthesizeAnnotation(annotations[i], annotatedElement); - } - return annotations; + return synthesizeAnnotationArray(annotations, annotatedElement); } } @@ -1124,6 +1122,35 @@ public abstract class AnnotationUtils { return synthesizedAnnotation; } + /** + * Synthesize the supplied array of {@code annotations} by + * creating a new array of the same size and type and populating it + * with {@linkplain #synthesizeAnnotation(Annotation) synthesized} + * versions of the annotations from the input array. + * + * @param annotations the array of annotations to synthesize + * @param annotatedElement the element that is annotated with the supplied + * array of annotations; may be {@code null} if unknown + * @return a new array of synthesized annotations, or {@code null} if + * the supplied array is {@code null} + * @throws AnnotationConfigurationException if invalid configuration of + * {@code @AliasFor} is detected + * @since 4.2 + * @see #synthesizeAnnotation(Annotation, AnnotatedElement) + * @see #synthesizeAnnotation(Map, Class, AnnotatedElement) + */ + public static Annotation[] synthesizeAnnotationArray(Annotation[] annotations, AnnotatedElement annotatedElement) { + if (annotations == null) { + return null; + } + + Annotation[] synthesized = (Annotation[]) Array.newInstance(annotations.getClass().getComponentType(), annotations.length); + for (int i = 0; i < annotations.length; i++) { + synthesized[i] = synthesizeAnnotation(annotations[i], annotatedElement); + } + return synthesized; + } + /** * Get a map of all attribute alias pairs, declared via {@code @AliasFor} * in the supplied annotation type. diff --git a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java index dedd7c9bad..c3ae43c58e 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java @@ -18,7 +18,6 @@ package org.springframework.core.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; -import java.lang.reflect.Array; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.util.Arrays; @@ -103,12 +102,7 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { value = synthesizeAnnotation((Annotation) value, this.attributeExtractor.getAnnotatedElement()); } else if (value instanceof Annotation[]) { - Annotation[] orig = (Annotation[]) value; - Annotation[] clone = (Annotation[]) Array.newInstance(orig.getClass().getComponentType(), orig.length); - for (int i = 0; i < orig.length; i++) { - clone[i] = synthesizeAnnotation(orig[i], this.attributeExtractor.getAnnotatedElement()); - } - value = clone; + value = synthesizeAnnotationArray((Annotation[]) value, this.attributeExtractor.getAnnotatedElement()); } this.valueCache.put(attributeName, value);