Skip to content

Commit 7879adf

Browse files
committed
Merge pull request #26337 from mjeffrey
* pr/26337: Polish "Add support for @value annotation" Add support for @value annotation Closes gh-26337
2 parents ffe2d43 + 69c2621 commit 7879adf

File tree

6 files changed

+88
-6
lines changed

6 files changed

+88
-6
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/configuration-metadata/annotation-processor.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The processor picks up both classes and methods that are annotated with `@Config
6565

6666
If the class is also annotated with `@ConstructorBinding`, a single constructor is expected and one property is created per constructor parameter.
6767
Otherwise, properties are discovered through the presence of standard getters and setters with special handling for collection and map types (that is detected even if only a getter is present).
68-
The annotation processor also supports the use of the `@Data`, `@Getter`, and `@Setter` lombok annotations.
68+
The annotation processor also supports the use of the `@Data`, `@Value`, `@Getter`, and `@Setter` lombok annotations.
6969

7070
Consider the following example:
7171

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

Lines changed: 5 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-2021 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,8 @@ class LombokPropertyDescriptor extends PropertyDescriptor<VariableElement> {
3636

3737
private static final String LOMBOK_DATA_ANNOTATION = "lombok.Data";
3838

39+
private static final String LOMBOK_VALUE_ANNOTATION = "lombok.Value";
40+
3941
private static final String LOMBOK_GETTER_ANNOTATION = "lombok.Getter";
4042

4143
private static final String LOMBOK_SETTER_ANNOTATION = "lombok.Setter";
@@ -100,7 +102,8 @@ private boolean hasLombokPublicAccessor(MetadataGenerationEnvironment env, boole
100102
if (lombokMethodAnnotationOnElement != null) {
101103
return isAccessLevelPublic(env, lombokMethodAnnotationOnElement);
102104
}
103-
return (env.getAnnotation(getOwnerElement(), LOMBOK_DATA_ANNOTATION) != null);
105+
return (env.hasAnnotation(getOwnerElement(), LOMBOK_DATA_ANNOTATION)
106+
|| env.hasAnnotation(getOwnerElement(), LOMBOK_VALUE_ANNOTATION));
104107
}
105108

106109
private boolean isAccessLevelPublic(MetadataGenerationEnvironment env, AnnotationMirror lombokAnnotation) {

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokMetadataGenerationTests.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-2021 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.
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.configurationsample.lombok.LombokInnerClassWithGetterProperties;
3030
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
3131
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
32+
import org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties;
3233
import org.springframework.boot.configurationsample.lombok.SimpleLombokPojo;
3334

3435
import static org.assertj.core.api.Assertions.assertThat;
@@ -46,6 +47,12 @@ void lombokDataProperties() {
4647
assertSimpleLombokProperties(metadata, LombokSimpleDataProperties.class, "data");
4748
}
4849

50+
@Test
51+
void lombokValueProperties() {
52+
ConfigurationMetadata metadata = compile(LombokSimpleValueProperties.class);
53+
assertSimpleLombokProperties(metadata, LombokSimpleValueProperties.class, "value");
54+
}
55+
4956
@Test
5057
void lombokSimpleProperties() {
5158
ConfigurationMetadata metadata = compile(LombokSimpleProperties.class);

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

Lines changed: 12 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-2021 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.
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.configurationsample.lombok.LombokInnerClassProperties;
3131
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
3232
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
33+
import org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties;
3334
import org.springframework.boot.configurationsample.simple.SimpleProperties;
3435
import org.springframework.boot.configurationsample.specific.InnerClassProperties;
3536

@@ -114,6 +115,16 @@ void lombokSimplePropertyWithOnlyGetterOnDataClassShouldNotBeExposed() throws IO
114115
});
115116
}
116117

118+
@Test
119+
void lombokSimplePropertyWithOnlyGetterOnValueClassShouldNotBeExposed() throws IOException {
120+
process(LombokSimpleValueProperties.class, (roundEnv, metadataEnv) -> {
121+
TypeElement ownerElement = roundEnv.getRootElement(LombokSimpleValueProperties.class);
122+
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement, "ignored");
123+
assertThat(property.isProperty(metadataEnv)).isFalse();
124+
assertThat(property.isNested(metadataEnv)).isFalse();
125+
});
126+
}
127+
117128
@Test
118129
void lombokSimplePropertyWithOnlyGetterOnFieldShouldNotBeExposed() throws IOException {
119130
process(LombokExplicitProperties.class, (roundEnv, metadataEnv) -> {

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-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -41,6 +41,7 @@
4141
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
4242
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
4343
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
44+
import org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties;
4445
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
4546
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesGrandparent;
4647
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesParent;
@@ -104,6 +105,12 @@ void propertiesWithLombokDataClass() throws IOException {
104105
(stream) -> assertThat(stream).containsExactly("name", "description", "counter", "number", "items")));
105106
}
106107

108+
@Test
109+
void propertiesWithLombokValueClass() throws IOException {
110+
process(LombokSimpleValueProperties.class, propertyNames(
111+
(stream) -> assertThat(stream).containsExactly("name", "description", "counter", "number", "items")));
112+
}
113+
107114
@Test
108115
void propertiesWithConstructorWithConstructorBinding() throws IOException {
109116
process(ImmutableSimpleProperties.class, propertyNames(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2021 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.lombok;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import lombok.Value;
23+
24+
import org.springframework.boot.configurationsample.ConfigurationProperties;
25+
26+
/**
27+
* Configuration properties using lombok @Value.
28+
*
29+
* @author Mark Jeffrey
30+
*/
31+
@Value
32+
@ConfigurationProperties(prefix = "value")
33+
@SuppressWarnings("unused")
34+
public class LombokSimpleValueProperties {
35+
36+
private final String id = "super-id";
37+
38+
/**
39+
* Name description.
40+
*/
41+
private String name;
42+
43+
private String description;
44+
45+
private Integer counter;
46+
47+
@Deprecated
48+
private Integer number = 0;
49+
50+
private final List<String> items = new ArrayList<>();
51+
52+
private final String ignored = "foo";
53+
54+
}

0 commit comments

Comments
 (0)