Skip to content

Commit 14d8603

Browse files
mjeffreysnicoll
authored andcommitted
Add support for @value annotation
This commit adds support for `@Value` from project Lombok for metadata generation. This is very similar to the existing `@Data` support. See gh-26337
1 parent ffe2d43 commit 14d8603

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,11 @@ 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 (hasAnnotation(env, LOMBOK_DATA_ANNOTATION) || hasAnnotation(env, LOMBOK_VALUE_ANNOTATION));
106+
}
107+
108+
private boolean hasAnnotation(MetadataGenerationEnvironment env, String lombokAnnotation) {
109+
return (env.getAnnotation(getOwnerElement(), lombokAnnotation) != null);
104110
}
105111

106112
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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-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.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)