Skip to content

Commit f316f6a

Browse files
committed
Remove unnecessary assertions from annotation resolution code paths
Issue: SPR-16514
1 parent 3ba8582 commit f316f6a

File tree

2 files changed

+6
-83
lines changed

2 files changed

+6
-83
lines changed

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

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,6 @@
3030

3131
import org.springframework.core.BridgeMethodResolver;
3232
import org.springframework.lang.Nullable;
33-
import org.springframework.util.Assert;
3433
import org.springframework.util.LinkedMultiValueMap;
3534
import org.springframework.util.MultiValueMap;
3635

@@ -154,9 +153,6 @@ public Annotation[] getDeclaredAnnotations() {
154153
* @see #hasMetaAnnotationTypes
155154
*/
156155
public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, Class<? extends Annotation> annotationType) {
157-
Assert.notNull(element, "AnnotatedElement must not be null");
158-
Assert.notNull(annotationType, "'annotationType' must not be null");
159-
160156
return getMetaAnnotationTypes(element, element.getAnnotation(annotationType));
161157
}
162158

@@ -175,9 +171,6 @@ public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, Class
175171
* @see #hasMetaAnnotationTypes
176172
*/
177173
public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, String annotationName) {
178-
Assert.notNull(element, "AnnotatedElement must not be null");
179-
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
180-
181174
return getMetaAnnotationTypes(element, AnnotationUtils.getAnnotation(element, annotationName));
182175
}
183176

@@ -217,9 +210,6 @@ public Object process(@Nullable AnnotatedElement annotatedElement, Annotation an
217210
* @see #getMetaAnnotationTypes
218211
*/
219212
public static boolean hasMetaAnnotationTypes(AnnotatedElement element, Class<? extends Annotation> annotationType) {
220-
Assert.notNull(element, "AnnotatedElement must not be null");
221-
Assert.notNull(annotationType, "'annotationType' must not be null");
222-
223213
return hasMetaAnnotationTypes(element, annotationType, null);
224214
}
225215

@@ -236,9 +226,6 @@ public static boolean hasMetaAnnotationTypes(AnnotatedElement element, Class<? e
236226
* @see #getMetaAnnotationTypes
237227
*/
238228
public static boolean hasMetaAnnotationTypes(AnnotatedElement element, String annotationName) {
239-
Assert.notNull(element, "AnnotatedElement must not be null");
240-
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
241-
242229
return hasMetaAnnotationTypes(element, null, annotationName);
243230
}
244231

@@ -271,14 +258,10 @@ public Boolean process(@Nullable AnnotatedElement annotatedElement, Annotation a
271258
* @see #hasAnnotation(AnnotatedElement, Class)
272259
*/
273260
public static boolean isAnnotated(AnnotatedElement element, Class<? extends Annotation> annotationType) {
274-
Assert.notNull(element, "AnnotatedElement must not be null");
275-
Assert.notNull(annotationType, "'annotationType' must not be null");
276-
277261
// Shortcut: directly present on the element, with no processing needed?
278262
if (element.isAnnotationPresent(annotationType)) {
279263
return true;
280264
}
281-
282265
return Boolean.TRUE.equals(searchWithGetSemantics(element, annotationType, null, alwaysTrueAnnotationProcessor));
283266
}
284267

@@ -295,9 +278,6 @@ public static boolean isAnnotated(AnnotatedElement element, Class<? extends Anno
295278
* @return {@code true} if a matching annotation is present
296279
*/
297280
public static boolean isAnnotated(AnnotatedElement element, String annotationName) {
298-
Assert.notNull(element, "AnnotatedElement must not be null");
299-
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
300-
301281
return Boolean.TRUE.equals(searchWithGetSemantics(element, null, annotationName, alwaysTrueAnnotationProcessor));
302282
}
303283

@@ -322,7 +302,6 @@ public static boolean isAnnotated(AnnotatedElement element, String annotationNam
322302
public static AnnotationAttributes getMergedAnnotationAttributes(
323303
AnnotatedElement element, Class<? extends Annotation> annotationType) {
324304

325-
Assert.notNull(annotationType, "'annotationType' must not be null");
326305
AnnotationAttributes attributes = searchWithGetSemantics(element, annotationType, null,
327306
new MergedAnnotationAttributesProcessor());
328307
AnnotationUtils.postProcessAnnotationAttributes(element, attributes, false, false);
@@ -382,7 +361,6 @@ public static AnnotationAttributes getMergedAnnotationAttributes(AnnotatedElemen
382361
public static AnnotationAttributes getMergedAnnotationAttributes(AnnotatedElement element,
383362
String annotationName, boolean classValuesAsString, boolean nestedAnnotationsAsMap) {
384363

385-
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
386364
AnnotationAttributes attributes = searchWithGetSemantics(element, null, annotationName,
387365
new MergedAnnotationAttributesProcessor(classValuesAsString, nestedAnnotationsAsMap));
388366
AnnotationUtils.postProcessAnnotationAttributes(element, attributes, classValuesAsString, nestedAnnotationsAsMap);
@@ -409,8 +387,6 @@ public static AnnotationAttributes getMergedAnnotationAttributes(AnnotatedElemen
409387
*/
410388
@Nullable
411389
public static <A extends Annotation> A getMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
412-
Assert.notNull(annotationType, "'annotationType' must not be null");
413-
414390
// Shortcut: directly present on the element, with no merging needed?
415391
if (!(element instanceof Class)) {
416392
// Do not use this shortcut against a Class: Inherited annotations
@@ -446,12 +422,7 @@ public static <A extends Annotation> A getMergedAnnotation(AnnotatedElement elem
446422
* @see #getAllAnnotationAttributes(AnnotatedElement, String)
447423
* @see #findAllMergedAnnotations(AnnotatedElement, Class)
448424
*/
449-
public static <A extends Annotation> Set<A> getAllMergedAnnotations(AnnotatedElement element,
450-
Class<A> annotationType) {
451-
452-
Assert.notNull(element, "AnnotatedElement must not be null");
453-
Assert.notNull(annotationType, "'annotationType' must not be null");
454-
425+
public static <A extends Annotation> Set<A> getAllMergedAnnotations(AnnotatedElement element, Class<A> annotationType) {
455426
MergedAnnotationAttributesProcessor processor = new MergedAnnotationAttributesProcessor(false, false, true);
456427
searchWithGetSemantics(element, annotationType, null, processor);
457428
return postProcessAndSynthesizeAggregatedResults(element, annotationType, processor.getAggregatedResults());
@@ -516,9 +487,6 @@ public static <A extends Annotation> Set<A> getMergedRepeatableAnnotations(Annot
516487
public static <A extends Annotation> Set<A> getMergedRepeatableAnnotations(AnnotatedElement element,
517488
Class<A> annotationType, @Nullable Class<? extends Annotation> containerType) {
518489

519-
Assert.notNull(element, "AnnotatedElement must not be null");
520-
Assert.notNull(annotationType, "'annotationType' must not be null");
521-
522490
if (containerType == null) {
523491
containerType = resolveContainerType(annotationType);
524492
}
@@ -603,14 +571,10 @@ public Object process(@Nullable AnnotatedElement annotatedElement, Annotation an
603571
* @see #isAnnotated(AnnotatedElement, Class)
604572
*/
605573
public static boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationType) {
606-
Assert.notNull(element, "AnnotatedElement must not be null");
607-
Assert.notNull(annotationType, "'annotationType' must not be null");
608-
609574
// Shortcut: directly present on the element, with no processing needed?
610575
if (element.isAnnotationPresent(annotationType)) {
611576
return true;
612577
}
613-
614578
return Boolean.TRUE.equals(searchWithFindSemantics(element, annotationType, null, alwaysTrueAnnotationProcessor));
615579
}
616580

@@ -710,8 +674,6 @@ public static AnnotationAttributes findMergedAnnotationAttributes(AnnotatedEleme
710674
*/
711675
@Nullable
712676
public static <A extends Annotation> A findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
713-
Assert.notNull(annotationType, "'annotationType' must not be null");
714-
715677
// Shortcut: directly present on the element, with no merging needed?
716678
if (!(element instanceof Class)) {
717679
// Do not use this shortcut against a Class: Inherited annotations
@@ -746,12 +708,7 @@ public static <A extends Annotation> A findMergedAnnotation(AnnotatedElement ele
746708
* @see #findMergedAnnotation(AnnotatedElement, Class)
747709
* @see #getAllMergedAnnotations(AnnotatedElement, Class)
748710
*/
749-
public static <A extends Annotation> Set<A> findAllMergedAnnotations(AnnotatedElement element,
750-
Class<A> annotationType) {
751-
752-
Assert.notNull(element, "AnnotatedElement must not be null");
753-
Assert.notNull(annotationType, "'annotationType' must not be null");
754-
711+
public static <A extends Annotation> Set<A> findAllMergedAnnotations(AnnotatedElement element, Class<A> annotationType) {
755712
MergedAnnotationAttributesProcessor processor = new MergedAnnotationAttributesProcessor(false, false, true);
756713
searchWithFindSemantics(element, annotationType, null, processor);
757714
return postProcessAndSynthesizeAggregatedResults(element, annotationType, processor.getAggregatedResults());
@@ -816,9 +773,6 @@ public static <A extends Annotation> Set<A> findMergedRepeatableAnnotations(Anno
816773
public static <A extends Annotation> Set<A> findMergedRepeatableAnnotations(AnnotatedElement element,
817774
Class<A> annotationType, @Nullable Class<? extends Annotation> containerType) {
818775

819-
Assert.notNull(element, "AnnotatedElement must not be null");
820-
Assert.notNull(annotationType, "'annotationType' must not be null");
821-
822776
if (containerType == null) {
823777
containerType = resolveContainerType(annotationType);
824778
}
@@ -902,8 +856,6 @@ private static <T> T searchWithGetSemantics(AnnotatedElement element,
902856
@Nullable Class<? extends Annotation> containerType, Processor<T> processor,
903857
Set<AnnotatedElement> visited, int metaDepth) {
904858

905-
Assert.notNull(element, "AnnotatedElement must not be null");
906-
907859
if (visited.add(element)) {
908860
try {
909861
// Start searching within locally declared annotations
@@ -1096,8 +1048,6 @@ private static <T> T searchWithFindSemantics(AnnotatedElement element,
10961048
@Nullable Class<? extends Annotation> containerType, Processor<T> processor,
10971049
Set<AnnotatedElement> visited, int metaDepth) {
10981050

1099-
Assert.notNull(element, "AnnotatedElement must not be null");
1100-
11011051
if (visited.add(element)) {
11021052
try {
11031053
// Locally declared annotations (ignoring @Inherited)

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

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -439,9 +439,6 @@ public static <A extends Annotation> Set<A> getDeclaredRepeatableAnnotations(Ann
439439
private static <A extends Annotation> Set<A> getRepeatableAnnotations(AnnotatedElement annotatedElement,
440440
Class<A> annotationType, @Nullable Class<? extends Annotation> containerAnnotationType, boolean declaredMode) {
441441

442-
Assert.notNull(annotatedElement, "AnnotatedElement must not be null");
443-
Assert.notNull(annotationType, "Annotation type must not be null");
444-
445442
try {
446443
if (annotatedElement instanceof Method) {
447444
annotatedElement = BridgeMethodResolver.findBridgedMethod((Method) annotatedElement);
@@ -472,8 +469,6 @@ private static <A extends Annotation> Set<A> getRepeatableAnnotations(AnnotatedE
472469
*/
473470
@Nullable
474471
public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
475-
Assert.notNull(annotatedElement, "AnnotatedElement must not be null");
476-
477472
// Do NOT store result in the findAnnotationCache since doing so could break
478473
// findAnnotation(Class, Class) and findAnnotation(Method, Class).
479474
A ann = findAnnotation(annotatedElement, annotationType, new HashSet<>());
@@ -745,7 +740,6 @@ private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A>
745740
*/
746741
@Nullable
747742
public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, @Nullable Class<?> clazz) {
748-
Assert.notNull(annotationType, "Annotation type must not be null");
749743
if (clazz == null || Object.class == clazz) {
750744
return null;
751745
}
@@ -781,7 +775,6 @@ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation>
781775
*/
782776
@Nullable
783777
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, @Nullable Class<?> clazz) {
784-
Assert.notEmpty(annotationTypes, "List of annotation types must not be empty");
785778
if (clazz == null || Object.class == clazz) {
786779
return null;
787780
}
@@ -812,8 +805,6 @@ public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends
812805
* @see #isAnnotationInherited(Class, Class)
813806
*/
814807
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
815-
Assert.notNull(annotationType, "Annotation type must not be null");
816-
Assert.notNull(clazz, "Class must not be null");
817808
try {
818809
return (clazz.getDeclaredAnnotation(annotationType) != null);
819810
}
@@ -843,8 +834,6 @@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> an
843834
* @see #isAnnotationDeclaredLocally(Class, Class)
844835
*/
845836
public static boolean isAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz) {
846-
Assert.notNull(annotationType, "Annotation type must not be null");
847-
Assert.notNull(clazz, "Class must not be null");
848837
return (clazz.isAnnotationPresent(annotationType) && !isAnnotationDeclaredLocally(annotationType, clazz));
849838
}
850839

@@ -1444,7 +1433,6 @@ public static <A extends Annotation> A synthesizeAnnotation(
14441433

14451434
@SuppressWarnings("unchecked")
14461435
static <A extends Annotation> A synthesizeAnnotation(A annotation, @Nullable Object annotatedElement) {
1447-
Assert.notNull(annotation, "Annotation must not be null");
14481436
if (annotation instanceof SynthesizedAnnotation) {
14491437
return annotation;
14501438
}
@@ -1497,9 +1485,6 @@ static <A extends Annotation> A synthesizeAnnotation(A annotation, @Nullable Obj
14971485
public static <A extends Annotation> A synthesizeAnnotation(Map<String, Object> attributes,
14981486
Class<A> annotationType, @Nullable AnnotatedElement annotatedElement) {
14991487

1500-
Assert.notNull(attributes, "'attributes' must not be null");
1501-
Assert.notNull(annotationType, "'annotationType' must not be null");
1502-
15031488
MapAnnotationAttributeExtractor attributeExtractor =
15041489
new MapAnnotationAttributeExtractor(attributes, annotationType, annotatedElement);
15051490
InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(attributeExtractor);
@@ -1574,7 +1559,6 @@ static Annotation[] synthesizeAnnotationArray(
15741559
@SuppressWarnings("unchecked")
15751560
@Nullable
15761561
static <A extends Annotation> A[] synthesizeAnnotationArray(@Nullable Map<String, Object>[] maps, Class<A> annotationType) {
1577-
Assert.notNull(annotationType, "'annotationType' must not be null");
15781562
if (maps == null) {
15791563
return null;
15801564
}
@@ -1703,7 +1687,6 @@ else if (Annotation.class.isAssignableFrom(returnType)) {
17031687
* @see #getAttributeOverrideName(Method, Class)
17041688
*/
17051689
static List<String> getAttributeAliasNames(Method attribute) {
1706-
Assert.notNull(attribute, "attribute must not be null");
17071690
AliasDescriptor descriptor = AliasDescriptor.from(attribute);
17081691
return (descriptor != null ? descriptor.getAttributeAliasNames() : Collections.<String> emptyList());
17091692
}
@@ -1726,13 +1709,9 @@ static List<String> getAttributeAliasNames(Method attribute) {
17261709
*/
17271710
@Nullable
17281711
static String getAttributeOverrideName(Method attribute, @Nullable Class<? extends Annotation> metaAnnotationType) {
1729-
Assert.notNull(attribute, "attribute must not be null");
1730-
Assert.notNull(metaAnnotationType, "metaAnnotationType must not be null");
1731-
Assert.isTrue(Annotation.class != metaAnnotationType,
1732-
"metaAnnotationType must not be [java.lang.annotation.Annotation]");
1733-
17341712
AliasDescriptor descriptor = AliasDescriptor.from(attribute);
1735-
return (descriptor != null ? descriptor.getAttributeOverrideName(metaAnnotationType) : null);
1713+
return (descriptor != null && metaAnnotationType != null ?
1714+
descriptor.getAttributeOverrideName(metaAnnotationType) : null);
17361715
}
17371716

17381717
/**
@@ -2043,7 +2022,6 @@ public static AliasDescriptor from(Method attribute) {
20432022
@SuppressWarnings("unchecked")
20442023
private AliasDescriptor(Method sourceAttribute, AliasFor aliasFor) {
20452024
Class<?> declaringClass = sourceAttribute.getDeclaringClass();
2046-
Assert.isTrue(declaringClass.isAnnotation(), "sourceAttribute must be from an annotation");
20472025

20482026
this.sourceAttribute = sourceAttribute;
20492027
this.sourceAnnotationType = (Class<? extends Annotation>) declaringClass;
@@ -2117,7 +2095,6 @@ private void validate() {
21172095
}
21182096

21192097
private void validateDefaultValueConfiguration(Method aliasedAttribute) {
2120-
Assert.notNull(aliasedAttribute, "aliasedAttribute must not be null");
21212098
Object defaultValue = this.sourceAttribute.getDefaultValue();
21222099
Object aliasedDefaultValue = aliasedAttribute.getDefaultValue();
21232100

@@ -2211,10 +2188,6 @@ private List<AliasDescriptor> getOtherDescriptors() {
22112188

22122189
@Nullable
22132190
public String getAttributeOverrideName(Class<? extends Annotation> metaAnnotationType) {
2214-
Assert.notNull(metaAnnotationType, "metaAnnotationType must not be null");
2215-
Assert.isTrue(Annotation.class != metaAnnotationType,
2216-
"metaAnnotationType must not be [java.lang.annotation.Annotation]");
2217-
22182191
// Search the attribute override hierarchy, starting with the current attribute
22192192
for (AliasDescriptor desc = this; desc != null; desc = desc.getAttributeOverrideDescriptor()) {
22202193
if (desc.isOverrideFor(metaAnnotationType)) {

0 commit comments

Comments
 (0)