Browse Source

Re-introduce support for annotation declarations with self references

Closes gh-31400
pull/31496/head
Juergen Hoeller 12 months ago
parent
commit
5459304a4b
  1. 9
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java
  2. 86
      spring-core/src/test/kotlin/org/springframework/core/annotation/KotlinMergedAnnotationsTests.kt
  3. 31
      spring-core/src/test/kotlin/org/springframework/core/annotation/PersonWithAlias.kt
  4. 29
      spring-core/src/test/kotlin/org/springframework/core/annotation/PersonWithoutAlias.kt

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

@ -45,6 +45,7 @@ import org.springframework.util.StringUtils; @@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
*
* @author Phillip Webb
* @author Sam Brannen
* @author Juergen Hoeller
* @since 5.2
* @see AnnotationTypeMappings
*/
@ -402,9 +403,11 @@ final class AnnotationTypeMapping { @@ -402,9 +403,11 @@ final class AnnotationTypeMapping {
if (type.isAnnotation() || (type.isArray() && type.getComponentType().isAnnotation())) {
Class<? extends Annotation> annotationType =
(Class<? extends Annotation>) (type.isAnnotation() ? type : type.getComponentType());
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(annotationType).get(0);
if (mapping.isSynthesizable()) {
return true;
if (annotationType != this.annotationType) {
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(annotationType).get(0);
if (mapping.isSynthesizable()) {
return true;
}
}
}
}

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

@ -0,0 +1,86 @@ @@ -0,0 +1,86 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
/**
* Tests for {@link MergedAnnotations} and {@link MergedAnnotation} in Kotlin.
*
* @author Sam Brannen
* @author Juergen Hoeller
* @since 5.3.16
*/
class KotlinMergedAnnotationsTests {
@Test // gh-28012
fun recursiveAnnotationWithAlias() {
val method = javaClass.getMethod("personWithAliasMethod")
// MergedAnnotations
val mergedAnnotations = MergedAnnotations.from(method)
assertThat(mergedAnnotations.isPresent(PersonWithAlias::class.java)).isTrue();
// MergedAnnotation
val mergedAnnotation = MergedAnnotation.from(method.getAnnotation(PersonWithAlias::class.java))
assertThat(mergedAnnotation).isNotNull();
// Synthesized Annotations
val jane = mergedAnnotation.synthesize()
assertThat(jane.value).isEqualTo("jane")
assertThat(jane.name).isEqualTo("jane")
val synthesizedFriends = jane.friends
assertThat(synthesizedFriends).hasSize(2)
val john = synthesizedFriends[0]
assertThat(john.value).isEqualTo("john")
assertThat(john.name).isEqualTo("john")
val sally = synthesizedFriends[1]
assertThat(sally.value).isEqualTo("sally")
assertThat(sally.name).isEqualTo("sally")
}
@Test // gh-31400
fun recursiveAnnotationWithoutAlias() {
val method = javaClass.getMethod("personWithoutAliasMethod")
// MergedAnnotations
val mergedAnnotations = MergedAnnotations.from(method)
assertThat(mergedAnnotations.isPresent(PersonWithoutAlias::class.java)).isTrue();
// MergedAnnotation
val mergedAnnotation = MergedAnnotation.from(method.getAnnotation(PersonWithoutAlias::class.java))
assertThat(mergedAnnotation).isNotNull();
// Synthesized Annotations
val jane = mergedAnnotation.synthesize()
val synthesizedFriends = jane.friends
assertThat(synthesizedFriends).hasSize(2)
}
@PersonWithAlias("jane", friends = [PersonWithAlias("john"), PersonWithAlias("sally")])
fun personWithAliasMethod() {
}
@PersonWithoutAlias("jane", friends = [PersonWithoutAlias("john"), PersonWithoutAlias("sally")])
fun personWithoutAliasMethod() {
}
}

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

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class PersonWithAlias(
@get:AliasFor("name")
val value: String = "",
@get:AliasFor("value")
val name: String = "",
vararg val friends: PersonWithAlias = []
)

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

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class PersonWithoutAlias(
val value: String = "",
val name: String = "",
vararg val friends: PersonWithoutAlias = []
)
Loading…
Cancel
Save