Skip to content

Commit 743f4a4

Browse files
ChunMengLusnicoll
authored andcommitted
Detect config props using builder pattern and generics
See gh-19099
1 parent 4fb0a2b commit 743f4a4

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
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;
3132
import javax.lang.model.type.TypeKind;
3233
import javax.lang.model.type.TypeMirror;
34+
import javax.lang.model.type.TypeVariable;
3335
import javax.lang.model.util.ElementFilter;
3436

3537
/**
@@ -116,8 +118,26 @@ private boolean isSetter(ExecutableElement method) {
116118

117119
private boolean isSetterReturnType(ExecutableElement method) {
118120
TypeMirror returnType = method.getReturnType();
119-
return (TypeKind.VOID == returnType.getKind()
120-
|| this.env.getTypeUtils().isSameType(method.getEnclosingElement().asType(), returnType));
121+
// void
122+
if (TypeKind.VOID == returnType.getKind()) {
123+
return true;
124+
}
125+
126+
TypeMirror classType = method.getEnclosingElement().asType();
127+
TypeUtils typeUtils = this.env.getTypeUtils();
128+
// Chain
129+
if (typeUtils.isSameType(classType, returnType)) {
130+
return true;
131+
}
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+
});
121141
}
122142

123143
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +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;
2426
import org.springframework.boot.configurationsample.generic.ComplexGenericProperties;
2527
import org.springframework.boot.configurationsample.generic.GenericConfig;
2628
import org.springframework.boot.configurationsample.generic.SimpleGenericProperties;
@@ -110,4 +112,15 @@ void wildcardTypes() {
110112
assertThat(metadata.getItems()).hasSize(3);
111113
}
112114

115+
@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));
123+
assertThat(metadata.getItems()).hasSize(3);
124+
}
125+
113126
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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.generic;
18+
19+
/**
20+
* Chain Generic
21+
*
22+
* @param <T> name type
23+
* @author L.cm
24+
*/
25+
public class ChainGenericConfig<T extends ChainGenericConfig> {
26+
27+
/**
28+
* Generic config pingTimeout.
29+
*/
30+
private Integer pingTimeout = 1000;
31+
32+
public int getPingTimeout() {
33+
return this.pingTimeout;
34+
}
35+
36+
public T setPingTimeout(int pingTimeout) {
37+
this.pingTimeout = pingTimeout;
38+
return (T) this;
39+
}
40+
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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.generic;
18+
19+
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
21+
22+
/**
23+
* Chain Generic Properties
24+
*
25+
* @author L.cm
26+
*/
27+
@ConfigurationProperties("generic")
28+
public class ChainGenericProperties {
29+
30+
/**
31+
* Generic config.
32+
*/
33+
@NestedConfigurationProperty
34+
private ChainGenericConfig config;
35+
36+
public ChainGenericConfig getConfig() {
37+
return this.config;
38+
}
39+
40+
public void setConfig(ChainGenericConfig config) {
41+
this.config = config;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)