Skip to content

Commit ffdcdc0

Browse files
committed
Merge branch '2.0.x'
2 parents 69aa491 + 7b7e802 commit ffdcdc0

File tree

7 files changed

+492
-6
lines changed

7 files changed

+492
-6
lines changed

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* @author Stephane Nicoll
6464
* @author Phillip Webb
6565
* @author Kris De Volder
66+
* @author Jonas Keßler
6667
* @since 1.2.0
6768
*/
6869
@SupportedAnnotationTypes({ "*" })
@@ -94,6 +95,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
9495

9596
static final String LOMBOK_SETTER_ANNOTATION = "lombok.Setter";
9697

98+
static final String LOMBOK_ACCESS_LEVEL_PUBLIC = "PUBLIC";
99+
97100
private static final Set<String> SUPPORTED_OPTIONS = Collections.unmodifiableSet(
98101
new HashSet<>(Arrays.asList(ADDITIONAL_METADATA_LOCATIONS_OPTION)));
99102

@@ -366,16 +369,44 @@ private void processNestedLombokTypes(String prefix, TypeElement element,
366369
}
367370

368371
private boolean isLombokField(VariableElement field, TypeElement element) {
369-
return hasAnnotation(field, LOMBOK_GETTER_ANNOTATION)
370-
|| hasAnnotation(element, LOMBOK_GETTER_ANNOTATION)
371-
|| hasAnnotation(element, LOMBOK_DATA_ANNOTATION);
372+
return hasLombokPublicAccessor(field, element, true);
372373
}
373374

374375
private boolean hasLombokSetter(VariableElement field, TypeElement element) {
375376
return !field.getModifiers().contains(Modifier.FINAL)
376-
&& (hasAnnotation(field, LOMBOK_SETTER_ANNOTATION)
377-
|| hasAnnotation(element, LOMBOK_SETTER_ANNOTATION)
378-
|| hasAnnotation(element, LOMBOK_DATA_ANNOTATION));
377+
&& hasLombokPublicAccessor(field, element, false);
378+
}
379+
380+
/**
381+
* Determine if the specified {@link VariableElement field} defines a public accessor
382+
* using lombok annotations.
383+
* @param field the field to inspect
384+
* @param element the parent element of the field (i.e. its holding class)
385+
* @param getter {@code true} to look for the read accessor, {@code false} for the
386+
* write accessor
387+
* @return {@code true} if this field is a public accessor of the specified type
388+
*/
389+
private boolean hasLombokPublicAccessor(VariableElement field, TypeElement element,
390+
boolean getter) {
391+
String annotation = (getter ? LOMBOK_GETTER_ANNOTATION
392+
: LOMBOK_SETTER_ANNOTATION);
393+
AnnotationMirror lombokMethodAnnotationOnField = getAnnotation(field, annotation);
394+
if (lombokMethodAnnotationOnField != null) {
395+
return isAccessLevelPublic(lombokMethodAnnotationOnField);
396+
}
397+
AnnotationMirror lombokMethodAnnotationOnElement = getAnnotation(element,
398+
annotation);
399+
if (lombokMethodAnnotationOnElement != null) {
400+
return isAccessLevelPublic(lombokMethodAnnotationOnElement);
401+
}
402+
return hasAnnotation(element, LOMBOK_DATA_ANNOTATION);
403+
}
404+
405+
406+
private boolean isAccessLevelPublic(AnnotationMirror lombokAnnotation) {
407+
Map<String, Object> values = getAnnotationElementValues(lombokAnnotation);
408+
Object value = values.get("value");
409+
return (value == null || value.toString().equals(LOMBOK_ACCESS_LEVEL_PUBLIC));
379410
}
380411

381412
private void processNestedType(String prefix, TypeElement element,

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
import org.springframework.boot.configurationsample.incremental.BarProperties;
4848
import org.springframework.boot.configurationsample.incremental.FooProperties;
4949
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
50+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelOverwriteDataProperties;
51+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelOverwriteDefaultProperties;
52+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelOverwriteExplicitProperties;
53+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelProperties;
5054
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
5155
import org.springframework.boot.configurationsample.lombok.LombokInnerClassProperties;
5256
import org.springframework.boot.configurationsample.lombok.LombokInnerClassWithGetterProperties;
@@ -96,6 +100,7 @@
96100
* @author Phillip Webb
97101
* @author Andy Wilkinson
98102
* @author Kris De Volder
103+
* @author Jonas Keßler
99104
*/
100105
public class ConfigurationMetadataAnnotationProcessorTests {
101106

@@ -542,6 +547,41 @@ public void lombokExplicitProperties() {
542547
ConfigurationMetadata metadata = compile(LombokExplicitProperties.class);
543548
assertSimpleLombokProperties(metadata, LombokExplicitProperties.class,
544549
"explicit");
550+
assertThat(metadata.getItems()).hasSize(6);
551+
}
552+
553+
@Test
554+
public void lombokAccessLevelProperties() {
555+
ConfigurationMetadata metadata = compile(LombokAccessLevelProperties.class);
556+
assertAccessLevelLombokProperties(metadata, LombokAccessLevelProperties.class,
557+
"accesslevel", 2);
558+
}
559+
560+
@Test
561+
public void lombokAccessLevelOverwriteDataProperties() {
562+
ConfigurationMetadata metadata = compile(
563+
LombokAccessLevelOverwriteDataProperties.class);
564+
assertAccessLevelOverwriteLombokProperties(metadata,
565+
LombokAccessLevelOverwriteDataProperties.class,
566+
"accesslevel.overwrite.data");
567+
}
568+
569+
@Test
570+
public void lombokAccessLevelOverwriteExplicitProperties() {
571+
ConfigurationMetadata metadata = compile(
572+
LombokAccessLevelOverwriteExplicitProperties.class);
573+
assertAccessLevelOverwriteLombokProperties(metadata,
574+
LombokAccessLevelOverwriteExplicitProperties.class,
575+
"accesslevel.overwrite.explicit");
576+
}
577+
578+
@Test
579+
public void lombokAccessLevelOverwriteDefaultProperties() {
580+
ConfigurationMetadata metadata = compile(
581+
LombokAccessLevelOverwriteDefaultProperties.class);
582+
assertAccessLevelOverwriteLombokProperties(metadata,
583+
LombokAccessLevelOverwriteDefaultProperties.class,
584+
"accesslevel.overwrite.default");
545585
}
546586

547587
@Test
@@ -1000,6 +1040,21 @@ private void assertSimpleLombokProperties(ConfigurationMetadata metadata,
10001040
assertThat(metadata).doesNotHave(Metadata.withProperty(prefix + ".ignored"));
10011041
}
10021042

1043+
private void assertAccessLevelOverwriteLombokProperties(
1044+
ConfigurationMetadata metadata, Class<?> source, String prefix) {
1045+
assertAccessLevelLombokProperties(metadata, source, prefix, 7);
1046+
}
1047+
1048+
private void assertAccessLevelLombokProperties(ConfigurationMetadata metadata,
1049+
Class<?> source, String prefix, int countNameFields) {
1050+
assertThat(metadata).has(Metadata.withGroup(prefix).fromSource(source));
1051+
for (int i = 0; i < countNameFields; i++) {
1052+
assertThat(metadata)
1053+
.has(Metadata.withProperty(prefix + ".name" + i, String.class));
1054+
}
1055+
assertThat(metadata.getItems()).hasSize(1 + countNameFields);
1056+
}
1057+
10031058
private ConfigurationMetadata compile(Class<?>... types) {
10041059
TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor(
10051060
this.compiler.getOutputLocation());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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 lombok.AccessLevel;
20+
import lombok.Data;
21+
import lombok.Getter;
22+
import lombok.Setter;
23+
24+
import org.springframework.boot.configurationsample.ConfigurationProperties;
25+
26+
/**
27+
* Configuration properties using lombok @Data on element level and overwriting behaviour
28+
* with @Getter und @Setter at field level.
29+
*
30+
* @author Jonas Keßler
31+
*/
32+
@Data
33+
@ConfigurationProperties(prefix = "accesslevel.overwrite.data")
34+
public class LombokAccessLevelOverwriteDataProperties {
35+
36+
private String name0;
37+
38+
@Getter(AccessLevel.PUBLIC)
39+
@Setter(AccessLevel.PUBLIC)
40+
private String name1;
41+
42+
@Getter(AccessLevel.PUBLIC)
43+
private String name2;
44+
45+
@Setter(AccessLevel.PUBLIC)
46+
private String name3;
47+
48+
@Getter
49+
@Setter
50+
private String name4;
51+
52+
@Getter
53+
private String name5;
54+
55+
@Setter
56+
private String name6;
57+
58+
/*
59+
* AccessLevel.NONE
60+
*/
61+
@Getter(AccessLevel.NONE)
62+
@Setter(AccessLevel.NONE)
63+
private String ignoredAccessLevelNone;
64+
65+
@Getter(AccessLevel.NONE)
66+
private String ignoredGetterAccessLevelNone;
67+
68+
@Setter(AccessLevel.NONE)
69+
private String ignoredSetterAccessLevelNone;
70+
71+
/*
72+
* AccessLevel.PRIVATE
73+
*/
74+
@Getter(AccessLevel.PRIVATE)
75+
@Setter(AccessLevel.PRIVATE)
76+
private String ignoredAccessLevelPrivate;
77+
78+
@Getter(AccessLevel.PRIVATE)
79+
private String ignoredGetterAccessLevelPrivate;
80+
81+
@Setter(AccessLevel.PRIVATE)
82+
private String ignoredSetterAccessLevelPrivate;
83+
84+
/*
85+
* AccessLevel.PACKAGE
86+
*/
87+
@Getter(AccessLevel.PACKAGE)
88+
@Setter(AccessLevel.PACKAGE)
89+
private String ignoredAccessLevelPackage;
90+
91+
@Getter(AccessLevel.PACKAGE)
92+
private String ignoredGetterAccessLevelPackage;
93+
94+
@Setter(AccessLevel.PACKAGE)
95+
private String ignoredSetterAccessLevelPackage;
96+
97+
/*
98+
* AccessLevel.PROTECTED
99+
*/
100+
@Getter(AccessLevel.PROTECTED)
101+
@Setter(AccessLevel.PROTECTED)
102+
private String ignoredAccessLevelProtected;
103+
104+
@Getter(AccessLevel.PROTECTED)
105+
private String ignoredGetterAccessLevelProtected;
106+
107+
@Setter(AccessLevel.PROTECTED)
108+
private String ignoredSetterAccessLevelProtected;
109+
110+
/*
111+
* AccessLevel.MODULE
112+
*/
113+
@Getter(AccessLevel.MODULE)
114+
@Setter(AccessLevel.MODULE)
115+
private String ignoredAccessLevelModule;
116+
117+
@Getter(AccessLevel.MODULE)
118+
private String ignoredGetterAccessLevelModule;
119+
120+
@Setter(AccessLevel.MODULE)
121+
private String ignoredSetterAccessLevelModule;
122+
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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 lombok.AccessLevel;
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
23+
import org.springframework.boot.configurationsample.ConfigurationProperties;
24+
25+
/**
26+
* Configuration properties using lombok @Getter and @Setter without explicitly defining
27+
* AccessLevel on element level and overwriting behaviour at field level.
28+
*
29+
* @author Jonas Keßler
30+
*/
31+
@Getter
32+
@Setter
33+
@ConfigurationProperties(prefix = "accesslevel.overwrite.default")
34+
public class LombokAccessLevelOverwriteDefaultProperties {
35+
36+
private String name0;
37+
38+
@Getter(AccessLevel.PUBLIC)
39+
@Setter(AccessLevel.PUBLIC)
40+
private String name1;
41+
42+
@Getter(AccessLevel.PUBLIC)
43+
private String name2;
44+
45+
@Setter(AccessLevel.PUBLIC)
46+
private String name3;
47+
48+
@Getter
49+
@Setter
50+
private String name4;
51+
52+
@Getter
53+
private String name5;
54+
55+
@Setter
56+
private String name6;
57+
58+
/*
59+
* AccessLevel.NONE
60+
*/
61+
@Getter(AccessLevel.NONE)
62+
@Setter(AccessLevel.NONE)
63+
private String ignoredAccessLevelNone;
64+
65+
/*
66+
* AccessLevel.PRIVATE
67+
*/
68+
@Getter(AccessLevel.PRIVATE)
69+
@Setter(AccessLevel.PRIVATE)
70+
private String ignoredAccessLevelPrivate;
71+
72+
/*
73+
* AccessLevel.PACKAGE
74+
*/
75+
@Getter(AccessLevel.PACKAGE)
76+
@Setter(AccessLevel.PACKAGE)
77+
private String ignoredAccessLevelPackage;
78+
79+
/*
80+
* AccessLevel.PROTECTED
81+
*/
82+
@Getter(AccessLevel.PROTECTED)
83+
@Setter(AccessLevel.PROTECTED)
84+
private String ignoredAccessLevelProtected;
85+
86+
/*
87+
* AccessLevel.MODULE
88+
*/
89+
@Getter(AccessLevel.MODULE)
90+
@Setter(AccessLevel.MODULE)
91+
private String ignoredAccessLevelModule;
92+
93+
}

0 commit comments

Comments
 (0)