Skip to content

Commit fc8f31c

Browse files
committed
Deprecate "enclosing classes" search strategy for MergedAnnotations
This commit deprecates the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy for MergedAnnotations in 6.0 M3, allowing consumers of 6.0 milestones and release candidates to provide feedback before potentially completely removing the search strategy or providing an alternate mechanism for achieving the same goal prior to 6.0 GA. Closes gh-28079 See gh-28080
1 parent a3781a4 commit fc8f31c

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -92,6 +92,7 @@ private static <C, R> R process(C context, AnnotatedElement source,
9292
}
9393

9494
@Nullable
95+
@SuppressWarnings("deprecation")
9596
private static <C, R> R processClass(C context, Class<?> source,
9697
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
9798

@@ -229,6 +230,7 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
229230
}
230231

231232
@Nullable
233+
@SuppressWarnings("deprecation")
232234
private static <C, R> R processMethod(C context, Method source,
233235
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
234236

@@ -499,6 +501,7 @@ static boolean hasPlainJavaAnnotationsOnly(Class<?> type) {
499501
return (type.getName().startsWith("java.") || type == Ordered.class);
500502
}
501503

504+
@SuppressWarnings("deprecation")
502505
private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrategy searchStrategy) {
503506
if (source == Object.class) {
504507
return true;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,10 @@ enum SearchStrategy {
492492
* whether the source type is an <em>inner class</em>, a {@code static}
493493
* nested class, or a nested interface. Thus, it may find more annotations
494494
* than you would expect.
495+
* @deprecated as of Spring Framework 6.0 M3, for potential removal or
496+
* replacement before 6.0 GA
495497
*/
498+
@Deprecated
496499
TYPE_HIERARCHY_AND_ENCLOSING_CLASSES
497500

498501
}

spring-core/src/test/java/org/springframework/core/annotation/AnnotationsScannerTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -421,20 +421,23 @@ void typeHierarchyStrategyOnMethodWithGenericParameterNonOverrideScansAnnotation
421421
}
422422

423423
@Test
424+
@SuppressWarnings("deprecation")
424425
void typeHierarchyWithEnclosedStrategyOnEnclosedStaticClassScansAnnotations() {
425426
Class<?> source = AnnotationEnclosingClassSample.EnclosedStatic.EnclosedStaticStatic.class;
426427
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES))
427428
.containsExactly("0:EnclosedThree", "1:EnclosedTwo", "2:EnclosedOne");
428429
}
429430

430431
@Test
432+
@SuppressWarnings("deprecation")
431433
void typeHierarchyWithEnclosedStrategyOnEnclosedInnerClassScansAnnotations() {
432434
Class<?> source = AnnotationEnclosingClassSample.EnclosedInner.EnclosedInnerInner.class;
433435
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES))
434436
.containsExactly("0:EnclosedThree", "1:EnclosedTwo", "2:EnclosedOne");
435437
}
436438

437439
@Test
440+
@SuppressWarnings("deprecation")
438441
void typeHierarchyWithEnclosedStrategyOnMethodHierarchyUsesTypeHierarchyScan() {
439442
Method source = methodFrom(WithHierarchy.class);
440443
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)).containsExactly(

spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,20 +711,23 @@ void streamTypeHierarchyFromClassWithInterface() throws Exception {
711711
}
712712

713713
@Test
714+
@SuppressWarnings("deprecation")
714715
void streamTypeHierarchyAndEnclosingClassesFromNonAnnotatedInnerClassWithAnnotatedEnclosingClass() {
715716
Stream<Class<?>> classes = MergedAnnotations.from(AnnotatedClass.NonAnnotatedInnerClass.class,
716717
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES).stream().map(MergedAnnotation::getType);
717718
assertThat(classes).containsExactly(Component.class, Indexed.class);
718719
}
719720

720721
@Test
722+
@SuppressWarnings("deprecation")
721723
void streamTypeHierarchyAndEnclosingClassesFromNonAnnotatedStaticNestedClassWithAnnotatedEnclosingClass() {
722724
Stream<Class<?>> classes = MergedAnnotations.from(AnnotatedClass.NonAnnotatedStaticNestedClass.class,
723725
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES).stream().map(MergedAnnotation::getType);
724726
assertThat(classes).containsExactly(Component.class, Indexed.class);
725727
}
726728

727729
@Test
730+
@SuppressWarnings("deprecation")
728731
void getFromMethodWithMethodAnnotationOnLeaf() throws Exception {
729732
Method method = Leaf.class.getMethod("annotatedOnLeaf");
730733
assertThat(method.getAnnotation(Order.class)).isNotNull();

0 commit comments

Comments
 (0)