diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/configuration-metadata/annotation-processor.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/configuration-metadata/annotation-processor.adoc index 5f5e521523..5219c77c14 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/configuration-metadata/annotation-processor.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/configuration-metadata/annotation-processor.adoc @@ -65,7 +65,7 @@ The processor picks up both classes and methods that are annotated with `@Config If the class is also annotated with `@ConstructorBinding`, a single constructor is expected and one property is created per constructor parameter. 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). -The annotation processor also supports the use of the `@Data`, `@Getter`, and `@Setter` lombok annotations. +The annotation processor also supports the use of the `@Data`, `@Value`, `@Getter`, and `@Setter` lombok annotations. Consider the following example: diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptor.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptor.java index 3086400e77..81a6d6b5a4 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptor.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,8 @@ class LombokPropertyDescriptor extends PropertyDescriptor { private static final String LOMBOK_DATA_ANNOTATION = "lombok.Data"; + private static final String LOMBOK_VALUE_ANNOTATION = "lombok.Value"; + private static final String LOMBOK_GETTER_ANNOTATION = "lombok.Getter"; private static final String LOMBOK_SETTER_ANNOTATION = "lombok.Setter"; @@ -100,7 +102,8 @@ class LombokPropertyDescriptor extends PropertyDescriptor { if (lombokMethodAnnotationOnElement != null) { return isAccessLevelPublic(env, lombokMethodAnnotationOnElement); } - return (env.getAnnotation(getOwnerElement(), LOMBOK_DATA_ANNOTATION) != null); + return (env.hasAnnotation(getOwnerElement(), LOMBOK_DATA_ANNOTATION) + || env.hasAnnotation(getOwnerElement(), LOMBOK_VALUE_ANNOTATION)); } private boolean isAccessLevelPublic(MetadataGenerationEnvironment env, AnnotationMirror lombokAnnotation) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokMetadataGenerationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokMetadataGenerationTests.java index 943fcd1c43..6e63bb1b5a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokMetadataGenerationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokMetadataGenerationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import org.springframework.boot.configurationsample.lombok.LombokInnerClassPrope import org.springframework.boot.configurationsample.lombok.LombokInnerClassWithGetterProperties; import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties; import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties; +import org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties; import org.springframework.boot.configurationsample.lombok.SimpleLombokPojo; import static org.assertj.core.api.Assertions.assertThat; @@ -46,6 +47,12 @@ class LombokMetadataGenerationTests extends AbstractMetadataGenerationTests { assertSimpleLombokProperties(metadata, LombokSimpleDataProperties.class, "data"); } + @Test + void lombokValueProperties() { + ConfigurationMetadata metadata = compile(LombokSimpleValueProperties.class); + assertSimpleLombokProperties(metadata, LombokSimpleValueProperties.class, "value"); + } + @Test void lombokSimpleProperties() { ConfigurationMetadata metadata = compile(LombokSimpleProperties.class); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptorTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptorTests.java index 21bddfaaaf..66d02ce82f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptorTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/LombokPropertyDescriptorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import org.springframework.boot.configurationsample.lombok.LombokExplicitPropert import org.springframework.boot.configurationsample.lombok.LombokInnerClassProperties; import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties; import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties; +import org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties; import org.springframework.boot.configurationsample.simple.SimpleProperties; import org.springframework.boot.configurationsample.specific.InnerClassProperties; @@ -114,6 +115,16 @@ class LombokPropertyDescriptorTests extends PropertyDescriptorTests { }); } + @Test + void lombokSimplePropertyWithOnlyGetterOnValueClassShouldNotBeExposed() throws IOException { + process(LombokSimpleValueProperties.class, (roundEnv, metadataEnv) -> { + TypeElement ownerElement = roundEnv.getRootElement(LombokSimpleValueProperties.class); + LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement, "ignored"); + assertThat(property.isProperty(metadataEnv)).isFalse(); + assertThat(property.isNested(metadataEnv)).isFalse(); + }); + } + @Test void lombokSimplePropertyWithOnlyGetterOnFieldShouldNotBeExposed() throws IOException { process(LombokExplicitProperties.class, (roundEnv, metadataEnv) -> { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolverTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolverTests.java index 6e3bb6312c..69b4f051ff 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolverTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,6 +41,7 @@ import org.springframework.boot.configurationsample.immutable.ImmutableSimplePro import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties; import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties; import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties; +import org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties; import org.springframework.boot.configurationsample.simple.HierarchicalProperties; import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesGrandparent; import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesParent; @@ -104,6 +105,12 @@ class PropertyDescriptorResolverTests { (stream) -> assertThat(stream).containsExactly("name", "description", "counter", "number", "items"))); } + @Test + void propertiesWithLombokValueClass() throws IOException { + process(LombokSimpleValueProperties.class, propertyNames( + (stream) -> assertThat(stream).containsExactly("name", "description", "counter", "number", "items"))); + } + @Test void propertiesWithConstructorWithConstructorBinding() throws IOException { process(ImmutableSimpleProperties.class, propertyNames( diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleValueProperties.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleValueProperties.java new file mode 100644 index 0000000000..1907c53a3f --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleValueProperties.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.configurationsample.lombok; + +import java.util.ArrayList; +import java.util.List; + +import lombok.Value; + +import org.springframework.boot.configurationsample.ConfigurationProperties; + +/** + * Configuration properties using lombok @Value. + * + * @author Mark Jeffrey + */ +@Value +@ConfigurationProperties(prefix = "value") +@SuppressWarnings("unused") +public class LombokSimpleValueProperties { + + private final String id = "super-id"; + + /** + * Name description. + */ + private String name; + + private String description; + + private Integer counter; + + @Deprecated + private Integer number = 0; + + private final List items = new ArrayList<>(); + + private final String ignored = "foo"; + +}