From d4a761abeac3e75d59d481e4ccd5f38ee2e86e19 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 6 May 2019 09:51:26 -0700 Subject: [PATCH] Rename some MergedAnnotation `from` methods to `of` Rename `from` to `of` for the `MergedAnnotation` factory methods that work with Maps. The previous name was a little confusing, especially when an annotation source parameter was specified. The new method name helps to make it clearer when the user is explicitly defining the attributes of the annotation, as opposed to picking them up from the source. --- .../core/annotation/AnnotationUtils.java | 6 ++--- .../core/annotation/MergedAnnotation.java | 24 +++++++++---------- .../core/annotation/TypeMappedAnnotation.java | 2 +- .../annotation/MergedAnnotationsTests.java | 24 +++++++++---------- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index fdceb174ce..cae01fb25c 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -905,7 +905,7 @@ public abstract class AnnotationUtils { } else { // If we have nested annotations, we need them as nested maps - AnnotationAttributes attributes = MergedAnnotation.from(annotationType) + AnnotationAttributes attributes = MergedAnnotation.of(annotationType) .asMap(annotation -> new AnnotationAttributes(annotation.getType(), true), Adapt.ANNOTATION_TO_MAP); for (Map.Entry element : attributes.entrySet()) { @@ -1151,7 +1151,7 @@ public abstract class AnnotationUtils { if (annotationType == null || !StringUtils.hasText(attributeName)) { return null; } - return MergedAnnotation.from(annotationType).getDefaultValue(attributeName).orElse(null); + return MergedAnnotation.of(annotationType).getDefaultValue(attributeName).orElse(null); } /** @@ -1232,7 +1232,7 @@ public abstract class AnnotationUtils { Class annotationType, @Nullable AnnotatedElement annotatedElement) { try { - return MergedAnnotation.from(annotatedElement, annotationType, attributes).synthesize(); + return MergedAnnotation.of(annotatedElement, annotationType, attributes).synthesize(); } catch (NoSuchElementException | IllegalStateException ex) { throw new IllegalArgumentException(ex); diff --git a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotation.java b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotation.java index ecefd18099..cdc3c58a21 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotation.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotation.java @@ -523,34 +523,34 @@ public interface MergedAnnotation { } /** - * Create a new {@link MergedAnnotation} instance from the specified + * Create a new {@link MergedAnnotation} instance of the specified * annotation type. The resulting annotation will not have any attribute * values but may still be used to query default values. * @param annotationType the annotation type * @return a {@link MergedAnnotation} instance for the annotation */ - static MergedAnnotation from(Class annotationType) { - return from(null, annotationType, null); + static MergedAnnotation of(Class annotationType) { + return of(null, annotationType, null); } /** - * Create a new {@link MergedAnnotation} instance from the specified - * annotation type and attributes map. + * Create a new {@link MergedAnnotation} instance of the specified + * annotation type with attributes values supplied by a map. * @param annotationType the annotation type * @param attributes the annotation attributes or {@code null} if just default * values should be used * @return a {@link MergedAnnotation} instance for the annotation and attributes - * @see #from(AnnotatedElement, Class, Map) + * @see #of(AnnotatedElement, Class, Map) */ - static MergedAnnotation from( + static MergedAnnotation of( Class annotationType, @Nullable Map attributes) { - return from(null, annotationType, attributes); + return of(null, annotationType, attributes); } /** - * Create a new {@link MergedAnnotation} instance from the specified - * annotation type and attributes map. + * Create a new {@link MergedAnnotation} instance of the specified + * annotation type with attributes values supplied by a map. * @param source the source for the annotation. This source is used only for * information and logging. It does not need to actually contain * the specified annotations and it will not be searched. @@ -559,10 +559,10 @@ public interface MergedAnnotation { * values should be used * @return a {@link MergedAnnotation} instance for the annotation and attributes */ - static MergedAnnotation from( + static MergedAnnotation of( @Nullable AnnotatedElement source, Class annotationType, @Nullable Map attributes) { - return TypeMappedAnnotation.from(source, annotationType, attributes); + return TypeMappedAnnotation.of(source, annotationType, attributes); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java b/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java index 22bc15c6a4..da348b3416 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java @@ -547,7 +547,7 @@ final class TypeMappedAnnotation extends AbstractMergedAnn return new TypeMappedAnnotation<>(mappings.get(0), source, annotation, ReflectionUtils::invokeMethod, 0); } - static MergedAnnotation from(@Nullable Object source, + static MergedAnnotation of(@Nullable Object source, Class annotationType, @Nullable Map attributes) { Assert.notNull(annotationType, "Annotation type must not be null"); diff --git a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java index 9c1d374491..e07a0cefeb 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java @@ -1262,7 +1262,7 @@ public class MergedAnnotationsTests { @Test public void getDefaultValueFromAnnotationType() { - MergedAnnotation annotation = MergedAnnotation.from(Order.class); + MergedAnnotation annotation = MergedAnnotation.of(Order.class); assertThat(annotation.getDefaultValue("value")).contains( Ordered.LOWEST_PRECEDENCE); } @@ -1681,7 +1681,7 @@ public class MergedAnnotationsTests { Component component = WebController.class.getAnnotation(Component.class); assertThat(component).isNotNull(); Map map = Collections.singletonMap("value", "webController"); - MergedAnnotation annotation = MergedAnnotation.from(Component.class, + MergedAnnotation annotation = MergedAnnotation.of(Component.class, map); Component synthesizedComponent = annotation.synthesize(); assertThat(synthesizedComponent).isInstanceOf(SynthesizedAnnotation.class); @@ -1702,7 +1702,7 @@ public class MergedAnnotationsTests { assertThat(filterMap.get("pattern")).isEqualTo("*Foo"); filterMap.put("pattern", "newFoo"); filterMap.put("enigma", 42); - MergedAnnotation annotation = MergedAnnotation.from( + MergedAnnotation annotation = MergedAnnotation.of( ComponentScanSingleFilter.class, map); ComponentScanSingleFilter synthesizedComponentScan = annotation.synthesize(); assertThat(synthesizedComponentScan).isInstanceOf(SynthesizedAnnotation.class); @@ -1726,7 +1726,7 @@ public class MergedAnnotationsTests { filters[0].put("enigma", 42); filters[1].put("pattern", "newBar"); filters[1].put("enigma", 42); - MergedAnnotation annotation = MergedAnnotation.from( + MergedAnnotation annotation = MergedAnnotation.of( ComponentScan.class, map); ComponentScan synthesizedComponentScan = annotation.synthesize(); assertThat(synthesizedComponentScan).isInstanceOf(SynthesizedAnnotation.class); @@ -1736,7 +1736,7 @@ public class MergedAnnotationsTests { @Test public void synthesizeFromDefaultsWithoutAttributeAliases() throws Exception { - MergedAnnotation annotation = MergedAnnotation.from( + MergedAnnotation annotation = MergedAnnotation.of( AnnotationWithDefaults.class); AnnotationWithDefaults synthesized = annotation.synthesize(); assertThat(synthesized.text()).isEqualTo("enigma"); @@ -1746,7 +1746,7 @@ public class MergedAnnotationsTests { @Test public void synthesizeFromDefaultsWithAttributeAliases() throws Exception { - MergedAnnotation annotation = MergedAnnotation.from( + MergedAnnotation annotation = MergedAnnotation.of( TestConfiguration.class); TestConfiguration synthesized = annotation.synthesize(); assertThat(synthesized.value()).isEqualTo(""); @@ -1764,7 +1764,7 @@ public class MergedAnnotationsTests { public void synthesizeFromMapWithMinimalAttributesWithAttributeAliases() throws Exception { Map map = Collections.singletonMap("location", "test.xml"); - MergedAnnotation annotation = MergedAnnotation.from( + MergedAnnotation annotation = MergedAnnotation.of( TestConfiguration.class, map); TestConfiguration synthesized = annotation.synthesize(); assertThat(synthesized.value()).isEqualTo("test.xml"); @@ -1782,7 +1782,7 @@ public class MergedAnnotationsTests { private void synthesizeFromMapWithAttributeAliasesThatOverrideArraysWithSingleElements( Map map) { - MergedAnnotation annotation = MergedAnnotation.from(GetMapping.class, + MergedAnnotation annotation = MergedAnnotation.of(GetMapping.class, map); GetMapping synthesized = annotation.synthesize(); assertThat(synthesized.value()).isEqualTo("/foo"); @@ -1803,7 +1803,7 @@ public class MergedAnnotationsTests { throws Exception { Map map = Collections.singletonMap(attributeNameAndValue, attributeNameAndValue); - MergedAnnotation annotation = MergedAnnotation.from( + MergedAnnotation annotation = MergedAnnotation.of( ImplicitAliasesTestConfiguration.class, map); ImplicitAliasesTestConfiguration synthesized = annotation.synthesize(); assertThat(synthesized.value()).isEqualTo(attributeNameAndValue); @@ -1828,7 +1828,7 @@ public class MergedAnnotationsTests { private void testMissingTextAttribute(Map attributes) { assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> { - MergedAnnotation annotation = MergedAnnotation.from( + MergedAnnotation annotation = MergedAnnotation.of( AnnotationWithoutDefaults.class, attributes); annotation.synthesize(); }).withMessage("No value found for attribute named 'text' in merged annotation " @@ -1838,7 +1838,7 @@ public class MergedAnnotationsTests { @Test public void synthesizeFromMapWithAttributeOfIncorrectType() throws Exception { Map map = Collections.singletonMap("value", 42L); - MergedAnnotation annotation = MergedAnnotation.from(Component.class, + MergedAnnotation annotation = MergedAnnotation.of(Component.class, map); // annotation.synthesize(); assertThatIllegalStateException().isThrownBy( @@ -1853,7 +1853,7 @@ public class MergedAnnotationsTests { Component component = WebController.class.getAnnotation(Component.class); assertThat(component).isNotNull(); Map attributes = MergedAnnotation.from(component).asMap(); - Component synthesized = MergedAnnotation.from(Component.class, + Component synthesized = MergedAnnotation.of(Component.class, attributes).synthesize(); assertThat(synthesized).isInstanceOf(SynthesizedAnnotation.class); assertThat(synthesized).isEqualTo(component);