Skip to content

Commit 33b48c8

Browse files
committed
Add @name support to the annotation processor
Update the `ConfigurationMetadataAnnotationProcessor` so that `@Name` annotated parameters generate the correct meta-data. Closes gh-22492
1 parent 3aed03b commit 33b48c8

File tree

9 files changed

+168
-10
lines changed

9 files changed

+168
-10
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -81,6 +81,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
8181
static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.actuate."
8282
+ "endpoint.annotation.ReadOperation";
8383

84+
static final String NAME_ANNOTATION = "org.springframework.boot.context.properties.bind.Name";
85+
8486
private static final Set<String> SUPPORTED_OPTIONS = Collections
8587
.unmodifiableSet(Collections.singleton(ADDITIONAL_METADATA_LOCATIONS_OPTION));
8688

@@ -118,6 +120,10 @@ protected String readOperationAnnotation() {
118120
return READ_OPERATION_ANNOTATION;
119121
}
120122

123+
protected String nameAnnotation() {
124+
return NAME_ANNOTATION;
125+
}
126+
121127
@Override
122128
public SourceVersion getSupportedSourceVersion() {
123129
return SourceVersion.latestSupported();
@@ -136,7 +142,7 @@ public synchronized void init(ProcessingEnvironment env) {
136142
this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(),
137143
nestedConfigurationPropertyAnnotation(), deprecatedConfigurationPropertyAnnotation(),
138144
constructorBindingAnnotation(), defaultValueAnnotation(), endpointAnnotation(),
139-
readOperationAnnotation());
145+
readOperationAnnotation(), nameAnnotation());
140146
}
141147

142148
@Override

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -95,10 +95,12 @@ class MetadataGenerationEnvironment {
9595

9696
private final String readOperationAnnotation;
9797

98+
private final String nameAnnotation;
99+
98100
MetadataGenerationEnvironment(ProcessingEnvironment environment, String configurationPropertiesAnnotation,
99101
String nestedConfigurationPropertyAnnotation, String deprecatedConfigurationPropertyAnnotation,
100102
String constructorBindingAnnotation, String defaultValueAnnotation, String endpointAnnotation,
101-
String readOperationAnnotation) {
103+
String readOperationAnnotation, String nameAnnotation) {
102104
this.typeUtils = new TypeUtils(environment);
103105
this.elements = environment.getElementUtils();
104106
this.messager = environment.getMessager();
@@ -110,6 +112,7 @@ class MetadataGenerationEnvironment {
110112
this.defaultValueAnnotation = defaultValueAnnotation;
111113
this.endpointAnnotation = endpointAnnotation;
112114
this.readOperationAnnotation = readOperationAnnotation;
115+
this.nameAnnotation = nameAnnotation;
113116
}
114117

115118
private static FieldValuesParser resolveFieldValuesParser(ProcessingEnvironment env) {
@@ -275,6 +278,10 @@ AnnotationMirror getReadOperationAnnotation(Element element) {
275278
return getAnnotation(element, this.readOperationAnnotation);
276279
}
277280

281+
AnnotationMirror getNameAnnotation(Element element) {
282+
return getAnnotation(element, this.nameAnnotation);
283+
}
284+
278285
boolean hasNullableAnnotation(Element element) {
279286
return getAnnotation(element, NULLABLE_ANNOTATION) != null;
280287
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -22,6 +22,7 @@
2222
import java.util.stream.Collectors;
2323
import java.util.stream.Stream;
2424

25+
import javax.lang.model.element.AnnotationMirror;
2526
import javax.lang.model.element.ExecutableElement;
2627
import javax.lang.model.element.NestingKind;
2728
import javax.lang.model.element.TypeElement;
@@ -76,7 +77,7 @@ Stream<PropertyDescriptor<?>> resolveConstructorProperties(TypeElement type, Exe
7677
TypeElementMembers members, ExecutableElement constructor) {
7778
Map<String, PropertyDescriptor<?>> candidates = new LinkedHashMap<>();
7879
constructor.getParameters().forEach((parameter) -> {
79-
String name = parameter.getSimpleName().toString();
80+
String name = getParameterName(parameter);
8081
TypeMirror propertyType = parameter.asType();
8182
ExecutableElement getter = members.getPublicGetter(name, propertyType);
8283
ExecutableElement setter = members.getPublicSetter(name, propertyType);
@@ -87,6 +88,14 @@ Stream<PropertyDescriptor<?>> resolveConstructorProperties(TypeElement type, Exe
8788
return candidates.values().stream();
8889
}
8990

91+
private String getParameterName(VariableElement parameter) {
92+
AnnotationMirror nameAnnotation = this.environment.getNameAnnotation(parameter);
93+
if (nameAnnotation != null) {
94+
return (String) this.environment.getAnnotationElementValues(nameAnnotation).get("value");
95+
}
96+
return parameter.getSimpleName().toString();
97+
}
98+
9099
Stream<PropertyDescriptor<?>> resolveJavaBeanProperties(TypeElement type, ExecutableElement factoryMethod,
91100
TypeElementMembers members) {
92101
// First check if we have regular java bean properties there
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.configurationprocessor;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
22+
import org.springframework.boot.configurationprocessor.metadata.Metadata;
23+
import org.springframework.boot.configurationsample.immutable.ImmutableNameAnnotationProperties;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Metadata generation tests for immutable properties using {@code @Name}.
29+
*
30+
* @author Phillip Webb
31+
*/
32+
public class ImmutableNameAnnotationPropertiesTests extends AbstractMetadataGenerationTests {
33+
34+
@Test
35+
void immutableNameAnnotationProperties() {
36+
ConfigurationMetadata metadata = compile(ImmutableNameAnnotationProperties.class);
37+
assertThat(metadata).has(Metadata.withProperty("named.import", String.class)
38+
.fromSource(ImmutableNameAnnotationProperties.class));
39+
}
40+
41+
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironmentFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -38,7 +38,8 @@ public MetadataGenerationEnvironment apply(ProcessingEnvironment environment) {
3838
TestConfigurationMetadataAnnotationProcessor.CONSTRUCTOR_BINDING_ANNOTATION,
3939
TestConfigurationMetadataAnnotationProcessor.DEFAULT_VALUE_ANNOTATION,
4040
TestConfigurationMetadataAnnotationProcessor.ENDPOINT_ANNOTATION,
41-
TestConfigurationMetadataAnnotationProcessor.READ_OPERATION_ANNOTATION);
41+
TestConfigurationMetadataAnnotationProcessor.READ_OPERATION_ANNOTATION,
42+
TestConfigurationMetadataAnnotationProcessor.NAME_ANNOTATION);
4243
}
4344

4445
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolverTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -36,6 +36,7 @@
3636
import org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor;
3737
import org.springframework.boot.configurationsample.immutable.ImmutableClassConstructorBindingProperties;
3838
import org.springframework.boot.configurationsample.immutable.ImmutableMultiConstructorProperties;
39+
import org.springframework.boot.configurationsample.immutable.ImmutableNameAnnotationProperties;
3940
import org.springframework.boot.configurationsample.immutable.ImmutableSimpleProperties;
4041
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
4142
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
@@ -144,6 +145,12 @@ void propertiesWithMultiConstructorNoDirective() throws IOException {
144145
properties((stream) -> assertThat(stream).element(0).isInstanceOf(JavaBeanPropertyDescriptor.class)));
145146
}
146147

148+
@Test
149+
void propertiesWithNameAnnotationParameter() throws IOException {
150+
process(ImmutableNameAnnotationProperties.class,
151+
propertyNames((stream) -> assertThat(stream).containsExactly("import")));
152+
}
153+
147154
private BiConsumer<TypeElement, MetadataGenerationEnvironment> properties(
148155
Consumer<Stream<PropertyDescriptor<?>>> stream) {
149156
return (element, metadataEnv) -> {

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestConfigurationMetadataAnnotationProcessor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -55,6 +55,8 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM
5555

5656
public static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.configurationsample.ReadOperation";
5757

58+
public static final String NAME_ANNOTATION = "org.springframework.boot.configurationsample.Name";
59+
5860
private ConfigurationMetadata metadata;
5961

6062
private final File outputLocation;
@@ -98,6 +100,11 @@ protected String readOperationAnnotation() {
98100
return READ_OPERATION_ANNOTATION;
99101
}
100102

103+
@Override
104+
protected String nameAnnotation() {
105+
return NAME_ANNOTATION;
106+
}
107+
101108
@Override
102109
protected ConfigurationMetadata writeMetaData() throws Exception {
103110
super.writeMetaData();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.configurationsample;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* Alternative to Spring Boot's {@code @Name} for testing (removes the need for a
27+
* dependency on the real annotation).
28+
*
29+
* @author Phillip Webb
30+
*/
31+
@Target(ElementType.PARAMETER)
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Documented
34+
public @interface Name {
35+
36+
String value();
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.configurationsample.immutable;
18+
19+
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
import org.springframework.boot.configurationsample.ConstructorBinding;
21+
import org.springframework.boot.configurationsample.Name;
22+
23+
/**
24+
* Immutable properties making use of {@code @Name}.
25+
*
26+
* @author Phillip Webb
27+
*/
28+
@ConfigurationProperties("named")
29+
@ConstructorBinding
30+
public class ImmutableNameAnnotationProperties {
31+
32+
private String imports;
33+
34+
public ImmutableNameAnnotationProperties(@Name("import") String imports) {
35+
this.imports = imports;
36+
}
37+
38+
public String getImports() {
39+
return this.imports;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)