Skip to content

Commit 650cbee

Browse files
yilianhuaixiaosbrannen
authored andcommitted
Avoid infinite loop in AnnotationScanner
Prior to this commit, scanning for annotations resulted in an infinite loop when using the INHERITED_ANNOTATIONS search strategy and a class filter that filters out visited classes. This commit avoids an infinite loop in AnnotationsScanner's processClassInheritedAnnotations(...) method by skipping the current level of the class hierarchy when the current source class has been filtered out. Closes spring-projectsgh-25429
1 parent 335c3d5 commit 650cbee

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ private static <C, R> R processClassInheritedAnnotations(C context, Class<?> sou
151151
return result;
152152
}
153153
if (isFiltered(source, context, classFilter)) {
154+
source = source.getSuperclass();
155+
aggregateIndex++;
154156
continue;
155157
}
156158
Annotation[] declaredAnnotations =

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,22 @@ public String finish(String result) {
499499
assertThat(result).isEqualTo("OK");
500500
}
501501

502+
@Test
503+
void scanWithFilteredAll() {
504+
List<Integer> indexes = new ArrayList<>();
505+
String result = AnnotationsScanner.scan(this, WithSingleSuperclass.class,
506+
SearchStrategy.INHERITED_ANNOTATIONS,
507+
(context, aggregateIndex, source, annotations) -> {
508+
indexes.add(aggregateIndex);
509+
return "";
510+
},
511+
(context,cls)->{
512+
return true;
513+
}
514+
);
515+
assertThat(result).isNull();
516+
}
517+
502518

503519
private Method methodFrom(Class<?> type) {
504520
return ReflectionUtils.findMethod(type, "method");

0 commit comments

Comments
 (0)