Skip to content

Commit 5689395

Browse files
committed
Deprecate "enclosing classes" search strategy for MergedAnnotations
The TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy for MergedAnnotations was originally introduced to support @nested test classes in JUnit Jupiter (see #23378). However, while implementing #19930, we determined that the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy unfortunately could not be used since it does not allow the user to control when to recurse up the enclosing class hierarchy. For example, this search strategy will automatically search on enclosing classes for static nested classes as well as for inner classes, when the user probably only wants one such category of "enclosing class" to be searched. Consequently, TestContextAnnotationUtils was introduced in the Spring TestContext Framework to address the shortcomings of the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy. Since this search strategy is unlikely to be useful to general users, the team has decided to deprecate this search strategy in Spring Framework 5.3.x and remove it in 6.0. Closes gh-28079
1 parent 071c298 commit 5689395

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-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

@@ -235,6 +236,7 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
235236
}
236237

237238
@Nullable
239+
@SuppressWarnings("deprecation")
238240
private static <C, R> R processMethod(C context, Method source,
239241
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
240242

@@ -510,6 +512,7 @@ static boolean hasPlainJavaAnnotationsOnly(Class<?> type) {
510512
return (type.getName().startsWith("java.") || type == Ordered.class);
511513
}
512514

515+
@SuppressWarnings("deprecation")
513516
private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrategy searchStrategy) {
514517
if (source == Object.class) {
515518
return true;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,9 @@ enum SearchStrategy {
482482
* need to be meta-annotated with {@link Inherited @Inherited}. When
483483
* searching a {@link Method} source, this strategy is identical to
484484
* {@link #TYPE_HIERARCHY}.
485+
* @deprecated as of Spring Framework 5.3.17; to be removed in Spring Framework 6.0
485486
*/
487+
@Deprecated
486488
TYPE_HIERARCHY_AND_ENCLOSING_CLASSES
487489
}
488490

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,13 +712,15 @@ void streamTypeHierarchyFromClassWithInterface() throws Exception {
712712
}
713713

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

721722
@Test
723+
@SuppressWarnings("deprecation")
722724
void streamTypeHierarchyAndEnclosingClassesFromNonAnnotatedStaticNestedClassWithAnnotatedEnclosingClass() {
723725
Stream<Class<?>> classes = MergedAnnotations.from(AnnotatedClass.NonAnnotatedStaticNestedClass.class,
724726
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES).stream().map(MergedAnnotation::getType);

0 commit comments

Comments
 (0)