Skip to content

Commit 46b14de

Browse files
committed
Polish 'Handle generics with identical names in different positions'
See gh-45011
1 parent e24fd50 commit 46b14de

File tree

2 files changed

+14
-23
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src

2 files changed

+14
-23
lines changed

Diff for: spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java

+14-22
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Stephane Nicoll
4747
* @author Phillip Webb
4848
* @author Pavel Anisimov
49+
* @author Dmytro Nosan
4950
*/
5051
class TypeUtils {
5152

@@ -217,7 +218,7 @@ private TypeKind getPrimitiveFor(TypeMirror type) {
217218
return WRAPPER_TO_PRIMITIVE.get(type.toString());
218219
}
219220

220-
TypeDescriptor resolveTypeDescriptor(TypeElement element) {
221+
private TypeDescriptor resolveTypeDescriptor(TypeElement element) {
221222
if (this.typeDescriptors.containsKey(element)) {
222223
return this.typeDescriptors.get(element);
223224
}
@@ -318,22 +319,22 @@ private String determineQualifiedName(DeclaredType type, TypeElement enclosingEl
318319
}
319320

320321
@Override
321-
public String visitTypeVariable(TypeVariable t, TypeDescriptor descriptor) {
322-
TypeMirror typeMirror = descriptor.resolveGeneric(t);
323-
if (typeMirror != null) {
324-
if (typeMirror instanceof TypeVariable typeVariable) {
322+
public String visitTypeVariable(TypeVariable typeVariable, TypeDescriptor descriptor) {
323+
TypeMirror resolvedGeneric = descriptor.resolveGeneric(typeVariable);
324+
if (resolvedGeneric != null) {
325+
if (resolvedGeneric instanceof TypeVariable resolveTypeVariable) {
325326
// Still unresolved, let's use the upper bound, checking first if
326327
// a cycle may exist
327-
if (!hasCycle(typeVariable)) {
328-
return visit(typeVariable.getUpperBound(), descriptor);
328+
if (!hasCycle(resolveTypeVariable)) {
329+
return visit(resolveTypeVariable.getUpperBound(), descriptor);
329330
}
330331
}
331332
else {
332-
return visit(typeMirror, descriptor);
333+
return visit(resolvedGeneric, descriptor);
333334
}
334335
}
335336
// Fallback to simple representation of the upper bound
336-
return defaultAction(t.getUpperBound(), descriptor);
337+
return defaultAction(typeVariable.getUpperBound(), descriptor);
337338
}
338339

339340
private boolean hasCycle(TypeVariable variable) {
@@ -394,20 +395,11 @@ static class TypeDescriptor {
394395
private final Map<TypeVariable, TypeMirror> generics = new HashMap<>();
395396

396397
TypeMirror resolveGeneric(TypeVariable typeVariable) {
397-
if (this.generics.containsKey(typeVariable)) {
398-
TypeMirror resolvedType = this.generics.get(typeVariable);
399-
// Unresolved <T> -> <T>
400-
if (resolvedType == typeVariable) {
401-
return resolvedType;
402-
}
403-
// <T> -> <T1> -> <T2>
404-
if (resolvedType instanceof TypeVariable) {
405-
return resolveGeneric((TypeVariable) resolvedType);
406-
}
407-
// Resolved e.g. java.lang.String
408-
return resolvedType;
398+
TypeMirror resolved = this.generics.get(typeVariable);
399+
if (resolved != typeVariable && resolved instanceof TypeVariable resolvedTypeVariable) {
400+
return resolveGeneric(resolvedTypeVariable);
409401
}
410-
return null;
402+
return resolved;
411403
}
412404

413405
private void registerIfNecessary(TypeMirror variable, TypeMirror resolution) {

Diff for: spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ void resolveTypeOnConcreteClass() {
5454
assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(String.class.getName());
5555
assertThat(getTypeOfField(typeUtils, typeElement, "mappings"))
5656
.hasToString(constructMapType(Integer.class, Duration.class));
57-
5857
});
5958
}
6059

0 commit comments

Comments
 (0)