Skip to content

Commit 25187ee

Browse files
committed
Document AnnotatedElementUtils & assert preconditions
This commit consistently documents the 'element' and 'annotationType' method arguments throughout AnnotatedElementUtils. In addition, this commit introduces assertions against preconditions for all 'element' and 'annotationType' method arguments. Issue: SPR-11514
1 parent 1a6aeb1 commit 25187ee

File tree

1 file changed

+68
-18
lines changed

1 file changed

+68
-18
lines changed

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

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Set;
2626

2727
import org.springframework.core.BridgeMethodResolver;
28+
import org.springframework.util.Assert;
2829
import org.springframework.util.LinkedMultiValueMap;
2930
import org.springframework.util.MultiValueMap;
3031

@@ -39,7 +40,15 @@
3940
*/
4041
public class AnnotatedElementUtils {
4142

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+
*/
4248
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+
4352
final Set<String> types = new LinkedHashSet<String>();
4453
processWithGetSemantics(element, annotationType, new Processor<Object>() {
4554
@Override
@@ -56,7 +65,15 @@ public void postProcess(Annotation annotation, Object result) {
5665
return (types.isEmpty() ? null : types);
5766
}
5867

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+
*/
5973
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+
6077
return Boolean.TRUE.equals(processWithGetSemantics(element, annotationType, new Processor<Boolean>() {
6178
@Override
6279
public Boolean process(Annotation annotation, int metaDepth) {
@@ -71,7 +88,15 @@ public void postProcess(Annotation annotation, Boolean result) {
7188
}));
7289
}
7390

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+
*/
7496
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+
75100
return Boolean.TRUE.equals(processWithGetSemantics(element, annotationType, new Processor<Boolean>() {
76101
@Override
77102
public Boolean process(Annotation annotation, int metaDepth) {
@@ -91,8 +116,9 @@ public void postProcess(Annotation annotation, Boolean result) {
91116
* <p>Delegates to {@link #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean)},
92117
* supplying {@code false} for {@code classValuesAsString} and {@code nestedAnnotationsAsMap}.
93118
*
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
96122
* @return the merged {@code AnnotationAttributes}
97123
* @see #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
98124
*/
@@ -105,8 +131,9 @@ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement elem
105131
* in the annotation hierarchy of the supplied {@link AnnotatedElement},
106132
* and merge the results into an {@link AnnotationAttributes} map.
107133
*
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
110137
* @param classValuesAsString whether to convert Class references into
111138
* Strings or to preserve them as Class references
112139
* @param nestedAnnotationsAsMap whether to convert nested Annotation
@@ -130,12 +157,13 @@ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement elem
130157
* {@link #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean, boolean, boolean, boolean, boolean)},
131158
* supplying {@code true} for all {@code search*} flags.
132159
*
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}
135162
* @return the merged {@code AnnotationAttributes}
136163
*/
137164
public static AnnotationAttributes findAnnotationAttributes(AnnotatedElement element,
138165
Class<? extends Annotation> annotationType) {
166+
Assert.notNull(annotationType, "annotationType must not be null");
139167
return findAnnotationAttributes(element, annotationType.getName(), true, true, true, true, false, false);
140168
}
141169

@@ -148,8 +176,9 @@ public static AnnotationAttributes findAnnotationAttributes(AnnotatedElement ele
148176
* {@link #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean, boolean, boolean, boolean, boolean)},
149177
* supplying {@code true} for all {@code search*} flags.
150178
*
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
153182
* @return the merged {@code AnnotationAttributes}
154183
*/
155184
public static AnnotationAttributes findAnnotationAttributes(AnnotatedElement element, String annotationType) {
@@ -161,8 +190,9 @@ public static AnnotationAttributes findAnnotationAttributes(AnnotatedElement ele
161190
* in the annotation hierarchy of the supplied {@link AnnotatedElement},
162191
* and merge the results into an {@link AnnotationAttributes} map.
163192
*
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
166196
* @param searchOnInterfaces whether to search on interfaces, if the
167197
* annotated element is a class
168198
* @param searchOnSuperclasses whether to search on superclasses, if
@@ -187,10 +217,20 @@ searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, new MergeAnnotationA
187217
classValuesAsString, nestedAnnotationsAsMap));
188218
}
189219

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+
*/
190225
public static MultiValueMap<String, Object> getAllAnnotationAttributes(AnnotatedElement element, String annotationType) {
191226
return getAllAnnotationAttributes(element, annotationType, false, false);
192227
}
193228

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+
*/
194234
public static MultiValueMap<String, Object> getAllAnnotationAttributes(AnnotatedElement element,
195235
final String annotationType, final boolean classValuesAsString, final boolean nestedAnnotationsAsMap) {
196236

@@ -225,8 +265,9 @@ public void postProcess(Annotation annotation, Void result) {
225265
* Process all annotations of the specified {@code annotationType} and
226266
* recursively all meta-annotations on the specified {@code element}.
227267
*
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
230271
* @param processor the processor to delegate to
231272
* @return the result of the processor
232273
*/
@@ -249,8 +290,9 @@ private static <T> T processWithGetSemantics(AnnotatedElement element, String an
249290
* <em>present</em> on the element will have a depth of 0; a meta-annotation
250291
* will have a depth of 1; and a meta-meta-annotation will have a depth of 2.
251292
*
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
254296
* @param processor the processor to delegate to
255297
* @param visited the set of annotated elements that have already been visited
256298
* @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
259301
private static <T> T processWithGetSemantics(AnnotatedElement element, String annotationType,
260302
Processor<T> processor, Set<AnnotatedElement> visited, int metaDepth) {
261303

304+
Assert.notNull(element, "AnnotatedElement must not be null");
305+
Assert.hasText(annotationType, "annotationType must not be null or empty");
306+
262307
if (visited.add(element)) {
263308
try {
264309
// Local annotations: declared OR inherited
@@ -304,8 +349,9 @@ private static <T> T processWithGetSemantics(AnnotatedElement element, String an
304349
* Process all annotations of the specified {@code annotationType} and
305350
* recursively all meta-annotations on the specified {@code element}.
306351
*
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
309355
* @param searchOnInterfaces whether to search on interfaces, if the
310356
* annotated element is a class
311357
* @param searchOnSuperclasses whether to search on superclasses, if
@@ -340,8 +386,9 @@ private static <T> T processWithFindSemantics(AnnotatedElement element, String a
340386
* <em>present</em> on the element will have a depth of 0; a meta-annotation
341387
* will have a depth of 1; and a meta-meta-annotation will have a depth of 2.
342388
*
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
345392
* @param searchOnInterfaces whether to search on interfaces, if the
346393
* annotated element is a class
347394
* @param searchOnSuperclasses whether to search on superclasses, if
@@ -359,6 +406,9 @@ private static <T> T processWithFindSemantics(AnnotatedElement element, String a
359406
boolean searchOnInterfaces, boolean searchOnSuperclasses, boolean searchOnMethodsInInterfaces,
360407
boolean searchOnMethodsInSuperclasses, Processor<T> processor, Set<AnnotatedElement> visited, int metaDepth) {
361408

409+
Assert.notNull(element, "AnnotatedElement must not be null");
410+
Assert.hasText(annotationType, "annotationType must not be null or empty");
411+
362412
if (visited.add(element)) {
363413
try {
364414

0 commit comments

Comments
 (0)