25
25
import java .util .Set ;
26
26
27
27
import org .springframework .core .BridgeMethodResolver ;
28
+ import org .springframework .util .Assert ;
28
29
import org .springframework .util .LinkedMultiValueMap ;
29
30
import org .springframework .util .MultiValueMap ;
30
31
39
40
*/
40
41
public class AnnotatedElementUtils {
41
42
43
+ /**
44
+ * @param element the annotated element; never {@code null}
45
+ * @param annotationType the fully qualified class name of the annotation
46
+ * type to find; never {@code null} or empty
47
+ */
42
48
public static Set <String > getMetaAnnotationTypes (AnnotatedElement element , String annotationType ) {
49
+ Assert .notNull (element , "AnnotatedElement must not be null" );
50
+ Assert .hasText (annotationType , "annotationType must not be null or empty" );
51
+
43
52
final Set <String > types = new LinkedHashSet <String >();
44
53
processWithGetSemantics (element , annotationType , new Processor <Object >() {
45
54
@ Override
@@ -56,7 +65,15 @@ public void postProcess(Annotation annotation, Object result) {
56
65
return (types .isEmpty () ? null : types );
57
66
}
58
67
68
+ /**
69
+ * @param element the annotated element; never {@code null}
70
+ * @param annotationType the fully qualified class name of the annotation
71
+ * type to find; never {@code null} or empty
72
+ */
59
73
public static boolean hasMetaAnnotationTypes (AnnotatedElement element , String annotationType ) {
74
+ Assert .notNull (element , "AnnotatedElement must not be null" );
75
+ Assert .hasText (annotationType , "annotationType must not be null or empty" );
76
+
60
77
return Boolean .TRUE .equals (processWithGetSemantics (element , annotationType , new Processor <Boolean >() {
61
78
@ Override
62
79
public Boolean process (Annotation annotation , int metaDepth ) {
@@ -71,7 +88,15 @@ public void postProcess(Annotation annotation, Boolean result) {
71
88
}));
72
89
}
73
90
91
+ /**
92
+ * @param element the annotated element; never {@code null}
93
+ * @param annotationType the fully qualified class name of the annotation
94
+ * type to find; never {@code null} or empty
95
+ */
74
96
public static boolean isAnnotated (AnnotatedElement element , String annotationType ) {
97
+ Assert .notNull (element , "AnnotatedElement must not be null" );
98
+ Assert .hasText (annotationType , "annotationType must not be null or empty" );
99
+
75
100
return Boolean .TRUE .equals (processWithGetSemantics (element , annotationType , new Processor <Boolean >() {
76
101
@ Override
77
102
public Boolean process (Annotation annotation , int metaDepth ) {
@@ -91,8 +116,9 @@ public void postProcess(Annotation annotation, Boolean result) {
91
116
* <p>Delegates to {@link #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean)},
92
117
* supplying {@code false} for {@code classValuesAsString} and {@code nestedAnnotationsAsMap}.
93
118
*
94
- * @param element the annotated element
95
- * @param annotationType the annotation type to find
119
+ * @param element the annotated element; never {@code null}
120
+ * @param annotationType the fully qualified class name of the annotation
121
+ * type to find; never {@code null} or empty
96
122
* @return the merged {@code AnnotationAttributes}
97
123
* @see #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
98
124
*/
@@ -105,8 +131,9 @@ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement elem
105
131
* in the annotation hierarchy of the supplied {@link AnnotatedElement},
106
132
* and merge the results into an {@link AnnotationAttributes} map.
107
133
*
108
- * @param element the annotated element
109
- * @param annotationType the annotation type to find
134
+ * @param element the annotated element; never {@code null}
135
+ * @param annotationType the fully qualified class name of the annotation
136
+ * type to find; never {@code null} or empty
110
137
* @param classValuesAsString whether to convert Class references into
111
138
* Strings or to preserve them as Class references
112
139
* @param nestedAnnotationsAsMap whether to convert nested Annotation
@@ -130,12 +157,13 @@ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement elem
130
157
* {@link #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean, boolean, boolean, boolean, boolean)},
131
158
* supplying {@code true} for all {@code search*} flags.
132
159
*
133
- * @param element the annotated element
134
- * @param annotationType the annotation type to find
160
+ * @param element the annotated element; never {@code null}
161
+ * @param annotationType the annotation type to find; never {@code null}
135
162
* @return the merged {@code AnnotationAttributes}
136
163
*/
137
164
public static AnnotationAttributes findAnnotationAttributes (AnnotatedElement element ,
138
165
Class <? extends Annotation > annotationType ) {
166
+ Assert .notNull (annotationType , "annotationType must not be null" );
139
167
return findAnnotationAttributes (element , annotationType .getName (), true , true , true , true , false , false );
140
168
}
141
169
@@ -148,8 +176,9 @@ public static AnnotationAttributes findAnnotationAttributes(AnnotatedElement ele
148
176
* {@link #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean, boolean, boolean, boolean, boolean)},
149
177
* supplying {@code true} for all {@code search*} flags.
150
178
*
151
- * @param element the annotated element
152
- * @param annotationType the annotation type to find
179
+ * @param element the annotated element; never {@code null}
180
+ * @param annotationType the fully qualified class name of the annotation
181
+ * type to find; never {@code null} or empty
153
182
* @return the merged {@code AnnotationAttributes}
154
183
*/
155
184
public static AnnotationAttributes findAnnotationAttributes (AnnotatedElement element , String annotationType ) {
@@ -161,8 +190,9 @@ public static AnnotationAttributes findAnnotationAttributes(AnnotatedElement ele
161
190
* in the annotation hierarchy of the supplied {@link AnnotatedElement},
162
191
* and merge the results into an {@link AnnotationAttributes} map.
163
192
*
164
- * @param element the annotated element
165
- * @param annotationType the annotation type to find
193
+ * @param element the annotated element; never {@code null}
194
+ * @param annotationType the fully qualified class name of the annotation
195
+ * type to find; never {@code null} or empty
166
196
* @param searchOnInterfaces whether to search on interfaces, if the
167
197
* annotated element is a class
168
198
* @param searchOnSuperclasses whether to search on superclasses, if
@@ -187,10 +217,20 @@ searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, new MergeAnnotationA
187
217
classValuesAsString , nestedAnnotationsAsMap ));
188
218
}
189
219
220
+ /**
221
+ * @param element the annotated element; never {@code null}
222
+ * @param annotationType the fully qualified class name of the annotation
223
+ * type to find; never {@code null} or empty
224
+ */
190
225
public static MultiValueMap <String , Object > getAllAnnotationAttributes (AnnotatedElement element , String annotationType ) {
191
226
return getAllAnnotationAttributes (element , annotationType , false , false );
192
227
}
193
228
229
+ /**
230
+ * @param element the annotated element; never {@code null}
231
+ * @param annotationType the fully qualified class name of the annotation
232
+ * type to find; never {@code null} or empty
233
+ */
194
234
public static MultiValueMap <String , Object > getAllAnnotationAttributes (AnnotatedElement element ,
195
235
final String annotationType , final boolean classValuesAsString , final boolean nestedAnnotationsAsMap ) {
196
236
@@ -225,8 +265,9 @@ public void postProcess(Annotation annotation, Void result) {
225
265
* Process all annotations of the specified {@code annotationType} and
226
266
* recursively all meta-annotations on the specified {@code element}.
227
267
*
228
- * @param element the annotated element
229
- * @param annotationType the annotation type to find
268
+ * @param element the annotated element; never {@code null}
269
+ * @param annotationType the fully qualified class name of the annotation
270
+ * type to find; never {@code null} or empty
230
271
* @param processor the processor to delegate to
231
272
* @return the result of the processor
232
273
*/
@@ -249,8 +290,9 @@ private static <T> T processWithGetSemantics(AnnotatedElement element, String an
249
290
* <em>present</em> on the element will have a depth of 0; a meta-annotation
250
291
* will have a depth of 1; and a meta-meta-annotation will have a depth of 2.
251
292
*
252
- * @param element the annotated element
253
- * @param annotationType the annotation type to find
293
+ * @param element the annotated element; never {@code null}
294
+ * @param annotationType the fully qualified class name of the annotation
295
+ * type to find; never {@code null} or empty
254
296
* @param processor the processor to delegate to
255
297
* @param visited the set of annotated elements that have already been visited
256
298
* @param metaDepth the depth of the annotation relative to the initial element
@@ -259,6 +301,9 @@ private static <T> T processWithGetSemantics(AnnotatedElement element, String an
259
301
private static <T > T processWithGetSemantics (AnnotatedElement element , String annotationType ,
260
302
Processor <T > processor , Set <AnnotatedElement > visited , int metaDepth ) {
261
303
304
+ Assert .notNull (element , "AnnotatedElement must not be null" );
305
+ Assert .hasText (annotationType , "annotationType must not be null or empty" );
306
+
262
307
if (visited .add (element )) {
263
308
try {
264
309
// Local annotations: declared OR inherited
@@ -304,8 +349,9 @@ private static <T> T processWithGetSemantics(AnnotatedElement element, String an
304
349
* Process all annotations of the specified {@code annotationType} and
305
350
* recursively all meta-annotations on the specified {@code element}.
306
351
*
307
- * @param element the annotated element
308
- * @param annotationType the annotation type to find
352
+ * @param element the annotated element; never {@code null}
353
+ * @param annotationType the fully qualified class name of the annotation
354
+ * type to find; never {@code null} or empty
309
355
* @param searchOnInterfaces whether to search on interfaces, if the
310
356
* annotated element is a class
311
357
* @param searchOnSuperclasses whether to search on superclasses, if
@@ -340,8 +386,9 @@ private static <T> T processWithFindSemantics(AnnotatedElement element, String a
340
386
* <em>present</em> on the element will have a depth of 0; a meta-annotation
341
387
* will have a depth of 1; and a meta-meta-annotation will have a depth of 2.
342
388
*
343
- * @param element the annotated element
344
- * @param annotationType the annotation type to find
389
+ * @param element the annotated element; never {@code null}
390
+ * @param annotationType the fully qualified class name of the annotation
391
+ * type to find; never {@code null} or empty
345
392
* @param searchOnInterfaces whether to search on interfaces, if the
346
393
* annotated element is a class
347
394
* @param searchOnSuperclasses whether to search on superclasses, if
@@ -359,6 +406,9 @@ private static <T> T processWithFindSemantics(AnnotatedElement element, String a
359
406
boolean searchOnInterfaces , boolean searchOnSuperclasses , boolean searchOnMethodsInInterfaces ,
360
407
boolean searchOnMethodsInSuperclasses , Processor <T > processor , Set <AnnotatedElement > visited , int metaDepth ) {
361
408
409
+ Assert .notNull (element , "AnnotatedElement must not be null" );
410
+ Assert .hasText (annotationType , "annotationType must not be null or empty" );
411
+
362
412
if (visited .add (element )) {
363
413
try {
364
414
0 commit comments