Skip to content

Commit 6a7a0bd

Browse files
lorenzsimonsbrannen
authored andcommitted
Restore support for recursive annotations in Kotlin
This commit reinstates support for recursive annotations in Kotlin. See gh-28012 See gh-28618 See gh-31400 Closes gh-31518
1 parent 5c012bb commit 6a7a0bd

File tree

6 files changed

+159
-28
lines changed

6 files changed

+159
-28
lines changed

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ final class AnnotationTypeMapping {
103103
private final Set<Method> claimedAliases = new HashSet<>();
104104

105105

106-
AnnotationTypeMapping(@Nullable AnnotationTypeMapping source,
107-
Class<? extends Annotation> annotationType, @Nullable Annotation annotation) {
106+
AnnotationTypeMapping(@Nullable AnnotationTypeMapping source, Class<? extends Annotation> annotationType,
107+
@Nullable Annotation annotation, Set<Class<? extends Annotation>> visitedAnnotationTypes) {
108108

109109
this.source = source;
110110
this.root = (source != null ? source.getRoot() : this);
@@ -124,7 +124,7 @@ final class AnnotationTypeMapping {
124124
processAliases();
125125
addConventionMappings();
126126
addConventionAnnotationValues();
127-
this.synthesizable = computeSynthesizableFlag();
127+
this.synthesizable = computeSynthesizableFlag(visitedAnnotationTypes);
128128
}
129129

130130

@@ -374,7 +374,10 @@ private boolean isBetterConventionAnnotationValue(int index, boolean isValueAttr
374374
}
375375

376376
@SuppressWarnings("unchecked")
377-
private boolean computeSynthesizableFlag() {
377+
private boolean computeSynthesizableFlag(Set<Class<? extends Annotation>> visitedAnnotationTypes) {
378+
// Track that we have visited the current annotation type.
379+
visitedAnnotationTypes.add(this.annotationType);
380+
378381
// Uses @AliasFor for local aliases?
379382
for (int index : this.aliasMappings) {
380383
if (index != -1) {
@@ -403,8 +406,12 @@ private boolean computeSynthesizableFlag() {
403406
if (type.isAnnotation() || (type.isArray() && type.getComponentType().isAnnotation())) {
404407
Class<? extends Annotation> annotationType =
405408
(Class<? extends Annotation>) (type.isAnnotation() ? type : type.getComponentType());
406-
if (annotationType != this.annotationType) {
407-
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(annotationType).get(0);
409+
// Ensure we have not yet visited the current nested annotation type, in order
410+
// to avoid infinite recursion for JVM languages other than Java that support
411+
// recursive annotation definitions.
412+
if (visitedAnnotationTypes.add(annotationType)) {
413+
AnnotationTypeMapping mapping =
414+
AnnotationTypeMappings.forAnnotationType(annotationType, visitedAnnotationTypes).get(0);
408415
if (mapping.isSynthesizable()) {
409416
return true;
410417
}

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

+54-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -20,8 +20,10 @@
2020
import java.util.ArrayDeque;
2121
import java.util.ArrayList;
2222
import java.util.Deque;
23+
import java.util.HashSet;
2324
import java.util.List;
2425
import java.util.Map;
26+
import java.util.Set;
2527

2628
import org.springframework.lang.Nullable;
2729
import org.springframework.util.ConcurrentReferenceHashMap;
@@ -40,6 +42,7 @@
4042
* be searched once, regardless of how many times they are actually used.
4143
*
4244
* @author Phillip Webb
45+
* @author Sam Brannen
4346
* @since 5.2
4447
* @see AnnotationTypeMapping
4548
*/
@@ -60,19 +63,21 @@ final class AnnotationTypeMappings {
6063

6164

6265
private AnnotationTypeMappings(RepeatableContainers repeatableContainers,
63-
AnnotationFilter filter, Class<? extends Annotation> annotationType) {
66+
AnnotationFilter filter, Class<? extends Annotation> annotationType,
67+
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
6468

6569
this.repeatableContainers = repeatableContainers;
6670
this.filter = filter;
6771
this.mappings = new ArrayList<>();
68-
addAllMappings(annotationType);
72+
addAllMappings(annotationType, visitedAnnotationTypes);
6973
this.mappings.forEach(AnnotationTypeMapping::afterAllMappingsSet);
7074
}
7175

7276

73-
private void addAllMappings(Class<? extends Annotation> annotationType) {
77+
private void addAllMappings(Class<? extends Annotation> annotationType,
78+
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
7479
Deque<AnnotationTypeMapping> queue = new ArrayDeque<>();
75-
addIfPossible(queue, null, annotationType, null);
80+
addIfPossible(queue, null, annotationType, null, visitedAnnotationTypes);
7681
while (!queue.isEmpty()) {
7782
AnnotationTypeMapping mapping = queue.removeFirst();
7883
this.mappings.add(mapping);
@@ -102,14 +107,15 @@ private void addMetaAnnotationsToQueue(Deque<AnnotationTypeMapping> queue, Annot
102107
}
103108

104109
private void addIfPossible(Deque<AnnotationTypeMapping> queue, AnnotationTypeMapping source, Annotation ann) {
105-
addIfPossible(queue, source, ann.annotationType(), ann);
110+
addIfPossible(queue, source, ann.annotationType(), ann, new HashSet<>());
106111
}
107112

108113
private void addIfPossible(Deque<AnnotationTypeMapping> queue, @Nullable AnnotationTypeMapping source,
109-
Class<? extends Annotation> annotationType, @Nullable Annotation ann) {
114+
Class<? extends Annotation> annotationType, @Nullable Annotation ann,
115+
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
110116

111117
try {
112-
queue.addLast(new AnnotationTypeMapping(source, annotationType, ann));
118+
queue.addLast(new AnnotationTypeMapping(source, annotationType, ann, visitedAnnotationTypes));
113119
}
114120
catch (Exception ex) {
115121
AnnotationUtils.rethrowAnnotationConfigurationException(ex);
@@ -166,20 +172,37 @@ AnnotationTypeMapping get(int index) {
166172
* @return type mappings for the annotation type
167173
*/
168174
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType) {
169-
return forAnnotationType(annotationType, AnnotationFilter.PLAIN);
175+
return forAnnotationType(annotationType, new HashSet<>());
170176
}
171177

172178
/**
173179
* Create {@link AnnotationTypeMappings} for the specified annotation type.
174180
* @param annotationType the source annotation type
181+
* @param visitedAnnotationTypes the set of annotations that we have already
182+
* visited; used to avoid infinite recursion for recursive annotations which
183+
* some JVM languages support (such as Kotlin)
184+
* @return type mappings for the annotation type
185+
*/
186+
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType,
187+
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
188+
189+
return forAnnotationType(annotationType, RepeatableContainers.standardRepeatables(),
190+
AnnotationFilter.PLAIN, visitedAnnotationTypes);
191+
}
192+
193+
/**
194+
* Create {@link AnnotationTypeMappings} for the specified annotation type.
195+
* @param annotationType the source annotation type
196+
* @param repeatableContainers the repeatable containers that may be used by
197+
* the meta-annotations
175198
* @param annotationFilter the annotation filter used to limit which
176199
* annotations are considered
177200
* @return type mappings for the annotation type
178201
*/
179-
static AnnotationTypeMappings forAnnotationType(
180-
Class<? extends Annotation> annotationType, AnnotationFilter annotationFilter) {
202+
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType,
203+
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) {
181204

182-
return forAnnotationType(annotationType, RepeatableContainers.standardRepeatables(), annotationFilter);
205+
return forAnnotationType(annotationType, repeatableContainers, annotationFilter, new HashSet<>());
183206
}
184207

185208
/**
@@ -189,20 +212,25 @@ static AnnotationTypeMappings forAnnotationType(
189212
* the meta-annotations
190213
* @param annotationFilter the annotation filter used to limit which
191214
* annotations are considered
215+
* @param visitedAnnotationTypes the set of annotations that we have already
216+
* visited; used to avoid infinite recursion for recursive annotations which
217+
* some JVM languages support (such as Kotlin)
192218
* @return type mappings for the annotation type
193219
*/
194220
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType,
195-
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) {
221+
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter,
222+
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
196223

197224
if (repeatableContainers == RepeatableContainers.standardRepeatables()) {
198225
return standardRepeatablesCache.computeIfAbsent(annotationFilter,
199-
key -> new Cache(repeatableContainers, key)).get(annotationType);
226+
key -> new Cache(repeatableContainers, key)).get(annotationType, visitedAnnotationTypes);
200227
}
201228
if (repeatableContainers == RepeatableContainers.none()) {
202229
return noRepeatablesCache.computeIfAbsent(annotationFilter,
203-
key -> new Cache(repeatableContainers, key)).get(annotationType);
230+
key -> new Cache(repeatableContainers, key)).get(annotationType, visitedAnnotationTypes);
204231
}
205-
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType);
232+
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType,
233+
visitedAnnotationTypes);
206234
}
207235

208236
static void clearCache() {
@@ -235,14 +263,20 @@ private static class Cache {
235263
/**
236264
* Get or create {@link AnnotationTypeMappings} for the specified annotation type.
237265
* @param annotationType the annotation type
266+
* @param visitedAnnotationTypes the set of annotations that we have already
267+
* visited; used to avoid infinite recursion for recursive annotations which
268+
* some JVM languages support (such as Kotlin)
238269
* @return a new or existing {@link AnnotationTypeMappings} instance
239270
*/
240-
AnnotationTypeMappings get(Class<? extends Annotation> annotationType) {
241-
return this.mappings.computeIfAbsent(annotationType, this::createMappings);
271+
AnnotationTypeMappings get(Class<? extends Annotation> annotationType,
272+
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
273+
return this.mappings.computeIfAbsent(annotationType, key -> createMappings(key, visitedAnnotationTypes));
242274
}
243275

244-
AnnotationTypeMappings createMappings(Class<? extends Annotation> annotationType) {
245-
return new AnnotationTypeMappings(this.repeatableContainers, this.filter, annotationType);
276+
private AnnotationTypeMappings createMappings(Class<? extends Annotation> annotationType,
277+
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
278+
return new AnnotationTypeMappings(this.repeatableContainers, this.filter, annotationType,
279+
visitedAnnotationTypes);
246280
}
247281
}
248282

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

+2-2
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-2023 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.
@@ -80,7 +80,7 @@ void forAnnotationTypeWhenHasRepeatingMetaAnnotationReturnsMapping() {
8080
@Test
8181
void forAnnotationTypeWhenRepeatableMetaAnnotationIsFiltered() {
8282
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(WithRepeatedMetaAnnotations.class,
83-
Repeating.class.getName()::equals);
83+
RepeatableContainers.standardRepeatables(), Repeating.class.getName()::equals);
8484
assertThat(getAll(mappings)).flatExtracting(AnnotationTypeMapping::getAnnotationType)
8585
.containsExactly(WithRepeatedMetaAnnotations.class);
8686
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.annotation
18+
19+
@Target(AnnotationTarget.FUNCTION)
20+
@Retention(AnnotationRetention.RUNTIME)
21+
annotation class FilterWithAlias(
22+
23+
@get:AliasFor("name")
24+
val value: String = "",
25+
26+
@get:AliasFor("value")
27+
val name: String = "",
28+
29+
val and: FiltersWithoutAlias = FiltersWithoutAlias()
30+
31+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.annotation
18+
19+
@Target(AnnotationTarget.FUNCTION)
20+
@Retention(AnnotationRetention.RUNTIME)
21+
annotation class FiltersWithoutAlias(
22+
23+
vararg val value: FilterWithAlias
24+
25+
)

spring-core/src/test/kotlin/org/springframework/core/annotation/KotlinMergedAnnotationsTests.kt

+34
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test
2424
*
2525
* @author Sam Brannen
2626
* @author Juergen Hoeller
27+
* @author Lorenz Simon
2728
* @since 5.3.16
2829
*/
2930
class KotlinMergedAnnotationsTests {
@@ -74,6 +75,35 @@ class KotlinMergedAnnotationsTests {
7475
assertThat(synthesizedFriends).hasSize(2)
7576
}
7677

78+
@Test // gh-28012
79+
fun recursiveNestedAnnotationWithAlias() {
80+
val method = javaClass.getMethod("filterWithAliasMethod")
81+
82+
// MergedAnnotations
83+
val mergedAnnotations = MergedAnnotations.from(method)
84+
assertThat(mergedAnnotations.isPresent(FilterWithAlias::class.java)).isTrue();
85+
86+
// MergedAnnotation
87+
val mergedAnnotation = MergedAnnotation.from(method.getAnnotation(FilterWithAlias::class.java))
88+
assertThat(mergedAnnotation).isNotNull();
89+
90+
// Synthesized Annotations
91+
val fooFilter = mergedAnnotation.synthesize()
92+
assertThat(fooFilter.value).isEqualTo("foo")
93+
assertThat(fooFilter.name).isEqualTo("foo")
94+
val filters = fooFilter.and
95+
assertThat(filters.value).hasSize(2)
96+
97+
val barFilter = filters.value[0]
98+
assertThat(barFilter.value).isEqualTo("bar")
99+
assertThat(barFilter.name).isEqualTo("bar")
100+
assertThat(barFilter.and.value).isEmpty()
101+
102+
val bazFilter = filters.value[1]
103+
assertThat(bazFilter.value).isEqualTo("baz")
104+
assertThat(bazFilter.name).isEqualTo("baz")
105+
assertThat(bazFilter.and.value).isEmpty()
106+
}
77107

78108
@PersonWithAlias("jane", friends = [PersonWithAlias("john"), PersonWithAlias("sally")])
79109
fun personWithAliasMethod() {
@@ -83,4 +113,8 @@ class KotlinMergedAnnotationsTests {
83113
fun personWithoutAliasMethod() {
84114
}
85115

116+
@FilterWithAlias("foo", and = FiltersWithoutAlias(FilterWithAlias("bar"), FilterWithAlias("baz")))
117+
fun filterWithAliasMethod() {
118+
}
119+
86120
}

0 commit comments

Comments
 (0)