Skip to content

Commit b364396

Browse files
committed
Polish "Detect config props using builder pattern and generics"
See gh-19099
1 parent 743f4a4 commit b364396

File tree

4 files changed

+40
-54
lines changed

4 files changed

+40
-54
lines changed

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@
2828
import javax.lang.model.element.Modifier;
2929
import javax.lang.model.element.TypeElement;
3030
import javax.lang.model.element.VariableElement;
31-
import javax.lang.model.type.DeclaredType;
3231
import javax.lang.model.type.TypeKind;
3332
import javax.lang.model.type.TypeMirror;
34-
import javax.lang.model.type.TypeVariable;
3533
import javax.lang.model.util.ElementFilter;
3634

3735
/**
@@ -46,15 +44,18 @@ class TypeElementMembers {
4644

4745
private final MetadataGenerationEnvironment env;
4846

47+
private final TypeElement targetType;
48+
4949
private final Map<String, VariableElement> fields = new LinkedHashMap<>();
5050

5151
private final Map<String, ExecutableElement> publicGetters = new LinkedHashMap<>();
5252

5353
private final Map<String, List<ExecutableElement>> publicSetters = new LinkedHashMap<>();
5454

55-
TypeElementMembers(MetadataGenerationEnvironment env, TypeElement element) {
55+
TypeElementMembers(MetadataGenerationEnvironment env, TypeElement targetType) {
5656
this.env = env;
57-
process(element);
57+
this.targetType = targetType;
58+
process(targetType);
5859
}
5960

6061
private void process(TypeElement element) {
@@ -118,26 +119,19 @@ private boolean isSetter(ExecutableElement method) {
118119

119120
private boolean isSetterReturnType(ExecutableElement method) {
120121
TypeMirror returnType = method.getReturnType();
121-
// void
122122
if (TypeKind.VOID == returnType.getKind()) {
123123
return true;
124124
}
125-
126-
TypeMirror classType = method.getEnclosingElement().asType();
127-
TypeUtils typeUtils = this.env.getTypeUtils();
128-
// Chain
129-
if (typeUtils.isSameType(classType, returnType)) {
125+
if (TypeKind.DECLARED == returnType.getKind()
126+
&& this.env.getTypeUtils().isSameType(method.getEnclosingElement().asType(), returnType)) {
130127
return true;
131128
}
132-
133-
// Chain generic type, <T extends classType>
134-
List<? extends TypeMirror> genericTypes = ((DeclaredType) classType).getTypeArguments();
135-
return genericTypes.stream().anyMatch((genericType) -> {
136-
TypeMirror upperBound = ((TypeVariable) genericType).getUpperBound();
137-
String classTypeName = typeUtils.getQualifiedName(((DeclaredType) classType).asElement());
138-
String genericTypeName = typeUtils.getQualifiedName(((DeclaredType) upperBound).asElement());
139-
return classTypeName.equals(genericTypeName);
140-
});
129+
if (TypeKind.TYPEVAR == returnType.getKind()) {
130+
String resolvedType = this.env.getTypeUtils().getType(this.targetType, returnType);
131+
return (resolvedType != null
132+
&& resolvedType.equals(this.env.getTypeUtils().getQualifiedName(this.targetType)));
133+
}
134+
return false;
141135
}
142136

143137
private String getAccessorName(String methodName) {

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
2222
import org.springframework.boot.configurationprocessor.metadata.Metadata;
2323
import org.springframework.boot.configurationsample.generic.AbstractGenericProperties;
24-
import org.springframework.boot.configurationsample.generic.ChainGenericConfig;
25-
import org.springframework.boot.configurationsample.generic.ChainGenericProperties;
2624
import org.springframework.boot.configurationsample.generic.ComplexGenericProperties;
25+
import org.springframework.boot.configurationsample.generic.ConcreteBuilderProperties;
2726
import org.springframework.boot.configurationsample.generic.GenericConfig;
2827
import org.springframework.boot.configurationsample.generic.SimpleGenericProperties;
2928
import org.springframework.boot.configurationsample.generic.UnresolvedGenericProperties;
@@ -113,13 +112,13 @@ void wildcardTypes() {
113112
}
114113

115114
@Test
116-
void chainGenericProperties() {
117-
ConfigurationMetadata metadata = compile(ChainGenericProperties.class);
118-
assertThat(metadata).has(Metadata.withGroup("generic").fromSource(ChainGenericProperties.class));
119-
assertThat(metadata).has(Metadata.withGroup("generic.config", ChainGenericConfig.class)
120-
.fromSource(ChainGenericProperties.class));
121-
assertThat(metadata).has(Metadata.withProperty("generic.config.ping-timeout", Integer.class)
122-
.fromSource(ChainGenericConfig.class).withDefaultValue(null));
115+
void builderPatternWithGenericReturnType() {
116+
ConfigurationMetadata metadata = compile(ConcreteBuilderProperties.class);
117+
assertThat(metadata).has(Metadata.withGroup("builder").fromSource(ConcreteBuilderProperties.class));
118+
assertThat(metadata).has(
119+
Metadata.withProperty("builder.number", Integer.class).fromSource(ConcreteBuilderProperties.class));
120+
assertThat(metadata).has(
121+
Metadata.withProperty("builder.description", String.class).fromSource(ConcreteBuilderProperties.class));
123122
assertThat(metadata.getItems()).hasSize(3);
124123
}
125124

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,24 @@
1717
package org.springframework.boot.configurationsample.generic;
1818

1919
import org.springframework.boot.configurationsample.ConfigurationProperties;
20-
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
2120

2221
/**
23-
* Chain Generic Properties
22+
* Builder pattern with a resolved generic
2423
*
25-
* @author L.cm
24+
* @author Stephane Nicoll
2625
*/
27-
@ConfigurationProperties("generic")
28-
public class ChainGenericProperties {
26+
@ConfigurationProperties("builder")
27+
public class ConcreteBuilderProperties extends GenericBuilderProperties<ConcreteBuilderProperties> {
2928

30-
/**
31-
* Generic config.
32-
*/
33-
@NestedConfigurationProperty
34-
private ChainGenericConfig config;
29+
private String description;
3530

36-
public ChainGenericConfig getConfig() {
37-
return this.config;
31+
public String getDescription() {
32+
return this.description;
3833
}
3934

40-
public void setConfig(ChainGenericConfig config) {
41-
this.config = config;
35+
public ConcreteBuilderProperties setDescription(String description) {
36+
this.description = description;
37+
return this;
4238
}
4339

4440
}
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,21 @@
1717
package org.springframework.boot.configurationsample.generic;
1818

1919
/**
20-
* Chain Generic
20+
* A configuration properties that uses the builder pattern with a generic.
2121
*
22-
* @param <T> name type
23-
* @author L.cm
22+
* @param <T> the type of the return type
23+
* @author Stephane Nicoll
2424
*/
25-
public class ChainGenericConfig<T extends ChainGenericConfig> {
25+
public class GenericBuilderProperties<T extends GenericBuilderProperties<T>> {
2626

27-
/**
28-
* Generic config pingTimeout.
29-
*/
30-
private Integer pingTimeout = 1000;
27+
private int number;
3128

32-
public int getPingTimeout() {
33-
return this.pingTimeout;
29+
public int getNumber() {
30+
return this.number;
3431
}
3532

36-
public T setPingTimeout(int pingTimeout) {
37-
this.pingTimeout = pingTimeout;
33+
public T setNumber(int number) {
34+
this.number = number;
3835
return (T) this;
3936
}
4037

0 commit comments

Comments
 (0)