diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java index 75be606b3b..4f683d5fba 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java @@ -44,15 +44,18 @@ class TypeElementMembers { private final MetadataGenerationEnvironment env; + private final TypeElement targetType; + private final Map fields = new LinkedHashMap<>(); private final Map publicGetters = new LinkedHashMap<>(); private final Map> publicSetters = new LinkedHashMap<>(); - TypeElementMembers(MetadataGenerationEnvironment env, TypeElement element) { + TypeElementMembers(MetadataGenerationEnvironment env, TypeElement targetType) { this.env = env; - process(element); + this.targetType = targetType; + process(targetType); } private void process(TypeElement element) { @@ -116,8 +119,19 @@ class TypeElementMembers { private boolean isSetterReturnType(ExecutableElement method) { TypeMirror returnType = method.getReturnType(); - return (TypeKind.VOID == returnType.getKind() - || this.env.getTypeUtils().isSameType(method.getEnclosingElement().asType(), returnType)); + if (TypeKind.VOID == returnType.getKind()) { + return true; + } + if (TypeKind.DECLARED == returnType.getKind() + && this.env.getTypeUtils().isSameType(method.getEnclosingElement().asType(), returnType)) { + return true; + } + if (TypeKind.TYPEVAR == returnType.getKind()) { + String resolvedType = this.env.getTypeUtils().getType(this.targetType, returnType); + return (resolvedType != null + && resolvedType.equals(this.env.getTypeUtils().getQualifiedName(this.targetType))); + } + return false; } private String getAccessorName(String methodName) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/GenericsMetadataGenerationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/GenericsMetadataGenerationTests.java index 0643c22cc0..575ea90cc1 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/GenericsMetadataGenerationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/GenericsMetadataGenerationTests.java @@ -22,6 +22,7 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet import org.springframework.boot.configurationprocessor.metadata.Metadata; import org.springframework.boot.configurationsample.generic.AbstractGenericProperties; import org.springframework.boot.configurationsample.generic.ComplexGenericProperties; +import org.springframework.boot.configurationsample.generic.ConcreteBuilderProperties; import org.springframework.boot.configurationsample.generic.GenericConfig; import org.springframework.boot.configurationsample.generic.SimpleGenericProperties; import org.springframework.boot.configurationsample.generic.UnresolvedGenericProperties; @@ -110,4 +111,15 @@ class GenericsMetadataGenerationTests extends AbstractMetadataGenerationTests { assertThat(metadata.getItems()).hasSize(3); } + @Test + void builderPatternWithGenericReturnType() { + ConfigurationMetadata metadata = compile(ConcreteBuilderProperties.class); + assertThat(metadata).has(Metadata.withGroup("builder").fromSource(ConcreteBuilderProperties.class)); + assertThat(metadata).has( + Metadata.withProperty("builder.number", Integer.class).fromSource(ConcreteBuilderProperties.class)); + assertThat(metadata).has( + Metadata.withProperty("builder.description", String.class).fromSource(ConcreteBuilderProperties.class)); + assertThat(metadata.getItems()).hasSize(3); + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/generic/ConcreteBuilderProperties.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/generic/ConcreteBuilderProperties.java new file mode 100644 index 0000000000..94e78ee0b1 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/generic/ConcreteBuilderProperties.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2019 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.generic; + +import org.springframework.boot.configurationsample.ConfigurationProperties; + +/** + * Builder pattern with a resolved generic + * + * @author Stephane Nicoll + */ +@ConfigurationProperties("builder") +public class ConcreteBuilderProperties extends GenericBuilderProperties { + + private String description; + + public String getDescription() { + return this.description; + } + + public ConcreteBuilderProperties setDescription(String description) { + this.description = description; + return this; + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/generic/GenericBuilderProperties.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/generic/GenericBuilderProperties.java new file mode 100644 index 0000000000..bb8db7e3c5 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/generic/GenericBuilderProperties.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2019 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.generic; + +/** + * A configuration properties that uses the builder pattern with a generic. + * + * @param the type of the return type + * @author Stephane Nicoll + */ +public class GenericBuilderProperties> { + + private int number; + + public int getNumber() { + return this.number; + } + + public T setNumber(int number) { + this.number = number; + return (T) this; + } + +}