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.
This commit is contained in:
Phillip Webb 2019-05-06 09:51:26 -07:00 committed by Juergen Hoeller
parent 3b145a5a73
commit d4a761abea
4 changed files with 28 additions and 28 deletions

View File

@ -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<String, Object> 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<A> 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);

View File

@ -523,34 +523,34 @@ public interface MergedAnnotation<A extends Annotation> {
}
/**
* 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 <A extends Annotation> MergedAnnotation<A> from(Class<A> annotationType) {
return from(null, annotationType, null);
static <A extends Annotation> MergedAnnotation<A> of(Class<A> 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 <A extends Annotation> MergedAnnotation<A> from(
static <A extends Annotation> MergedAnnotation<A> of(
Class<A> annotationType, @Nullable Map<String, ?> 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 <em>actually</em> contain
* the specified annotations and it will not be searched.
@ -559,10 +559,10 @@ public interface MergedAnnotation<A extends Annotation> {
* values should be used
* @return a {@link MergedAnnotation} instance for the annotation and attributes
*/
static <A extends Annotation> MergedAnnotation<A> from(
static <A extends Annotation> MergedAnnotation<A> of(
@Nullable AnnotatedElement source, Class<A> annotationType, @Nullable Map<String, ?> attributes) {
return TypeMappedAnnotation.from(source, annotationType, attributes);
return TypeMappedAnnotation.of(source, annotationType, attributes);
}

View File

@ -547,7 +547,7 @@ final class TypeMappedAnnotation<A extends Annotation> extends AbstractMergedAnn
return new TypeMappedAnnotation<>(mappings.get(0), source, annotation, ReflectionUtils::invokeMethod, 0);
}
static <A extends Annotation> MergedAnnotation<A> from(@Nullable Object source,
static <A extends Annotation> MergedAnnotation<A> of(@Nullable Object source,
Class<A> annotationType, @Nullable Map<String, ?> attributes) {
Assert.notNull(annotationType, "Annotation type must not be null");

View File

@ -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<String, Object> map = Collections.singletonMap("value", "webController");
MergedAnnotation<Component> annotation = MergedAnnotation.from(Component.class,
MergedAnnotation<Component> 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<ComponentScanSingleFilter> annotation = MergedAnnotation.from(
MergedAnnotation<ComponentScanSingleFilter> 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<ComponentScan> annotation = MergedAnnotation.from(
MergedAnnotation<ComponentScan> 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<AnnotationWithDefaults> annotation = MergedAnnotation.from(
MergedAnnotation<AnnotationWithDefaults> 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<TestConfiguration> annotation = MergedAnnotation.from(
MergedAnnotation<TestConfiguration> 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<String, Object> map = Collections.singletonMap("location", "test.xml");
MergedAnnotation<TestConfiguration> annotation = MergedAnnotation.from(
MergedAnnotation<TestConfiguration> 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<String, Object> map) {
MergedAnnotation<GetMapping> annotation = MergedAnnotation.from(GetMapping.class,
MergedAnnotation<GetMapping> 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<String, Object> map = Collections.singletonMap(attributeNameAndValue,
attributeNameAndValue);
MergedAnnotation<ImplicitAliasesTestConfiguration> annotation = MergedAnnotation.from(
MergedAnnotation<ImplicitAliasesTestConfiguration> 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<String, Object> attributes) {
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> {
MergedAnnotation<AnnotationWithoutDefaults> annotation = MergedAnnotation.from(
MergedAnnotation<AnnotationWithoutDefaults> 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<String, Object> map = Collections.singletonMap("value", 42L);
MergedAnnotation<Component> annotation = MergedAnnotation.from(Component.class,
MergedAnnotation<Component> 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<String, Object> 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);