Skip to content

Commit 83cb51a

Browse files
committed
Add MergedAnnotation.getRoot() method
Update `MergedAnnotation` with a `getRoot()` method that allows the root direct annotation to be retrieved easily. Closes gh-22818
1 parent 7cc132b commit 83cb51a

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ public interface MergedAnnotation<A extends Annotation> {
140140
@Nullable
141141
MergedAnnotation<?> getParent();
142142

143+
/**
144+
* Get the root annotation, i.e. the {@link #getDepth() depth} {@code 0}
145+
* annotation as directly declared on the source.
146+
* @return the root annotation
147+
*/
148+
MergedAnnotation<?> getRoot();
149+
143150
/**
144151
* Determine if the specified attribute name has a non-default value when
145152
* compared to the annotation declaration.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public MergedAnnotation<?> getParent() {
6666
return null;
6767
}
6868

69+
@Override
70+
public MergedAnnotation<?> getRoot() {
71+
return this;
72+
}
73+
6974
@Override
7075
public int getDepth() {
7176
return -1;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ public MergedAnnotation<?> getParent() {
171171
this.valueExtractor, this.aggregateIndex, this.resolvedRootMirrors);
172172
}
173173

174+
@Override
175+
public MergedAnnotation<?> getRoot() {
176+
if (getDepth() == 0) {
177+
return this;
178+
}
179+
AnnotationTypeMapping rootMapping = this.mapping.getRoot();
180+
return new TypeMappedAnnotation<>(rootMapping, this.source, this.rootAttributes,
181+
this.valueExtractor, this.aggregateIndex, this.resolvedRootMirrors);
182+
}
183+
174184
@Override
175185
public boolean hasDefaultValue(String attributeName) {
176186
int attributeIndex = getAttributeIndex(attributeName, true);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ public void getParent() {
154154
.isEqualTo(ComposedTransactionalComponent.class);
155155
}
156156

157+
@Test
158+
public void getRootWhenNotDirect() {
159+
MergedAnnotations annotations = MergedAnnotations.from(ComposedTransactionalComponentClass.class);
160+
MergedAnnotation<?> annotation = annotations.get(TransactionalComponent.class);
161+
assertThat(annotation.getDepth()).isGreaterThan(0);
162+
assertThat(annotation.getRoot().getType()).isEqualTo(ComposedTransactionalComponent.class);
163+
}
164+
165+
@Test
166+
public void getRootWhenDirect() {
167+
MergedAnnotations annotations = MergedAnnotations.from(ComposedTransactionalComponentClass.class);
168+
MergedAnnotation<?> annotation = annotations.get(ComposedTransactionalComponent.class);
169+
assertThat(annotation.getDepth()).isEqualTo(0);
170+
assertThat(annotation.getRoot()).isSameAs(annotation);
171+
}
172+
157173
@Test
158174
public void collectMultiValueMapFromNonAnnotatedClass() {
159175
MultiValueMap<String, Object> map = MergedAnnotations.from(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public void getParentReturnsNull() {
7878
assertThat(this.missing.getParent()).isNull();
7979
}
8080

81+
@Test
82+
public void getRootReturnsEmptyAnnotation() {
83+
assertThat(this.missing.getRoot()).isSameAs(this.missing);
84+
}
85+
8186
@Test
8287
public void hasNonDefaultValueThrowsNoSuchElementException() {
8388
assertThatNoSuchElementException().isThrownBy(

0 commit comments

Comments
 (0)