Skip to content

Commit c259a67

Browse files
snicollscottfrederick
authored andcommitted
Make resolution algorithm of ConnectionDetailsFactory more explicit
This commit moves the resolution check for ConnectionDetailsFactory to a dedicated method to make it more clear that it is meant to verify that the implementation is resolved and can be loaded from the classpath. The previous algorithm relied on a behavior of ResolvableType that only resolves the first level generics. Further improvements in Spring Framework 6.2 make this check invalid as some implementations use a Container that can hold a nested generic. See gh-39737
1 parent 54cdc83 commit c259a67

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/service/connection/ConnectionDetailsFactories.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -122,13 +122,30 @@ record Registration<S, D extends ConnectionDetails>(Class<S> sourceType, Class<D
122122
@SuppressWarnings("unchecked")
123123
private static <S, D extends ConnectionDetails> Registration<S, D> get(ConnectionDetailsFactory<S, D> factory) {
124124
ResolvableType type = ResolvableType.forClass(ConnectionDetailsFactory.class, factory.getClass());
125-
if (!type.hasUnresolvableGenerics()) {
126-
Class<?>[] generics = type.resolveGenerics();
125+
Class<?>[] generics = resolveGenerics(type);
126+
if (generics != null) {
127127
return new Registration<>((Class<S>) generics[0], (Class<D>) generics[1], factory);
128128
}
129129
return null;
130130
}
131131

132+
/**
133+
* Resolve the generics of the given {@link ResolvableType} or {@code null} if any
134+
* of them cannot be resolved.
135+
* @param type the type to inspect
136+
* @return the resolved generics if they can be loaded from the classpath,
137+
* {@code null} otherwise
138+
*/
139+
private static Class<?>[] resolveGenerics(ResolvableType type) {
140+
Class<?>[] resolvedGenerics = type.resolveGenerics();
141+
for (Class<?> genericRawType : resolvedGenerics) {
142+
if (genericRawType == null) {
143+
return null;
144+
}
145+
}
146+
return resolvedGenerics;
147+
}
148+
132149
}
133150

134151
}

0 commit comments

Comments
 (0)