Protect against stack overflow when searching meta annotations
It is legal for an annotation to be annotated with itself. Previously, when searching for meta annotations this could lead to a stack overflow. This was likely to occur when using Kotlin as, like Java, its Target annotation is annotated with itself. A stack overflow doesn’t occur with Java’s Target annotation due to some short-circuiting logic for annotations in java.lang. This commit updates the logic for finding meta-annotations to short-circuit when an annotation that has already been seen is encountered. Closes gh-5902
This commit is contained in:
@@ -37,6 +37,7 @@ import static org.mockito.Mockito.mock;
|
||||
* Tests for {@link ImportsContextCustomizerFactory} and {@link ImportsContextCustomizer}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ImportsContextCustomizerFactoryTests {
|
||||
|
||||
@@ -101,6 +102,12 @@ public class ImportsContextCustomizerFactoryTests {
|
||||
assertThat(context.getBean(ImportedBean.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selfAnnotatingAnnotationDoesNotCauseStackOverflow() {
|
||||
assertThat(this.factory.createContextCustomizer(
|
||||
TestWithImportAndSelfAnnotatingAnnotation.class, null)).isNotNull();
|
||||
}
|
||||
|
||||
static class TestWithNoImport {
|
||||
|
||||
}
|
||||
@@ -137,6 +144,12 @@ public class ImportsContextCustomizerFactoryTests {
|
||||
|
||||
}
|
||||
|
||||
@SelfAnnotating
|
||||
@Import(ImportedBean.class)
|
||||
static class TestWithImportAndSelfAnnotatingAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Import(ImportedBean.class)
|
||||
@interface MetaImport {
|
||||
@@ -153,4 +166,10 @@ public class ImportsContextCustomizerFactoryTests {
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@SelfAnnotating
|
||||
static @interface SelfAnnotating {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user