Skip to content

Commit 1afc938

Browse files
committed
Introduce synthesizeAnnotationArray() in AnnotationUtils
1 parent 9f71787 commit 1afc938

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.AnnotatedElement;
21+
import java.lang.reflect.Array;
2122
import java.lang.reflect.InvocationHandler;
2223
import java.lang.reflect.InvocationTargetException;
2324
import java.lang.reflect.Method;
@@ -926,10 +927,7 @@ else if (value instanceof Class[]) {
926927
return mappedAnnotations;
927928
}
928929
else {
929-
for (int i = 0; i < annotations.length; i++) {
930-
annotations[i] = synthesizeAnnotation(annotations[i], annotatedElement);
931-
}
932-
return annotations;
930+
return synthesizeAnnotationArray(annotations, annotatedElement);
933931
}
934932
}
935933

@@ -1124,6 +1122,35 @@ public static <A extends Annotation> A synthesizeAnnotation(Map<String, Object>
11241122
return synthesizedAnnotation;
11251123
}
11261124

1125+
/**
1126+
* <em>Synthesize</em> the supplied array of {@code annotations} by
1127+
* creating a new array of the same size and type and populating it
1128+
* with {@linkplain #synthesizeAnnotation(Annotation) synthesized}
1129+
* versions of the annotations from the input array.
1130+
*
1131+
* @param annotations the array of annotations to synthesize
1132+
* @param annotatedElement the element that is annotated with the supplied
1133+
* array of annotations; may be {@code null} if unknown
1134+
* @return a new array of synthesized annotations, or {@code null} if
1135+
* the supplied array is {@code null}
1136+
* @throws AnnotationConfigurationException if invalid configuration of
1137+
* {@code @AliasFor} is detected
1138+
* @since 4.2
1139+
* @see #synthesizeAnnotation(Annotation, AnnotatedElement)
1140+
* @see #synthesizeAnnotation(Map, Class, AnnotatedElement)
1141+
*/
1142+
public static Annotation[] synthesizeAnnotationArray(Annotation[] annotations, AnnotatedElement annotatedElement) {
1143+
if (annotations == null) {
1144+
return null;
1145+
}
1146+
1147+
Annotation[] synthesized = (Annotation[]) Array.newInstance(annotations.getClass().getComponentType(), annotations.length);
1148+
for (int i = 0; i < annotations.length; i++) {
1149+
synthesized[i] = synthesizeAnnotation(annotations[i], annotatedElement);
1150+
}
1151+
return synthesized;
1152+
}
1153+
11271154
/**
11281155
* Get a map of all attribute alias pairs, declared via {@code @AliasFor}
11291156
* in the supplied annotation type.

spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.AnnotatedElement;
21-
import java.lang.reflect.Array;
2221
import java.lang.reflect.InvocationHandler;
2322
import java.lang.reflect.Method;
2423
import java.util.Arrays;
@@ -103,12 +102,7 @@ private Object getAttributeValue(Method attributeMethod) {
103102
value = synthesizeAnnotation((Annotation) value, this.attributeExtractor.getAnnotatedElement());
104103
}
105104
else if (value instanceof Annotation[]) {
106-
Annotation[] orig = (Annotation[]) value;
107-
Annotation[] clone = (Annotation[]) Array.newInstance(orig.getClass().getComponentType(), orig.length);
108-
for (int i = 0; i < orig.length; i++) {
109-
clone[i] = synthesizeAnnotation(orig[i], this.attributeExtractor.getAnnotatedElement());
110-
}
111-
value = clone;
105+
value = synthesizeAnnotationArray((Annotation[]) value, this.attributeExtractor.getAnnotatedElement());
112106
}
113107

114108
this.valueCache.put(attributeName, value);

0 commit comments

Comments
 (0)