Skip to content

Commit 1014fc2

Browse files
prishedkosnicoll
authored andcommitted
Stop using custom name for elements resolution
Previously, when a property was customized with @name, getter, setter, field, and record component were looked up using the custom name, rather than the property name. This lead to missing information as the custom name is not meant to match the field name, for instance. This commit keeps the custom name, but uses the property name as for other cases. See gh-46599 Signed-off-by: Bohdan Pryshedko <[email protected]>
1 parent 449a901 commit 1014fc2

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ private Stream<PropertyDescriptor> resolveConstructorBoundProperties(TypeElement
8686
private PropertyDescriptor extracted(TypeElement declaringElement, TypeElementMembers members,
8787
VariableElement parameter) {
8888
String name = getPropertyName(parameter);
89+
String parameterName = parameter.getSimpleName().toString();
8990
TypeMirror type = parameter.asType();
90-
ExecutableElement getter = members.getPublicGetter(name, type);
91-
ExecutableElement setter = members.getPublicSetter(name, type);
92-
VariableElement field = members.getFields().get(name);
93-
RecordComponentElement recordComponent = members.getRecordComponents().get(name);
91+
ExecutableElement getter = members.getPublicGetter(parameterName, type);
92+
ExecutableElement setter = members.getPublicSetter(parameterName, type);
93+
VariableElement field = members.getFields().get(parameterName);
94+
RecordComponentElement recordComponent = members.getRecordComponents().get(parameterName);
9495
return (recordComponent != null)
9596
? new RecordParameterPropertyDescriptor(name, type, parameter, declaringElement, getter,
9697
recordComponent)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ void deprecatedPropertyOnRecord() {
264264
.fromSource(type)
265265
.withDeprecation("some-reason", null, null));
266266
assertThat(metadata).has(Metadata.withProperty("deprecated-record.bravo", String.class).fromSource(type));
267+
assertThat(metadata).has(Metadata.withProperty("deprecated-record.named.charlie", String.class)
268+
.fromSource(type)
269+
.withDeprecation("another-reason", null, null));
267270
}
268271

269272
@Test
@@ -568,6 +571,8 @@ void recordPropertiesWithDescriptions() {
568571
.withDescription("description without space after asterisk"));
569572
assertThat(metadata).has(Metadata.withProperty("record.descriptions.some-byte", Byte.class)
570573
.withDescription("last description in Javadoc"));
574+
assertThat(metadata).has(Metadata.withProperty("record.descriptions.named.record.component", String.class)
575+
.withDescription("description of a named component"));
571576
}
572577

573578
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/record/ExampleRecord.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.configurationsample.record;
1818

19+
import org.springframework.boot.configurationsample.Name;
20+
1921
// @formatter:off
2022

2123
/**
@@ -26,12 +28,21 @@
2628
* @param someInteger description with @param and @ pitfalls
2729
* @param someBoolean description with extra spaces
2830
* @param someLong description without space after asterisk
31+
* @param namedComponent description of a named component
2932
* @param someByte last description in Javadoc
3033
* @since 1.0.0
3134
* @author Pavel Anisimov
3235
*/
3336
@org.springframework.boot.configurationsample.ConfigurationProperties("record.descriptions")
34-
public record ExampleRecord(String someString, Integer someInteger, Boolean someBoolean, Long someLong, Byte someByte) {
37+
public record ExampleRecord(
38+
String someString,
39+
Integer someInteger,
40+
Boolean someBoolean,
41+
Long someLong,
42+
@Name("named.record.component")
43+
String namedComponent,
44+
Byte someByte
45+
) {
3546
}
3647

3748
//@formatter:on

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/DeprecatedRecord.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@
1818

1919
import org.springframework.boot.configurationsample.ConfigurationProperties;
2020
import org.springframework.boot.configurationsample.DeprecatedConfigurationProperty;
21+
import org.springframework.boot.configurationsample.Name;
2122

2223
/**
2324
* Configuration properties as record with deprecated property.
2425
*
2526
* @param alpha alpha property, deprecated
2627
* @param bravo bravo property
28+
* @param charlie charlie property, named, deprecated
2729
* @author Moritz Halbritter
2830
*/
2931
@ConfigurationProperties("deprecated-record")
30-
public record DeprecatedRecord(String alpha, String bravo) {
32+
public record DeprecatedRecord(String alpha, String bravo, @Name("named.charlie") String charlie) {
3133

3234
@Deprecated
3335
@DeprecatedConfigurationProperty(reason = "some-reason")
3436
public String alpha() {
3537
return this.alpha;
3638
}
3739

40+
@Deprecated
41+
@DeprecatedConfigurationProperty(reason = "another-reason")
42+
public String charlie() {
43+
return this.charlie;
44+
}
3845
}

0 commit comments

Comments
 (0)