Skip to content

Commit b190e80

Browse files
committed
Consistently detect the default value of a property
This is a follow-up of gh-46599 where detecting the default value suffer from the same problem. Rather than using a name, the resolver now provides the field itself. This also adds a dedicated test for the use of @name with Lombok. Closes gh-46662
1 parent 14820b0 commit b190e80

File tree

9 files changed

+156
-30
lines changed

9 files changed

+156
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected String resolveDescription(MetadataGenerationEnvironment environment) {
6464

6565
@Override
6666
protected Object resolveDefaultValue(MetadataGenerationEnvironment environment) {
67-
return environment.getFieldDefaultValue(getDeclaringElement(), getName());
67+
return environment.getFieldDefaultValue(getDeclaringElement(), this.field);
6868
}
6969

7070
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected String resolveDescription(MetadataGenerationEnvironment environment) {
7676

7777
@Override
7878
protected Object resolveDefaultValue(MetadataGenerationEnvironment environment) {
79-
return environment.getFieldDefaultValue(getDeclaringElement(), getName());
79+
return environment.getFieldDefaultValue(getDeclaringElement(), this.field);
8080
}
8181

8282
@Override

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,15 @@ Messager getMessager() {
132132
}
133133

134134
/**
135-
* Return the default value of the field with the specified {@code name}.
135+
* Return the default value of the given {@code field}.
136136
* @param type the type to consider
137-
* @param name the name of the field
137+
* @param field the field or {@code null} if it is not available
138138
* @return the default value or {@code null} if the field does not exist or no default
139139
* value has been detected
140140
*/
141-
Object getFieldDefaultValue(TypeElement type, String name) {
142-
return this.defaultValues.computeIfAbsent(type, this::resolveFieldValues).get(name);
141+
Object getFieldDefaultValue(TypeElement type, VariableElement field) {
142+
return (field != null) ? this.defaultValues.computeIfAbsent(type, this::resolveFieldValues)
143+
.get(field.getSimpleName().toString()) : null;
143144
}
144145

145146
boolean isExcluded(TypeMirror type) {

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

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,71 @@
2020

2121
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
2222
import org.springframework.boot.configurationprocessor.metadata.Metadata;
23-
import org.springframework.boot.configurationsample.immutable.ConstructorParameterNameAnnotationProperties;
24-
import org.springframework.boot.configurationsample.immutable.JavaBeanNameAnnotationProperties;
25-
import org.springframework.boot.configurationsample.immutable.RecordComponentNameAnnotationProperties;
23+
import org.springframework.boot.configurationsample.name.ConstructorParameterNameAnnotationProperties;
24+
import org.springframework.boot.configurationsample.name.JavaBeanNameAnnotationProperties;
25+
import org.springframework.boot.configurationsample.name.LombokNameAnnotationProperties;
26+
import org.springframework.boot.configurationsample.name.RecordComponentNameAnnotationProperties;
2627

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

2930
/**
3031
* Metadata generation tests for using {@code @Name}.
3132
*
3233
* @author Phillip Webb
34+
* @author Stephane Nicoll
3335
*/
3436
class NameAnnotationPropertiesTests extends AbstractMetadataGenerationTests {
3537

3638
@Test
3739
void constructorParameterNameAnnotationProperties() {
3840
ConfigurationMetadata metadata = compile(ConstructorParameterNameAnnotationProperties.class);
39-
assertThat(metadata).has(Metadata.withProperty("named.import", String.class)
40-
.fromSource(ConstructorParameterNameAnnotationProperties.class));
41+
assertThat(metadata)
42+
.has(Metadata.withProperty("named.import", String.class)
43+
.fromSource(ConstructorParameterNameAnnotationProperties.class)
44+
.withDescription("Imports to apply."))
45+
.has(Metadata.withProperty("named.default", Boolean.class)
46+
.fromSource(ConstructorParameterNameAnnotationProperties.class)
47+
.withDefaultValue("Whether default mode is enabled.")
48+
.withDefaultValue(true));
4149
}
4250

4351
@Test
4452
void recordComponentNameAnnotationProperties() {
4553
ConfigurationMetadata metadata = compile(RecordComponentNameAnnotationProperties.class);
46-
assertThat(metadata).has(Metadata.withProperty("named.import", String.class)
47-
.fromSource(RecordComponentNameAnnotationProperties.class));
54+
assertThat(metadata)
55+
.has(Metadata.withProperty("named.import", String.class)
56+
.fromSource(RecordComponentNameAnnotationProperties.class)
57+
.withDescription("Imports to apply."))
58+
.has(Metadata.withProperty("named.default", Boolean.class)
59+
.fromSource(RecordComponentNameAnnotationProperties.class)
60+
.withDefaultValue("Whether default mode is enabled.")
61+
.withDefaultValue(true));
4862
}
4963

5064
@Test
5165
void javaBeanNameAnnotationProperties() {
5266
ConfigurationMetadata metadata = compile(JavaBeanNameAnnotationProperties.class);
53-
assertThat(metadata).has(
54-
Metadata.withProperty("named.import", String.class).fromSource(JavaBeanNameAnnotationProperties.class));
67+
assertThat(metadata)
68+
.has(Metadata.withProperty("named.import", String.class)
69+
.fromSource(JavaBeanNameAnnotationProperties.class)
70+
.withDescription("Imports to apply."))
71+
.has(Metadata.withProperty("named.default", Boolean.class)
72+
.fromSource(JavaBeanNameAnnotationProperties.class)
73+
.withDefaultValue("Whether default mode is enabled.")
74+
.withDefaultValue(true));
75+
}
76+
77+
@Test
78+
void lombokNameAnnotationProperties() {
79+
ConfigurationMetadata metadata = compile(LombokNameAnnotationProperties.class);
80+
assertThat(metadata)
81+
.has(Metadata.withProperty("named.import", String.class)
82+
.fromSource(LombokNameAnnotationProperties.class)
83+
.withDescription("Imports to apply."))
84+
.has(Metadata.withProperty("named.default", Boolean.class)
85+
.fromSource(LombokNameAnnotationProperties.class)
86+
.withDefaultValue("Whether default mode is enabled.")
87+
.withDefaultValue(true));
5588
}
5689

5790
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@
3131
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
3232
import org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester;
3333
import org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor;
34-
import org.springframework.boot.configurationsample.immutable.ConstructorParameterNameAnnotationProperties;
3534
import org.springframework.boot.configurationsample.immutable.ImmutableClassConstructorBindingProperties;
3635
import org.springframework.boot.configurationsample.immutable.ImmutableDeducedConstructorBindingProperties;
3736
import org.springframework.boot.configurationsample.immutable.ImmutableMultiConstructorProperties;
3837
import org.springframework.boot.configurationsample.immutable.ImmutableSimpleProperties;
39-
import org.springframework.boot.configurationsample.immutable.JavaBeanNameAnnotationProperties;
40-
import org.springframework.boot.configurationsample.immutable.RecordComponentNameAnnotationProperties;
4138
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
4239
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
4340
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
4441
import org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties;
42+
import org.springframework.boot.configurationsample.name.ConstructorParameterNameAnnotationProperties;
43+
import org.springframework.boot.configurationsample.name.JavaBeanNameAnnotationProperties;
44+
import org.springframework.boot.configurationsample.name.LombokNameAnnotationProperties;
45+
import org.springframework.boot.configurationsample.name.RecordComponentNameAnnotationProperties;
4546
import org.springframework.boot.configurationsample.simple.AutowiredProperties;
4647
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
4748
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesGrandparent;
@@ -162,21 +163,27 @@ void propertiesWithMultiConstructorNoDirective() {
162163
}
163164

164165
@Test
165-
void contructorParameterPropertyWithNameAnnotationParameter() {
166+
void constructorParameterPropertyWithNameAnnotationParameter() {
166167
process(ConstructorParameterNameAnnotationProperties.class,
167-
propertyNames((stream) -> assertThat(stream).containsExactly("import")));
168+
propertyNames((stream) -> assertThat(stream).containsOnly("import", "default")));
168169
}
169170

170171
@Test
171172
void recordComponentPropertyWithNameAnnotationParameter() {
172173
process(RecordComponentNameAnnotationProperties.class,
173-
propertyNames((stream) -> assertThat(stream).containsExactly("import")));
174+
propertyNames((stream) -> assertThat(stream).containsOnly("import", "default")));
174175
}
175176

176177
@Test
177178
void javaBeanPropertyWithNameAnnotationParameter() {
178179
process(JavaBeanNameAnnotationProperties.class,
179-
propertyNames((stream) -> assertThat(stream).containsExactly("import")));
180+
propertyNames((stream) -> assertThat(stream).containsOnly("import", "default")));
181+
}
182+
183+
@Test
184+
void lombokPropertyWithNameAnnotationParameter() {
185+
process(LombokNameAnnotationProperties.class,
186+
propertyNames((stream) -> assertThat(stream).containsOnly("import", "default")));
180187
}
181188

182189
private BiConsumer<TypeElement, MetadataGenerationEnvironment> properties(
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,43 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.configurationsample.immutable;
17+
package org.springframework.boot.configurationsample.name;
1818

1919
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
import org.springframework.boot.configurationsample.DefaultValue;
2021
import org.springframework.boot.configurationsample.Name;
2122

2223
/**
23-
* Immutable class properties making use of {@code @Name}.
24+
* Constructor properties making use of {@code @Name}.
2425
*
2526
* @author Phillip Webb
27+
* @author Stephane Nicoll
2628
*/
2729
@ConfigurationProperties("named")
2830
public class ConstructorParameterNameAnnotationProperties {
2931

32+
/**
33+
* Imports to apply.
34+
*/
3035
private final String imports;
3136

32-
public ConstructorParameterNameAnnotationProperties(@Name("import") String imports) {
37+
/**
38+
* Whether default mode is enabled.
39+
*/
40+
private final boolean defaultValue;
41+
42+
public ConstructorParameterNameAnnotationProperties(@Name("import") String imports,
43+
@Name("default") @DefaultValue("true") boolean defaultValue) {
3344
this.imports = imports;
45+
this.defaultValue = defaultValue;
3446
}
3547

3648
public String getImports() {
3749
return this.imports;
3850
}
3951

52+
public boolean isDefaultValue() {
53+
return this.defaultValue;
54+
}
55+
4056
}
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.configurationsample.immutable;
17+
package org.springframework.boot.configurationsample.name;
1818

1919
import org.springframework.boot.configurationsample.ConfigurationProperties;
2020
import org.springframework.boot.configurationsample.Name;
@@ -23,13 +23,23 @@
2323
* Java bean properties making use of {@code @Name}.
2424
*
2525
* @author Andy Wilkinson
26+
* @author Stephane Nicoll
2627
*/
2728
@ConfigurationProperties("named")
2829
public class JavaBeanNameAnnotationProperties {
2930

31+
/**
32+
* Imports to apply.
33+
*/
3034
@Name("import")
3135
private String imports;
3236

37+
/**
38+
* Whether default mode is enabled.
39+
*/
40+
@Name("default")
41+
private boolean defaultValue = true;
42+
3343
public String getImports() {
3444
return this.imports;
3545
}
@@ -38,4 +48,12 @@ public void setImports(String imports) {
3848
this.imports = imports;
3949
}
4050

51+
public boolean isDefaultValue() {
52+
return this.defaultValue;
53+
}
54+
55+
public void setDefaultValue(boolean defaultValue) {
56+
this.defaultValue = defaultValue;
57+
}
58+
4159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-present 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.name;
18+
19+
import lombok.Getter;
20+
import lombok.Setter;
21+
22+
import org.springframework.boot.configurationsample.ConfigurationProperties;
23+
import org.springframework.boot.configurationsample.Name;
24+
25+
/**
26+
* Lombok properties making use of {@code @Name}.
27+
*
28+
* @author Stephane Nicoll
29+
*/
30+
@Getter
31+
@Setter
32+
@ConfigurationProperties("named")
33+
public class LombokNameAnnotationProperties {
34+
35+
/**
36+
* Imports to apply.
37+
*/
38+
@Name("import")
39+
private String imports;
40+
41+
/**
42+
* Whether default mode is enabled.
43+
*/
44+
@Name("default")
45+
private boolean defaultValue = true;
46+
47+
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.configurationsample.immutable;
17+
package org.springframework.boot.configurationsample.name;
1818

1919
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
import org.springframework.boot.configurationsample.DefaultValue;
2021
import org.springframework.boot.configurationsample.Name;
2122

2223
/**
23-
* Immutable record properties making use of {@code @Name}.
24+
* Record properties making use of {@code @Name}.
2425
*
25-
* @param imports some imports
26+
* @param imports Imports to apply.
27+
* @param defaultValue Whether default mode is enabled.
2628
* @author Andy Wilkinson
29+
* @author Stephane Nicoll
2730
*/
2831
@ConfigurationProperties("named")
29-
public record RecordComponentNameAnnotationProperties(@Name("import") String imports) {
32+
public record RecordComponentNameAnnotationProperties(@Name("import") String imports,
33+
@Name("default") @DefaultValue("true") boolean defaultValue) {
3034

3135
}

0 commit comments

Comments
 (0)