26
26
import java .util .Arrays ;
27
27
import java .util .List ;
28
28
import java .util .Objects ;
29
+ import java .util .function .Predicate ;
29
30
import java .util .stream .Stream ;
30
31
31
32
import org .junit .jupiter .api .Test ;
32
33
33
34
import org .springframework .core .annotation .MergedAnnotations .Search ;
34
35
import org .springframework .core .annotation .MergedAnnotations .SearchStrategy ;
35
36
import org .springframework .lang .Nullable ;
37
+ import org .springframework .util .ClassUtils ;
36
38
import org .springframework .util .ReflectionUtils ;
37
39
38
40
import static org .assertj .core .api .Assertions .assertThat ;
@@ -422,29 +424,34 @@ void typeHierarchyStrategyOnMethodWithGenericParameterNonOverrideScansAnnotation
422
424
}
423
425
424
426
@ Test
425
- @ SuppressWarnings ("deprecation" )
426
- void typeHierarchyWithEnclosedStrategyOnEnclosedStaticClassScansAnnotations () {
427
+ void typeHierarchyStrategyWithEnclosingClassPredicatesOnEnclosedStaticClassScansAnnotations () {
427
428
Class <?> source = AnnotationEnclosingClassSample .EnclosedStatic .EnclosedStaticStatic .class ;
428
- assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY_AND_ENCLOSING_CLASSES ))
429
+ assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY , ClassUtils ::isInnerClass ))
430
+ .containsExactly ("0:EnclosedThree" );
431
+ assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY , Search .always ).toList ())
432
+ .isEqualTo (scan (source , SearchStrategy .TYPE_HIERARCHY , ClassUtils ::isStaticClass ).toList ())
429
433
.containsExactly ("0:EnclosedThree" , "1:EnclosedTwo" , "2:EnclosedOne" );
430
434
}
431
435
432
436
@ Test
433
- @ SuppressWarnings ("deprecation" )
434
- void typeHierarchyWithEnclosedStrategyOnEnclosedInnerClassScansAnnotations () {
437
+ void typeHierarchyStrategyWithEnclosingClassPredicatesOnEnclosedInnerClassScansAnnotations () {
435
438
Class <?> source = AnnotationEnclosingClassSample .EnclosedInner .EnclosedInnerInner .class ;
436
- assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY_AND_ENCLOSING_CLASSES ))
439
+ assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY , ClassUtils ::isStaticClass ))
440
+ .containsExactly ("0:EnclosedThree" );
441
+ assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY , Search .always ).toList ())
442
+ .isEqualTo (scan (source , SearchStrategy .TYPE_HIERARCHY , ClassUtils ::isInnerClass ).toList ())
437
443
.containsExactly ("0:EnclosedThree" , "1:EnclosedTwo" , "2:EnclosedOne" );
438
444
}
439
445
440
446
@ Test
441
- @ SuppressWarnings ("deprecation" )
442
- void typeHierarchyWithEnclosedStrategyOnMethodHierarchyUsesTypeHierarchyScan () {
447
+ void typeHierarchyStrategyWithEnclosingClassPredicatesOnMethodHierarchyUsesTypeHierarchyScan () {
443
448
Method source = methodFrom (WithHierarchy .class );
444
- assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY_AND_ENCLOSING_CLASSES )).containsExactly (
445
- "0:TestAnnotation1" , "1:TestAnnotation5" , "1:TestInheritedAnnotation5" ,
446
- "2:TestAnnotation6" , "3:TestAnnotation2" , "3:TestInheritedAnnotation2" ,
447
- "4:TestAnnotation3" , "5:TestAnnotation4" );
449
+ assertThat (scan (source , SearchStrategy .TYPE_HIERARCHY , Search .always ).toList ())
450
+ .isEqualTo (scan (source , SearchStrategy .TYPE_HIERARCHY , ClassUtils ::isInnerClass ).toList ())
451
+ .containsExactly (
452
+ "0:TestAnnotation1" , "1:TestAnnotation5" , "1:TestInheritedAnnotation5" ,
453
+ "2:TestAnnotation6" , "3:TestAnnotation2" , "3:TestInheritedAnnotation2" ,
454
+ "4:TestAnnotation3" , "5:TestAnnotation4" );
448
455
}
449
456
450
457
@ Test
@@ -509,8 +516,14 @@ private Method methodFrom(Class<?> type) {
509
516
}
510
517
511
518
private Stream <String > scan (AnnotatedElement element , SearchStrategy searchStrategy ) {
519
+ return scan (element , searchStrategy , Search .never );
520
+ }
521
+
522
+ private Stream <String > scan (AnnotatedElement element , SearchStrategy searchStrategy ,
523
+ Predicate <Class <?>> searchEnclosingClass ) {
524
+
512
525
List <String > results = new ArrayList <>();
513
- scan (this , element , searchStrategy ,
526
+ scan (this , element , searchStrategy , searchEnclosingClass ,
514
527
(criteria , aggregateIndex , source , annotations ) -> {
515
528
trackIndexedAnnotations (aggregateIndex , annotations , results );
516
529
return null ; // continue searching
@@ -521,7 +534,13 @@ private Stream<String> scan(AnnotatedElement element, SearchStrategy searchStrat
521
534
private static <C , R > R scan (C context , AnnotatedElement source , SearchStrategy searchStrategy ,
522
535
AnnotationsProcessor <C , R > processor ) {
523
536
524
- return AnnotationsScanner .scan (context , source , searchStrategy , Search .never , processor );
537
+ return scan (context , source , searchStrategy , Search .never , processor );
538
+ }
539
+
540
+ private static <C , R > R scan (C context , AnnotatedElement source , SearchStrategy searchStrategy ,
541
+ Predicate <Class <?>> searchEnclosingClass , AnnotationsProcessor <C , R > processor ) {
542
+
543
+ return AnnotationsScanner .scan (context , source , searchStrategy , searchEnclosingClass , processor );
525
544
}
526
545
527
546
private void trackIndexedAnnotations (int aggregateIndex , Annotation [] annotations , List <String > results ) {
0 commit comments