diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java index e150a3134b..f410788de0 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java @@ -46,6 +46,7 @@ import javax.lang.model.util.Types; * @author Stephane Nicoll * @author Phillip Webb * @author Pavel Anisimov + * @author Dmytro Nosan */ class TypeUtils { @@ -217,7 +218,7 @@ class TypeUtils { return WRAPPER_TO_PRIMITIVE.get(type.toString()); } - TypeDescriptor resolveTypeDescriptor(TypeElement element) { + private TypeDescriptor resolveTypeDescriptor(TypeElement element) { if (this.typeDescriptors.containsKey(element)) { return this.typeDescriptors.get(element); } @@ -318,22 +319,22 @@ class TypeUtils { } @Override - public String visitTypeVariable(TypeVariable t, TypeDescriptor descriptor) { - TypeMirror typeMirror = descriptor.resolveGeneric(t); - if (typeMirror != null) { - if (typeMirror instanceof TypeVariable typeVariable) { + public String visitTypeVariable(TypeVariable typeVariable, TypeDescriptor descriptor) { + TypeMirror resolvedGeneric = descriptor.resolveGeneric(typeVariable); + if (resolvedGeneric != null) { + if (resolvedGeneric instanceof TypeVariable resolveTypeVariable) { // Still unresolved, let's use the upper bound, checking first if // a cycle may exist - if (!hasCycle(typeVariable)) { - return visit(typeVariable.getUpperBound(), descriptor); + if (!hasCycle(resolveTypeVariable)) { + return visit(resolveTypeVariable.getUpperBound(), descriptor); } } else { - return visit(typeMirror, descriptor); + return visit(resolvedGeneric, descriptor); } } // Fallback to simple representation of the upper bound - return defaultAction(t.getUpperBound(), descriptor); + return defaultAction(typeVariable.getUpperBound(), descriptor); } private boolean hasCycle(TypeVariable variable) { @@ -394,20 +395,11 @@ class TypeUtils { private final Map generics = new HashMap<>(); TypeMirror resolveGeneric(TypeVariable typeVariable) { - if (this.generics.containsKey(typeVariable)) { - TypeMirror resolvedType = this.generics.get(typeVariable); - // Unresolved -> - if (resolvedType == typeVariable) { - return resolvedType; - } - // -> -> - if (resolvedType instanceof TypeVariable) { - return resolveGeneric((TypeVariable) resolvedType); - } - // Resolved e.g. java.lang.String - return resolvedType; + TypeMirror resolved = this.generics.get(typeVariable); + if (resolved != typeVariable && resolved instanceof TypeVariable resolvedTypeVariable) { + return resolveGeneric(resolvedTypeVariable); } - return null; + return resolved; } private void registerIfNecessary(TypeMirror variable, TypeMirror resolution) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java index c1f06d7ccc..0593b6cbcb 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java @@ -54,7 +54,6 @@ class TypeUtilsTests { assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(String.class.getName()); assertThat(getTypeOfField(typeUtils, typeElement, "mappings")) .hasToString(constructMapType(Integer.class, Duration.class)); - }); }