Skip to content

Commit 38051d9

Browse files
committed
Merge pull request #13175 from kesslerj
* pr/13175: Polish "Respect lombok.AccessLevel attributes" Respect lombok.AccessLevel attributes
2 parents 0c55c54 + 563d7d7 commit 38051d9

File tree

7 files changed

+493
-7
lines changed

7 files changed

+493
-7
lines changed

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
@@ -56,6 +56,7 @@
5656
* @author Stephane Nicoll
5757
* @author Phillip Webb
5858
* @author Kris De Volder
59+
* @author Jonas Keßler
5960
* @since 1.2.0
6061
*/
6162
@SupportedAnnotationTypes({ "*" })
@@ -76,6 +77,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
7677

7778
static final String LOMBOK_SETTER_ANNOTATION = "lombok.Setter";
7879

80+
static final String LOMBOK_ACCESS_LEVEL_PUBLIC = "PUBLIC";
81+
7982
private MetadataStore metadataStore;
8083

8184
private MetadataCollector metadataCollector;
@@ -302,16 +305,44 @@ private void processNestedLombokTypes(String prefix, TypeElement element,
302305
}
303306

304307
private boolean isLombokField(VariableElement field, TypeElement element) {
305-
return hasAnnotation(field, LOMBOK_GETTER_ANNOTATION)
306-
|| hasAnnotation(element, LOMBOK_GETTER_ANNOTATION)
307-
|| hasAnnotation(element, LOMBOK_DATA_ANNOTATION);
308+
return hasLombokPublicAccessor(field, element, true);
308309
}
309310

310311
private boolean hasLombokSetter(VariableElement field, TypeElement element) {
311312
return !field.getModifiers().contains(Modifier.FINAL)
312-
&& (hasAnnotation(field, LOMBOK_SETTER_ANNOTATION)
313-
|| hasAnnotation(element, LOMBOK_SETTER_ANNOTATION)
314-
|| hasAnnotation(element, LOMBOK_DATA_ANNOTATION));
313+
&& hasLombokPublicAccessor(field, element, false);
314+
}
315+
316+
/**
317+
* Determine if the specified {@link VariableElement field} defines a public accessor
318+
* using lombok annotations.
319+
* @param field the field to inspect
320+
* @param element the parent element of the field (i.e. its holding class)
321+
* @param getter {@code true} to look for the read accessor, {@code false} for the
322+
* write accessor
323+
* @return {@code true} if this field is a public accessor of the specified type
324+
*/
325+
private boolean hasLombokPublicAccessor(VariableElement field, TypeElement element,
326+
boolean getter) {
327+
String annotation = (getter ? LOMBOK_GETTER_ANNOTATION
328+
: LOMBOK_SETTER_ANNOTATION);
329+
AnnotationMirror lombokMethodAnnotationOnField = getAnnotation(field, annotation);
330+
if (lombokMethodAnnotationOnField != null) {
331+
return isAccessLevelPublic(lombokMethodAnnotationOnField);
332+
}
333+
AnnotationMirror lombokMethodAnnotationOnElement = getAnnotation(element,
334+
annotation);
335+
if (lombokMethodAnnotationOnElement != null) {
336+
return isAccessLevelPublic(lombokMethodAnnotationOnElement);
337+
}
338+
return hasAnnotation(element, LOMBOK_DATA_ANNOTATION);
339+
}
340+
341+
342+
private boolean isAccessLevelPublic(AnnotationMirror lombokAnnotation) {
343+
Map<String, Object> values = getAnnotationElementValues(lombokAnnotation);
344+
Object value = values.get("value");
345+
return (value == null || value.toString().equals(LOMBOK_ACCESS_LEVEL_PUBLIC));
315346
}
316347

317348
private void processNestedType(String prefix, TypeElement element,

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
import org.springframework.boot.configurationsample.incremental.BarProperties;
3939
import org.springframework.boot.configurationsample.incremental.FooProperties;
4040
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
41+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelOverwriteDataProperties;
42+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelOverwriteDefaultProperties;
43+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelOverwriteExplicitProperties;
44+
import org.springframework.boot.configurationsample.lombok.LombokAccessLevelProperties;
4145
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
4246
import org.springframework.boot.configurationsample.lombok.LombokInnerClassProperties;
4347
import org.springframework.boot.configurationsample.lombok.LombokInnerClassWithGetterProperties;
@@ -83,6 +87,7 @@
8387
* @author Phillip Webb
8488
* @author Andy Wilkinson
8589
* @author Kris De Volder
90+
* @author Jonas Keßler
8691
*/
8792
public class ConfigurationMetadataAnnotationProcessorTests {
8893

@@ -488,6 +493,41 @@ public void lombokExplicitProperties() throws Exception {
488493
ConfigurationMetadata metadata = compile(LombokExplicitProperties.class);
489494
assertSimpleLombokProperties(metadata, LombokExplicitProperties.class,
490495
"explicit");
496+
assertThat(metadata.getItems()).hasSize(6);
497+
}
498+
499+
@Test
500+
public void lombokAccessLevelProperties() {
501+
ConfigurationMetadata metadata = compile(LombokAccessLevelProperties.class);
502+
assertAccessLevelLombokProperties(metadata, LombokAccessLevelProperties.class,
503+
"accesslevel", 2);
504+
}
505+
506+
@Test
507+
public void lombokAccessLevelOverwriteDataProperties() {
508+
ConfigurationMetadata metadata = compile(
509+
LombokAccessLevelOverwriteDataProperties.class);
510+
assertAccessLevelOverwriteLombokProperties(metadata,
511+
LombokAccessLevelOverwriteDataProperties.class,
512+
"accesslevel.overwrite.data");
513+
}
514+
515+
@Test
516+
public void lombokAccessLevelOverwriteExplicitProperties() {
517+
ConfigurationMetadata metadata = compile(
518+
LombokAccessLevelOverwriteExplicitProperties.class);
519+
assertAccessLevelOverwriteLombokProperties(metadata,
520+
LombokAccessLevelOverwriteExplicitProperties.class,
521+
"accesslevel.overwrite.explicit");
522+
}
523+
524+
@Test
525+
public void lombokAccessLevelOverwriteDefaultProperties() {
526+
ConfigurationMetadata metadata = compile(
527+
LombokAccessLevelOverwriteDefaultProperties.class);
528+
assertAccessLevelOverwriteLombokProperties(metadata,
529+
LombokAccessLevelOverwriteDefaultProperties.class,
530+
"accesslevel.overwrite.default");
491531
}
492532

493533
@Test
@@ -789,7 +829,22 @@ private void assertSimpleLombokProperties(ConfigurationMetadata metadata,
789829
assertThat(metadata).doesNotHave(Metadata.withProperty(prefix + ".ignored"));
790830
}
791831

792-
private ConfigurationMetadata compile(Class<?>... types) throws IOException {
832+
private void assertAccessLevelOverwriteLombokProperties(
833+
ConfigurationMetadata metadata, Class<?> source, String prefix) {
834+
assertAccessLevelLombokProperties(metadata, source, prefix, 7);
835+
}
836+
837+
private void assertAccessLevelLombokProperties(ConfigurationMetadata metadata,
838+
Class<?> source, String prefix, int countNameFields) {
839+
assertThat(metadata).has(Metadata.withGroup(prefix).fromSource(source));
840+
for (int i = 0; i < countNameFields; i++) {
841+
assertThat(metadata)
842+
.has(Metadata.withProperty(prefix + ".name" + i, String.class));
843+
}
844+
assertThat(metadata.getItems()).hasSize(1 + countNameFields);
845+
}
846+
847+
private ConfigurationMetadata compile(Class<?>... types) {
793848
TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor(
794849
this.compiler.getOutputLocation());
795850
this.compiler.getTask(types).call(processor);
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)