Skip to content

Commit

Permalink
Re-introduce support for annotation declarations with self references
Browse files Browse the repository at this point in the history
Closes gh-31400
  • Loading branch information
jhoeller committed Oct 10, 2023
1 parent 00d4830 commit 5459304
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*
* @author Phillip Webb
* @author Sam Brannen
* @author Juergen Hoeller
* @since 5.2
* @see AnnotationTypeMappings
*/
Expand Down Expand Up @@ -402,9 +403,11 @@ private boolean computeSynthesizableFlag() {
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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}

}
Original file line number Diff line number Diff line change
@@ -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 = []

)
Original file line number Diff line number Diff line change
@@ -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 = []

)

0 comments on commit 5459304

Please sign in to comment.