Skip to content

Commit 3bc27e8

Browse files
committed
Streamline AnnotationFilter usage with the MergedAnnotations API
MergedAnnotations provides 'from' variants with RepeatableContainers but without AnnotationFilter argument now, avoiding the need to refer to AnnotationFilter.PLAIN as a default at call sites.
1 parent c8f430e commit 3bc27e8

File tree

8 files changed

+100
-52
lines changed

8 files changed

+100
-52
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import org.springframework.core.annotation.AnnotationUtils;
6363
import org.springframework.core.annotation.MergedAnnotation;
6464
import org.springframework.core.annotation.MergedAnnotations;
65-
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
6665
import org.springframework.lang.Nullable;
6766
import org.springframework.util.Assert;
6867
import org.springframework.util.ClassUtils;
@@ -515,7 +514,7 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
515514

516515
@Nullable
517516
private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) {
518-
MergedAnnotations annotations = MergedAnnotations.from(ao, SearchStrategy.INHERITED_ANNOTATIONS);
517+
MergedAnnotations annotations = MergedAnnotations.from(ao);
519518
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
520519
MergedAnnotation<?> annotation = annotations.get(type);
521520
if (annotation.isPresent()) {
@@ -533,7 +532,7 @@ private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) {
533532
* @param ann the Autowired annotation
534533
* @return whether the annotation indicates that a dependency is required
535534
*/
536-
@SuppressWarnings({ "deprecation", "cast" })
535+
@SuppressWarnings({"deprecation", "cast"})
537536
protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
538537
// The following (AnnotationAttributes) cast is required on JDK 9+.
539538
return determineRequiredStatus((AnnotationAttributes)

spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.beans.factory.BeanFactoryAware;
3636
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3737
import org.springframework.beans.factory.config.EmbeddedValueResolver;
38-
import org.springframework.core.annotation.AnnotationFilter;
3938
import org.springframework.core.annotation.MergedAnnotation;
4039
import org.springframework.core.annotation.MergedAnnotationPredicates;
4140
import org.springframework.core.annotation.MergedAnnotations;
@@ -168,7 +167,7 @@ private static List<MergedAnnotation<? extends Annotation>> getRepeatableAnnotat
168167
Class<? extends Annotation> containerAnnotationType) {
169168

170169
return MergedAnnotations.from(annotatedElement, SearchStrategy.TYPE_HIERARCHY,
171-
RepeatableContainers.of(annotationType, containerAnnotationType), AnnotationFilter.PLAIN)
170+
RepeatableContainers.of(annotationType, containerAnnotationType))
172171
.stream(annotationType)
173172
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
174173
.map(MergedAnnotation::withNonMergedAttributes)

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -750,29 +750,25 @@ public static <A extends Annotation> Set<A> findMergedRepeatableAnnotations(Anno
750750
}
751751

752752
private static MergedAnnotations getAnnotations(AnnotatedElement element) {
753-
return MergedAnnotations.from(element, SearchStrategy.INHERITED_ANNOTATIONS,
754-
RepeatableContainers.none(), AnnotationFilter.PLAIN);
753+
return MergedAnnotations.from(element, SearchStrategy.INHERITED_ANNOTATIONS, RepeatableContainers.none());
755754
}
756755

757756
private static MergedAnnotations getRepeatableAnnotations(AnnotatedElement element,
758757
@Nullable Class<? extends Annotation> containerType, Class<? extends Annotation> annotationType) {
759758

760759
RepeatableContainers repeatableContainers = RepeatableContainers.of(annotationType, containerType);
761-
return MergedAnnotations.from(element, SearchStrategy.INHERITED_ANNOTATIONS,
762-
repeatableContainers, AnnotationFilter.PLAIN);
760+
return MergedAnnotations.from(element, SearchStrategy.INHERITED_ANNOTATIONS, repeatableContainers);
763761
}
764762

765763
private static MergedAnnotations findAnnotations(AnnotatedElement element) {
766-
return MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY,
767-
RepeatableContainers.none(), AnnotationFilter.PLAIN);
764+
return MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY, RepeatableContainers.none());
768765
}
769766

770767
private static MergedAnnotations findRepeatableAnnotations(AnnotatedElement element,
771768
@Nullable Class<? extends Annotation> containerType, Class<? extends Annotation> annotationType) {
772769

773770
RepeatableContainers repeatableContainers = RepeatableContainers.of(annotationType, containerType);
774-
return MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY,
775-
repeatableContainers, AnnotationFilter.PLAIN);
771+
return MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY, repeatableContainers);
776772
}
777773

778774
@Nullable

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,42 @@ public interface AnnotationFilter {
4040
*/
4141
AnnotationFilter JAVA = packages("java", "javax");
4242

43+
/**
44+
* {@link AnnotationFilter} that always matches and can be used when no
45+
* relevant annotation types are expected to present at all.
46+
*/
47+
AnnotationFilter ALL = new AnnotationFilter() {
48+
@Override
49+
public boolean matches(Annotation annotation) {
50+
return true;
51+
}
52+
@Override
53+
public boolean matches(Class<?> type) {
54+
return true;
55+
}
56+
@Override
57+
public boolean matches(String typeName) {
58+
return true;
59+
}
60+
@Override
61+
public String toString() {
62+
return "All annotations filtered";
63+
}
64+
};
65+
4366
/**
4467
* {@link AnnotationFilter} that never matches and can be used when no
45-
* filtering is needed.
68+
* filtering is needed (allowing for any annotation types to be present).
4669
*/
4770
AnnotationFilter NONE = new AnnotationFilter() {
71+
@Override
72+
public boolean matches(Annotation annotation) {
73+
return false;
74+
}
75+
@Override
76+
public boolean matches(Class<?> type) {
77+
return false;
78+
}
4879
@Override
4980
public boolean matches(String typeName) {
5081
return false;

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public abstract class AnnotationUtils {
112112
private static final AnnotationFilter JAVA_LANG_ANNOTATION_FILTER =
113113
AnnotationFilter.packages("java.lang.annotation");
114114

115-
private static Map<Class<? extends Annotation>, Map<String, DefaultValueHolder>> defaultValuesCache =
115+
private static final Map<Class<? extends Annotation>, Map<String, DefaultValueHolder>> defaultValuesCache =
116116
new ConcurrentReferenceHashMap<>();
117117

118118

@@ -126,6 +126,7 @@ public abstract class AnnotationUtils {
126126
* if {@code true} is being returned here.
127127
* @since 5.2
128128
* @see #isCandidateClass(Class, Class)
129+
* @see #isCandidateClass(Class, String)
129130
*/
130131
public static boolean isCandidateClass(Class<?> clazz, Collection<Class<? extends Annotation>> annotationTypes) {
131132
for (Class<? extends Annotation> annotationType : annotationTypes) {
@@ -160,6 +161,7 @@ public static boolean isCandidateClass(Class<?> clazz, Class<? extends Annotatio
160161
* {@code true} otherwise. Callers will usually perform full method/field introspection
161162
* if {@code true} is being returned here.
162163
* @since 5.2
164+
* @see #isCandidateClass(Class, Class)
163165
*/
164166
public static boolean isCandidateClass(Class<?> clazz, String annotationName) {
165167
if (annotationName.startsWith("java.")) {
@@ -196,8 +198,7 @@ public static <A extends Annotation> A getAnnotation(Annotation annotation, Clas
196198
return null;
197199
}
198200
// Exhaustive retrieval of merged annotations...
199-
return MergedAnnotations.from(null, new Annotation[] {annotation},
200-
RepeatableContainers.none(), AnnotationFilter.PLAIN)
201+
return MergedAnnotations.from(annotation, new Annotation[] {annotation}, RepeatableContainers.none())
201202
.get(annotationType).withNonMergedAttributes()
202203
.synthesize(AnnotationUtils::isSingleLevelPresent).orElse(null);
203204
}
@@ -222,8 +223,7 @@ public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedE
222223
return annotatedElement.getAnnotation(annotationType);
223224
}
224225
// Exhaustive retrieval of merged annotations...
225-
return MergedAnnotations.from(annotatedElement, SearchStrategy.INHERITED_ANNOTATIONS,
226-
RepeatableContainers.none(), AnnotationFilter.PLAIN)
226+
return MergedAnnotations.from(annotatedElement, SearchStrategy.INHERITED_ANNOTATIONS, RepeatableContainers.none())
227227
.get(annotationType).withNonMergedAttributes()
228228
.synthesize(AnnotationUtils::isSingleLevelPresent).orElse(null);
229229
}
@@ -375,8 +375,7 @@ public static <A extends Annotation> Set<A> getRepeatableAnnotations(AnnotatedEl
375375
RepeatableContainers repeatableContainers = (containerAnnotationType != null ?
376376
RepeatableContainers.of(annotationType, containerAnnotationType) :
377377
RepeatableContainers.standardRepeatables());
378-
return MergedAnnotations.from(annotatedElement, SearchStrategy.SUPERCLASS,
379-
repeatableContainers, AnnotationFilter.PLAIN)
378+
return MergedAnnotations.from(annotatedElement, SearchStrategy.SUPERCLASS, repeatableContainers)
380379
.stream(annotationType)
381380
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
382381
.map(MergedAnnotation::withNonMergedAttributes)
@@ -457,8 +456,8 @@ public static <A extends Annotation> Set<A> getDeclaredRepeatableAnnotations(Ann
457456
RepeatableContainers repeatableContainers = containerAnnotationType != null ?
458457
RepeatableContainers.of(annotationType, containerAnnotationType) :
459458
RepeatableContainers.standardRepeatables();
460-
return MergedAnnotations.from(annotatedElement, SearchStrategy.DIRECT,
461-
repeatableContainers, AnnotationFilter.PLAIN).stream(annotationType)
459+
return MergedAnnotations.from(annotatedElement, SearchStrategy.DIRECT, repeatableContainers)
460+
.stream(annotationType)
462461
.map(MergedAnnotation::withNonMergedAttributes)
463462
.collect(MergedAnnotationCollectors.toAnnotationSet());
464463
}
@@ -492,8 +491,7 @@ public static <A extends Annotation> A findAnnotation(
492491
return annotatedElement.getDeclaredAnnotation(annotationType);
493492
}
494493
// Exhaustive retrieval of merged annotations...
495-
return MergedAnnotations.from(annotatedElement, SearchStrategy.INHERITED_ANNOTATIONS,
496-
RepeatableContainers.none(), AnnotationFilter.PLAIN)
494+
return MergedAnnotations.from(annotatedElement, SearchStrategy.INHERITED_ANNOTATIONS, RepeatableContainers.none())
497495
.get(annotationType).withNonMergedAttributes()
498496
.synthesize(MergedAnnotation::isPresent).orElse(null);
499497
}
@@ -524,8 +522,7 @@ public static <A extends Annotation> A findAnnotation(Method method, @Nullable C
524522
return method.getDeclaredAnnotation(annotationType);
525523
}
526524
// Exhaustive retrieval of merged annotations...
527-
return MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY,
528-
RepeatableContainers.none(), AnnotationFilter.PLAIN)
525+
return MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY, RepeatableContainers.none())
529526
.get(annotationType).withNonMergedAttributes()
530527
.synthesize(MergedAnnotation::isPresent).orElse(null);
531528
}
@@ -563,8 +560,7 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable
563560
return clazz.getDeclaredAnnotation(annotationType);
564561
}
565562
// Exhaustive retrieval of merged annotations...
566-
return MergedAnnotations.from(clazz, SearchStrategy.TYPE_HIERARCHY,
567-
RepeatableContainers.none(), AnnotationFilter.PLAIN)
563+
return MergedAnnotations.from(clazz, SearchStrategy.TYPE_HIERARCHY, RepeatableContainers.none())
568564
.get(annotationType).withNonMergedAttributes()
569565
.synthesize(MergedAnnotation::isPresent).orElse(null);
570566
}
@@ -714,7 +710,7 @@ public static boolean isAnnotationMetaPresent(Class<? extends Annotation> annota
714710
}
715711
// Exhaustive retrieval of merged annotations...
716712
return MergedAnnotations.from(annotationType, SearchStrategy.INHERITED_ANNOTATIONS,
717-
RepeatableContainers.none(), AnnotationFilter.PLAIN).isPresent(metaAnnotationType);
713+
RepeatableContainers.none()).isPresent(metaAnnotationType);
718714
}
719715

720716
/**

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
*
5454
* &#064;AliasFor(attribute = "value")
5555
* String[] path() default {};
56-
*
5756
* }
5857
* </pre>
5958
*
@@ -303,7 +302,24 @@ static MergedAnnotations from(AnnotatedElement element) {
303302
* element annotations
304303
*/
305304
static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStrategy) {
306-
return from(element, searchStrategy, RepeatableContainers.standardRepeatables(), AnnotationFilter.PLAIN);
305+
return from(element, searchStrategy, RepeatableContainers.standardRepeatables());
306+
}
307+
308+
/**
309+
* Create a new {@link MergedAnnotations} instance containing all
310+
* annotations and meta-annotations from the specified element and,
311+
* depending on the {@link SearchStrategy}, related inherited elements.
312+
* @param element the source element
313+
* @param searchStrategy the search strategy to use
314+
* @param repeatableContainers the repeatable containers that may be used by
315+
* the element annotations or the meta-annotations
316+
* @return a {@link MergedAnnotations} instance containing the merged
317+
* element annotations
318+
*/
319+
static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStrategy,
320+
RepeatableContainers repeatableContainers) {
321+
322+
return TypeMappedAnnotations.from(element, searchStrategy, repeatableContainers, AnnotationFilter.PLAIN);
307323
}
308324

309325
/**
@@ -333,7 +349,7 @@ static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStr
333349
* @see #from(Object, Annotation...)
334350
*/
335351
static MergedAnnotations from(Annotation... annotations) {
336-
return from(null, annotations);
352+
return from(annotations, annotations);
337353
}
338354

339355
/**
@@ -347,8 +363,23 @@ static MergedAnnotations from(Annotation... annotations) {
347363
* @see #from(Annotation...)
348364
* @see #from(AnnotatedElement)
349365
*/
350-
static MergedAnnotations from(@Nullable Object source, Annotation... annotations) {
351-
return from(source, annotations, RepeatableContainers.standardRepeatables(), AnnotationFilter.PLAIN);
366+
static MergedAnnotations from(Object source, Annotation... annotations) {
367+
return from(source, annotations, RepeatableContainers.standardRepeatables());
368+
}
369+
370+
/**
371+
* Create a new {@link MergedAnnotations} instance from the specified
372+
* annotations.
373+
* @param source the source for the annotations. This source is used only
374+
* for information and logging. It does not need to <em>actually</em>
375+
* contain the specified annotations, and it will not be searched.
376+
* @param annotations the annotations to include
377+
* @param repeatableContainers the repeatable containers that may be used by
378+
* meta-annotations
379+
* @return a {@link MergedAnnotations} instance containing the annotations
380+
*/
381+
static MergedAnnotations from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers) {
382+
return TypeMappedAnnotations.from(source, annotations, repeatableContainers, AnnotationFilter.PLAIN);
352383
}
353384

354385
/**
@@ -364,7 +395,7 @@ static MergedAnnotations from(@Nullable Object source, Annotation... annotations
364395
* annotations considered
365396
* @return a {@link MergedAnnotations} instance containing the annotations
366397
*/
367-
static MergedAnnotations from(@Nullable Object source, Annotation[] annotations,
398+
static MergedAnnotations from(Object source, Annotation[] annotations,
368399
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) {
369400

370401
return TypeMappedAnnotations.from(source, annotations, repeatableContainers, annotationFilter);

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@
4040
*/
4141
final class TypeMappedAnnotations implements MergedAnnotations {
4242

43-
private static final AnnotationFilter FILTER_ALL = (annotationType -> true);
44-
4543
/**
4644
* Shared instance that can be used when there are no annotations.
4745
*/
4846
static final MergedAnnotations NONE = new TypeMappedAnnotations(
49-
null, new Annotation[0], RepeatableContainers.none(), FILTER_ALL);
47+
null, new Annotation[0], RepeatableContainers.none(), AnnotationFilter.ALL);
5048

5149

5250
@Nullable
@@ -180,39 +178,39 @@ public <A extends Annotation> MergedAnnotation<A> get(String annotationType,
180178

181179
@Override
182180
public <A extends Annotation> Stream<MergedAnnotation<A>> stream(Class<A> annotationType) {
183-
if (this.annotationFilter == FILTER_ALL) {
181+
if (this.annotationFilter == AnnotationFilter.ALL) {
184182
return Stream.empty();
185183
}
186184
return StreamSupport.stream(spliterator(annotationType), false);
187185
}
188186

189187
@Override
190188
public <A extends Annotation> Stream<MergedAnnotation<A>> stream(String annotationType) {
191-
if (this.annotationFilter == FILTER_ALL) {
189+
if (this.annotationFilter == AnnotationFilter.ALL) {
192190
return Stream.empty();
193191
}
194192
return StreamSupport.stream(spliterator(annotationType), false);
195193
}
196194

197195
@Override
198196
public Stream<MergedAnnotation<Annotation>> stream() {
199-
if (this.annotationFilter == FILTER_ALL) {
197+
if (this.annotationFilter == AnnotationFilter.ALL) {
200198
return Stream.empty();
201199
}
202200
return StreamSupport.stream(spliterator(), false);
203201
}
204202

205203
@Override
206204
public Iterator<MergedAnnotation<Annotation>> iterator() {
207-
if (this.annotationFilter == FILTER_ALL) {
205+
if (this.annotationFilter == AnnotationFilter.ALL) {
208206
return Collections.emptyIterator();
209207
}
210208
return Spliterators.iterator(spliterator());
211209
}
212210

213211
@Override
214212
public Spliterator<MergedAnnotation<Annotation>> spliterator() {
215-
if (this.annotationFilter == FILTER_ALL) {
213+
if (this.annotationFilter == AnnotationFilter.ALL) {
216214
return Collections.<MergedAnnotation<Annotation>> emptyList().spliterator();
217215
}
218216
return spliterator(null);
@@ -510,8 +508,7 @@ private static class Aggregate {
510508
this.annotations = annotations;
511509
this.mappings = new AnnotationTypeMappings[annotations.size()];
512510
for (int i = 0; i < annotations.size(); i++) {
513-
this.mappings[i] = AnnotationTypeMappings.forAnnotationType(
514-
annotations.get(i).annotationType());
511+
this.mappings[i] = AnnotationTypeMappings.forAnnotationType(annotations.get(i).annotationType());
515512
}
516513
}
517514

@@ -531,8 +528,8 @@ AnnotationTypeMappings getMappings(int annotationIndex) {
531528

532529
@Nullable
533530
<A extends Annotation> MergedAnnotation<A> createMergedAnnotationIfPossible(
534-
int annotationIndex, int mappingIndex,
535-
IntrospectionFailureLogger logger) {
531+
int annotationIndex, int mappingIndex, IntrospectionFailureLogger logger) {
532+
536533
return TypeMappedAnnotation.createIfPossible(
537534
this.mappings[annotationIndex].get(mappingIndex), this.source,
538535
this.annotations.get(annotationIndex), this.aggregateIndex, logger);

0 commit comments

Comments
 (0)