Merge pull request #26337 from mjeffrey

* pr/26337:
  Polish "Add support for @Value annotation"
  Add support for @Value annotation

Closes gh-26337
pull/26879/head
Stephane Nicoll 3 years ago
commit 7879adf5f2

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

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

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

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

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

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