Skip to content

Commit 98d6699

Browse files
philwebbsnicoll
authored andcommitted
Add additional AnnotationAttributes factory method
Add `createIfAnnotationPresent` method to `AnnotationAttributes` so that a `MergedAnnotation` instance can be quickly converted to an `AnnotationAttributes` Map. The method is specifically designed to work with the `asMap` method when the new API is not being used directly. For example: AnnotationAttributes attributes = mergedAnnotation.asMap( AnnotationAttributes::createIfAnnotationPresent); See spring-projectsgh-21697
1 parent e84e6db commit 98d6699

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ public AnnotationAttributes(Class<? extends Annotation> annotationType) {
113113
this.displayName = annotationType.getName();
114114
}
115115

116+
/**
117+
* Create a possibly already validated new, empty
118+
* {@link AnnotationAttributes} instance for the specified
119+
* {@code annotationType}.
120+
* @param annotationType the type of annotation represented by this
121+
* {@code AnnotationAttributes} instance; never {@code null}
122+
* @param validated if the attributes are considered already validated
123+
* @since 5.2
124+
*/
125+
AnnotationAttributes(Class<? extends Annotation> annotationType, boolean validated) {
126+
Assert.notNull(annotationType, "'annotationType' must not be null");
127+
this.annotationType = annotationType;
128+
this.displayName = annotationType.getName();
129+
this.validated = validated;
130+
}
131+
116132
/**
117133
* Create a new, empty {@link AnnotationAttributes} instance for the
118134
* specified {@code annotationType}.
@@ -434,4 +450,17 @@ public static AnnotationAttributes fromMap(@Nullable Map<String, Object> map) {
434450
return new AnnotationAttributes(map);
435451
}
436452

453+
/**
454+
* Factory method used to create {@link AnnotationAttributes} only when the
455+
* specific annotation is present.
456+
* @param annotation the source annotation
457+
* @return a new {@link AnnotationAttributes} instance of {@code null}
458+
* @since 5.2
459+
* @see MergedAnnotation#asMap(java.util.function.Function,
460+
* org.springframework.core.annotation.MergedAnnotation.MapValues...)
461+
*/
462+
public static AnnotationAttributes createIfAnnotationPresent(MergedAnnotation<?> annotation) {
463+
return annotation.isPresent() ? new AnnotationAttributes() : null;
464+
}
465+
437466
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import static org.hamcrest.CoreMatchers.*;
3131
import static org.junit.Assert.*;
32+
import static org.mockito.BDDMockito.given;
33+
import static org.mockito.Mockito.mock;
3234

3335
/**
3436
* Unit tests for {@link AnnotationAttributes}.
@@ -221,6 +223,23 @@ public void getAliasedStringArrayWithImplicitAliases() {
221223
aliases.stream().forEach(alias -> assertArrayEquals(new String[] {""}, attributes.getStringArray(alias)));
222224
}
223225

226+
@Test
227+
public void createIfAnnotationPresentWhenPresentReturnsAttributes() {
228+
MergedAnnotation<?> annotation = mock(MergedAnnotation.class);
229+
given(annotation.isPresent()).willReturn(true);
230+
AnnotationAttributes attributes = AnnotationAttributes.createIfAnnotationPresent(
231+
annotation);
232+
assertThat(attributes, notNullValue());
233+
}
234+
235+
@Test
236+
public void createIfAnnotationPresentWhenNotPresentReturnsNull() {
237+
MergedAnnotation<?> annotation = mock(MergedAnnotation.class);
238+
AnnotationAttributes attributes = AnnotationAttributes.createIfAnnotationPresent(
239+
annotation);
240+
assertThat(attributes, nullValue());
241+
}
242+
224243

225244
enum Color {
226245

0 commit comments

Comments
 (0)