Skip to content

Commit 06b81cf

Browse files
committed
Use @AliasFor when applicable
This commit adds `@AliasFor` meta-data to annotations that declare an alias attribute. `@ConditionalOnProperty` and `@AutoconfigureRestDocs` were not migrated due to the use of `AnnotationMetadata#getAnnotationAttributes`. Closes spring-projectsgh-5187
1 parent 62a41ae commit 06b81cf

File tree

13 files changed

+36
-48
lines changed

13 files changed

+36
-48
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ private String extractPrefix(ApplicationContext context,
220220
annotation = override;
221221
}
222222
}
223-
return (StringUtils.hasLength(annotation.value()) ? annotation.value()
224-
: annotation.prefix());
223+
return annotation.prefix();
225224
}
226225

227226
/**

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/MvcEndpoints.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.core.annotation.AnnotationUtils;
3131
import org.springframework.core.env.Environment;
3232
import org.springframework.stereotype.Component;
33-
import org.springframework.util.StringUtils;
3433

3534
/**
3635
* A registry for all {@link MvcEndpoint} beans, and a factory for a set of generic ones
@@ -102,9 +101,7 @@ private String determinePath(Endpoint<?> endpoint, Environment environment) {
102101
ConfigurationProperties configurationProperties = AnnotationUtils
103102
.findAnnotation(endpoint.getClass(), ConfigurationProperties.class);
104103
if (configurationProperties != null) {
105-
String prefix = StringUtils.hasText(configurationProperties.prefix())
106-
? configurationProperties.prefix() : configurationProperties.value();
107-
return environment.getProperty(prefix + ".path");
104+
return environment.getProperty(configurationProperties.prefix() + ".path");
108105
}
109106
return null;
110107
}

spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25+
import org.springframework.core.annotation.AliasFor;
26+
2527
/**
2628
* Annotation for externalized configuration. Add this to a class definition or a
2729
* {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate
@@ -44,13 +46,15 @@
4446
* for {@link #prefix()}.
4547
* @return the name prefix of the properties to bind
4648
*/
49+
@AliasFor("prefix")
4750
String value() default "";
4851

4952
/**
5053
* The name prefix of the properties that are valid to bind to this object. Synonym
5154
* for {@link #value()}.
5255
* @return the name prefix of the properties to bind
5356
*/
57+
@AliasFor("value")
5458
String prefix() default "";
5559

5660
/**

spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,8 @@ private void postProcessBeforeInitialization(Object bean, String beanName,
325325
factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
326326
factory.setExceptionIfInvalid(annotation.exceptionIfInvalid());
327327
factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
328-
String targetName = (StringUtils.hasLength(annotation.value())
329-
? annotation.value() : annotation.prefix());
330-
if (StringUtils.hasLength(targetName)) {
331-
factory.setTargetName(targetName);
328+
if (StringUtils.hasLength(annotation.prefix())) {
329+
factory.setTargetName(annotation.prefix());
332330
}
333331
}
334332
try {
@@ -346,8 +344,7 @@ private String getAnnotationDetails(ConfigurationProperties annotation) {
346344
return "";
347345
}
348346
StringBuilder details = new StringBuilder();
349-
details.append("prefix=").append((StringUtils.hasLength(annotation.value())
350-
? annotation.value() : annotation.prefix()));
347+
details.append("prefix=").append(annotation.prefix());
351348
details.append(", ignoreInvalidFields=").append(annotation.ignoreInvalidFields());
352349
details.append(", ignoreUnknownFields=").append(annotation.ignoreUnknownFields());
353350
details.append(", ignoreNestedProperties=")

spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ private String extractPrefix(Class<?> type) {
8888
ConfigurationProperties annotation = AnnotationUtils.findAnnotation(type,
8989
ConfigurationProperties.class);
9090
if (annotation != null) {
91-
return (StringUtils.hasLength(annotation.value()) ? annotation.value()
92-
: annotation.prefix());
91+
return annotation.prefix();
9392
}
9493
return "";
9594
}

spring-boot/src/main/java/org/springframework/boot/context/scan/AbstractEntityScanRegistrar.java

-10
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
3030
import org.springframework.core.annotation.AnnotationAttributes;
3131
import org.springframework.core.type.AnnotationMetadata;
32-
import org.springframework.util.Assert;
3332
import org.springframework.util.ClassUtils;
34-
import org.springframework.util.ObjectUtils;
3533

3634
/**
3735
* A base {@link ImportBeanDefinitionRegistrar} used to collect the packages to scan for a
@@ -88,17 +86,9 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
8886
protected Set<String> getPackagesToScan(AnnotationMetadata metadata) {
8987
AnnotationAttributes attributes = AnnotationAttributes
9088
.fromMap(metadata.getAnnotationAttributes(this.annotationType.getName()));
91-
String[] value = attributes.getStringArray("value");
9289
String[] basePackages = attributes.getStringArray("basePackages");
9390
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
94-
if (!ObjectUtils.isEmpty(value)) {
95-
Assert.state(ObjectUtils.isEmpty(basePackages),
96-
String.format(
97-
"@%s basePackages and value attributes are mutually exclusive",
98-
this.annotationType.getSimpleName()));
99-
}
10091
Set<String> packagesToScan = new LinkedHashSet<String>();
101-
packagesToScan.addAll(Arrays.asList(value));
10292
packagesToScan.addAll(Arrays.asList(basePackages));
10393
for (Class<?> basePackageClass : basePackageClasses) {
10494
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));

spring-boot/src/main/java/org/springframework/boot/neo4j/NodeEntityScan.java

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.neo4j.ogm.session.SessionFactory;
2727

2828
import org.springframework.context.annotation.Import;
29+
import org.springframework.core.annotation.AliasFor;
2930

3031
/**
3132
* Configures the {@link SessionFactory} to scan for Neo4J {@link NodeEntity} classes in
@@ -58,6 +59,7 @@
5859
* {@code @NodeEntityScan(basePackages="org.my.pkg")}.
5960
* @return the base packages to scan
6061
*/
62+
@AliasFor("basePackages")
6163
String[] value() default {};
6264

6365
/**
@@ -68,6 +70,7 @@
6870
* package names.
6971
* @return the base packages to scan
7072
*/
73+
@AliasFor("value")
7174
String[] basePackages() default {};
7275

7376
/**

spring-boot/src/main/java/org/springframework/boot/orm/jpa/EntityScan.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.context.annotation.Import;
26+
import org.springframework.core.annotation.AliasFor;
2627
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
2728

2829
/**
@@ -55,6 +56,7 @@
5556
* {@code @EntityScan(basePackages="org.my.pkg")}.
5657
* @return the base packages to scan
5758
*/
59+
@AliasFor("basePackages")
5860
String[] value() default {};
5961

6062
/**
@@ -65,6 +67,7 @@
6567
* package names.
6668
* @return the base packages to scan
6769
*/
70+
@AliasFor("value")
6871
String[] basePackages() default {};
6972

7073
/**

spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScan.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import javax.servlet.annotation.WebServlet;
2828

2929
import org.springframework.context.annotation.Import;
30+
import org.springframework.core.annotation.AliasFor;
3031

3132
/**
3233
* Enables scanning for Servlet components ({@link WebFilter filters}, {@link WebServlet
@@ -55,6 +56,7 @@
5556
* {@code @ServletComponentScan(basePackages="org.my.pkg")}.
5657
* @return the base packages to scan
5758
*/
59+
@AliasFor("basePackages")
5860
String[] value() default {};
5961

6062
/**
@@ -65,6 +67,7 @@
6567
* package names.
6668
* @return the base packages to scan
6769
*/
70+
@AliasFor("value")
6871
String[] basePackages() default {};
6972

7073
/**

spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,14 +28,13 @@
2828
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
2929
import org.springframework.core.annotation.AnnotationAttributes;
3030
import org.springframework.core.type.AnnotationMetadata;
31-
import org.springframework.util.Assert;
3231
import org.springframework.util.ClassUtils;
33-
import org.springframework.util.ObjectUtils;
3432

3533
/**
3634
* {@link ImportBeanDefinitionRegistrar} used by {@link ServletComponentScan}.
3735
*
3836
* @author Andy Wilkinson
37+
* @author Stephane Nicoll
3938
*/
4039
class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
4140

@@ -77,16 +76,9 @@ private void addPostProcessor(BeanDefinitionRegistry registry,
7776
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
7877
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
7978
metadata.getAnnotationAttributes(ServletComponentScan.class.getName()));
80-
String[] value = attributes.getStringArray("value");
8179
String[] basePackages = attributes.getStringArray("basePackages");
8280
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
83-
if (!ObjectUtils.isEmpty(value)) {
84-
Assert.state(ObjectUtils.isEmpty(basePackages),
85-
"@ServletComponentScan basePackages and value attributes are"
86-
+ " mutually exclusive");
87-
}
8881
Set<String> packagesToScan = new LinkedHashSet<String>();
89-
packagesToScan.addAll(Arrays.asList(value));
9082
packagesToScan.addAll(Arrays.asList(basePackages));
9183
for (Class<?> basePackageClass : basePackageClasses) {
9284
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));

spring-boot/src/test/java/org/springframework/boot/context/scan/TestEntityScan.java

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.context.annotation.Import;
26+
import org.springframework.core.annotation.AliasFor;
2627

2728
/**
2829
* EntityScan test annotation.
@@ -35,8 +36,10 @@
3536
@Import(TestEntityScanRegistrar.class)
3637
public @interface TestEntityScan {
3738

39+
@AliasFor("basePackages")
3840
String[] value() default {};
3941

42+
@AliasFor("value")
4043
String[] basePackages() default {};
4144

4245
Class<?>[] basePackageClasses() default {};

spring-boot/src/test/java/org/springframework/boot/context/scan/TestEntityScanTests.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.core.annotation.AnnotationConfigurationException;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
3031

@@ -75,10 +76,11 @@ public void fromConfigurationClass() throws Exception {
7576

7677
@Test
7778
public void valueAndBasePackagesThrows() throws Exception {
78-
this.thrown.expect(IllegalStateException.class);
79-
this.thrown.expectMessage("@TestEntityScan basePackages and value "
80-
+ "attributes are mutually exclusive");
81-
this.context = new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
79+
this.thrown.expect(AnnotationConfigurationException.class);
80+
this.thrown.expectMessage("attribute 'value' and its alias 'basePackages' are declared");
81+
this.thrown.expectMessage("com.mycorp.entity");
82+
this.thrown.expectMessage("com.mycorp");
83+
new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
8284
}
8385

8486
@Test

spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2525
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.core.annotation.AnnotationConfigurationException;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
2829

@@ -74,14 +75,9 @@ public void packagesConfiguredWithBasePackageClasses() {
7475

7576
@Test
7677
public void packagesConfiguredWithBothValueAndBasePackages() {
77-
this.thrown.expect(IllegalStateException.class);
78-
this.thrown.expectMessage("@ServletComponentScan basePackages and value"
79-
+ " attributes are mutually exclusive");
80-
this.context = new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
81-
ServletComponentRegisteringPostProcessor postProcessor = this.context
82-
.getBean(ServletComponentRegisteringPostProcessor.class);
83-
assertThat(postProcessor.getPackagesToScan())
84-
.contains(getClass().getPackage().getName());
78+
this.thrown.expect(AnnotationConfigurationException.class);
79+
this.thrown.expectMessage("attribute 'value' and its alias 'basePackages' are declared");
80+
new AnnotationConfigApplicationContext(ValueAndBasePackages.class);
8581
}
8682

8783
@Test

0 commit comments

Comments
 (0)