From 14d86034a162523b2cc8a65f06e0bdc8672bdd95 Mon Sep 17 00:00:00 2001 From: Mark Jeffrey Date: Sun, 2 May 2021 14:30:31 +0200 Subject: [PATCH 1/2] 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 --- .../LombokPropertyDescriptor.java | 8 ++- .../LombokMetadataGenerationTests.java | 7 +++ .../LombokPropertyDescriptorTests.java | 11 ++++ .../PropertyDescriptorResolverTests.java | 7 +++ .../lombok/LombokSimpleValueProperties.java | 54 +++++++++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleValueProperties.java 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 3086400e776..c2c59a549c1 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 @@ -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,11 @@ class LombokPropertyDescriptor extends PropertyDescriptor { if (lombokMethodAnnotationOnElement != null) { return isAccessLevelPublic(env, lombokMethodAnnotationOnElement); } - return (env.getAnnotation(getOwnerElement(), LOMBOK_DATA_ANNOTATION) != null); + return (hasAnnotation(env, LOMBOK_DATA_ANNOTATION) || hasAnnotation(env, LOMBOK_VALUE_ANNOTATION)); + } + + private boolean hasAnnotation(MetadataGenerationEnvironment env, String lombokAnnotation) { + return (env.getAnnotation(getOwnerElement(), lombokAnnotation) != null); } 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 943fcd1c433..af75dedbc96 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 @@ -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 21bddfaaaf2..2512010f775 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 @@ -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 6e3bb6312c6..93dc9504c18 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 @@ -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 00000000000..6f6a77987b5 --- /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-2019 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"; + +} From 69c2621a1438d77ede2e95f1441d40dd557318da Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 28 May 2021 16:02:29 +0200 Subject: [PATCH 2/2] Polish "Add support for @Value annotation" See gh-26337 --- .../configuration-metadata/annotation-processor.adoc | 2 +- .../configurationprocessor/LombokPropertyDescriptor.java | 9 +++------ .../LombokMetadataGenerationTests.java | 2 +- .../LombokPropertyDescriptorTests.java | 2 +- .../PropertyDescriptorResolverTests.java | 2 +- .../lombok/LombokSimpleValueProperties.java | 2 +- 6 files changed, 8 insertions(+), 11 deletions(-) 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 5f5e521523b..5219c77c14a 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 c2c59a549c1..81a6d6b5a46 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. @@ -102,11 +102,8 @@ class LombokPropertyDescriptor extends PropertyDescriptor { if (lombokMethodAnnotationOnElement != null) { return isAccessLevelPublic(env, lombokMethodAnnotationOnElement); } - return (hasAnnotation(env, LOMBOK_DATA_ANNOTATION) || hasAnnotation(env, LOMBOK_VALUE_ANNOTATION)); - } - - private boolean hasAnnotation(MetadataGenerationEnvironment env, String lombokAnnotation) { - return (env.getAnnotation(getOwnerElement(), lombokAnnotation) != 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 af75dedbc96..6e63bb1b5a9 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. 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 2512010f775..66d02ce82fb 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. 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 93dc9504c18..69b4f051ffa 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. 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 index 6f6a77987b5..1907c53a3f4 100644 --- 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 @@ -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.