Skip to content

Commit 6c8987e

Browse files
jaredtbatessnicoll
authored andcommitted
Allow NestedConfigurationProperty on getters
This commit adds support adding `@NestedConfigurationProperty` on a getter. See gh-38844
1 parent 5d4a777 commit 6c8987e

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ ExecutableElement getSetter() {
5353

5454
@Override
5555
protected boolean isMarkedAsNested(MetadataGenerationEnvironment environment) {
56-
return environment.getNestedConfigurationPropertyAnnotation(this.field) != null;
56+
return environment.getNestedConfigurationPropertyAnnotation(this.field) != null
57+
|| environment.getNestedConfigurationPropertyAnnotation(getGetter()) != null;
5758
}
5859

5960
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.boot.configurationsample.specific.BoxingPojo;
4646
import org.springframework.boot.configurationsample.specific.BuilderPojo;
4747
import org.springframework.boot.configurationsample.specific.DeprecatedLessPreciseTypePojo;
48+
import org.springframework.boot.configurationsample.specific.DeprecatedSimplePojo;
4849
import org.springframework.boot.configurationsample.specific.DeprecatedUnrelatedMethodPojo;
4950
import org.springframework.boot.configurationsample.specific.DoubleRegistrationProperties;
5051
import org.springframework.boot.configurationsample.specific.EmptyDefaultValueProperties;
@@ -336,6 +337,9 @@ void innerClassProperties() {
336337
assertThat(metadata).has(Metadata.withProperty("config.third.value"));
337338
assertThat(metadata).has(Metadata.withProperty("config.fourth"));
338339
assertThat(metadata).isNotEqualTo(Metadata.withGroup("config.fourth"));
340+
assertThat(metadata)
341+
.has(Metadata.withGroup("config.fifth").ofType(DeprecatedSimplePojo.class).fromSource(InnerClassProperties.class));
342+
assertThat(metadata).has(Metadata.withProperty("config.fifth.value").withDeprecation());
339343
}
340344

341345
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @author Phillip Webb
3131
* @since 1.2.0
3232
*/
33-
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT })
33+
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT, ElementType.METHOD })
3434
@Retention(RetentionPolicy.RUNTIME)
3535
@Documented
3636
public @interface NestedConfigurationProperty {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2019 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.specific;
18+
19+
/**
20+
* POJO for use with samples needing a deprecated value.
21+
*
22+
* @author Jared Bates
23+
*/
24+
public class DeprecatedSimplePojo {
25+
26+
private int value;
27+
28+
@Deprecated
29+
public int getValue() {
30+
return this.value;
31+
}
32+
33+
public void setValue(int value) {
34+
this.value = value;
35+
}
36+
37+
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/specific/InnerClassProperties.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class InnerClassProperties {
3636

3737
private Fourth fourth;
3838

39+
private final DeprecatedSimplePojo fifth = new DeprecatedSimplePojo();
40+
3941
public Foo getFirst() {
4042
return this.first;
4143
}
@@ -60,6 +62,11 @@ public void setFourth(Fourth fourth) {
6062
this.fourth = fourth;
6163
}
6264

65+
@NestedConfigurationProperty
66+
public DeprecatedSimplePojo getFifth() {
67+
return this.fifth;
68+
}
69+
6370
public static class Foo {
6471

6572
private String name;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @author Phillip Webb
3939
* @since 1.2.0
4040
*/
41-
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT })
41+
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT, ElementType.METHOD })
4242
@Retention(RetentionPolicy.RUNTIME)
4343
@Documented
4444
@Nested

0 commit comments

Comments
 (0)