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
This commit is contained in:
Mark Jeffrey 2021-05-02 14:30:31 +02:00 committed by Stephane Nicoll
parent ffe2d43653
commit 14d86034a1
5 changed files with 86 additions and 1 deletions

View File

@ -36,6 +36,8 @@ class LombokPropertyDescriptor extends PropertyDescriptor<VariableElement> {
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<VariableElement> {
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) {

View File

@ -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);

View File

@ -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) -> {

View File

@ -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(

View File

@ -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<String> items = new ArrayList<>();
private final String ignored = "foo";
}