Refine MergedAnnotation.asMap

Add a convenience method that allows a `MergedAnnotation` to be
converted into an `AnnotationAttributes` instance. Also rename
the `MapValues` enum to `Adapt` which generally seems to read
better.

Closes gh-22738
This commit is contained in:
Phillip Webb
2019-04-05 11:10:17 -07:00
parent ef151f578d
commit e905f1712e
10 changed files with 105 additions and 68 deletions

View File

@@ -27,7 +27,7 @@ import java.util.stream.Stream;
import org.junit.Test;
import org.springframework.core.annotation.MergedAnnotation.MapValues;
import org.springframework.core.annotation.MergedAnnotation.Adapt;
import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.*;
@@ -72,7 +72,7 @@ public class MergedAnnotationCollectorsTests {
MultiValueMap<String, Object> map = stream().map(
MergedAnnotation::filterDefaultValues).collect(
MergedAnnotationCollectors.toMultiValueMap(
MapValues.CLASS_TO_STRING));
Adapt.CLASS_TO_STRING));
assertThat(map.get("value")).containsExactly("a", "b", "c");
assertThat(map.get("extra")).containsExactly("java.lang.String",
"java.lang.Integer");

View File

@@ -40,7 +40,7 @@ import org.junit.Test;
import org.junit.internal.ArrayComparisonFailure;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.MergedAnnotation.MapValues;
import org.springframework.core.annotation.MergedAnnotation.Adapt;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.subpackage.NonPublicAnnotatedClass;
import org.springframework.lang.Nullable;
@@ -1665,7 +1665,7 @@ public class MergedAnnotationsTests {
assertThat(componentScan.value().pattern()).isEqualTo("*Foo");
Map<String, Object> map = MergedAnnotation.from(componentScan).asMap(
annotation -> new LinkedHashMap<String, Object>(),
MapValues.ANNOTATION_TO_MAP);
Adapt.ANNOTATION_TO_MAP);
Map<String, Object> filterMap = (Map<String, Object>) map.get("value");
assertThat(filterMap.get("pattern")).isEqualTo("*Foo");
filterMap.put("pattern", "newFoo");
@@ -1685,7 +1685,7 @@ public class MergedAnnotationsTests {
assertThat(componentScan).isNotNull();
Map<String, Object> map = MergedAnnotation.from(componentScan).asMap(
annotation -> new LinkedHashMap<String, Object>(),
MapValues.ANNOTATION_TO_MAP);
Adapt.ANNOTATION_TO_MAP);
Map<String, Object>[] filters = (Map[]) map.get("excludeFilters");
List<String> patterns = Arrays.stream(filters).map(
m -> (String) m.get("pattern")).collect(Collectors.toList());
@@ -2053,6 +2053,17 @@ public class MergedAnnotationsTests {
"FromValueAttributeMeta");
}
@Test
public void asAnnotationAttributesReturnsPopulatedAnnotationAttributes() {
MergedAnnotation<?> annotation = MergedAnnotations.from(
SpringApplicationConfigurationClass.class).get(
SpringApplicationConfiguration.class);
AnnotationAttributes attributes = annotation.asAnnotationAttributes(
Adapt.CLASS_TO_STRING);
assertThat(attributes).containsEntry("classes", new String[] { Number.class.getName() });
assertThat(attributes.annotationType()).isEqualTo(SpringApplicationConfiguration.class);
}
// @formatter:off
@Retention(RetentionPolicy.RUNTIME)

View File

@@ -264,6 +264,13 @@ public class MissingMergedAnnotationTests {
assertThat(this.missing.toString()).isEqualTo("(missing)");
}
@Test
public void asAnnotationAttributesReturnsNewAnnotationAttributes() {
AnnotationAttributes attributes = this.missing.asAnnotationAttributes();
assertThat(attributes).isEmpty();
assertThat(this.missing.asAnnotationAttributes()).isNotSameAs(attributes);
}
@Test
public void asMapReturnsEmptyMap() {
Map<String, Object> map = this.missing.asMap();