Specific check for parent of MethodInvocationInfo ClassLoader

See gh-30389
This commit is contained in:
Juergen Hoeller 2023-06-12 11:33:54 +02:00
parent fe7f8e2de5
commit cc8c852c2b
1 changed files with 16 additions and 3 deletions

View File

@ -780,11 +780,24 @@ public class MvcUriComponentsBuilder {
else if (controllerType.isInterface()) {
ClassLoader classLoader = controllerType.getClassLoader();
if (classLoader == null || classLoader.getParent() == null) {
// JDK interface type from bootstrap loader or platform loader ->
// use higher-level loader which can see Spring infrastructure classes
if (classLoader == null) {
// JDK bootstrap loader -> use MethodInvocationInfo ClassLoader instead.
classLoader = MethodInvocationInfo.class.getClassLoader();
}
else if (classLoader.getParent() == null) {
// Potentially the JDK platform loader on JDK 9+
ClassLoader miiClassLoader = MethodInvocationInfo.class.getClassLoader();
ClassLoader miiParent = miiClassLoader.getParent();
while (miiParent != null) {
if (classLoader == miiParent) {
// Suggested ClassLoader is ancestor of MethodInvocationInfo ClassLoader
// -> use MethodInvocationInfo ClassLoader itself instead.
classLoader = miiClassLoader;
break;
}
miiParent = miiParent.getParent();
}
}
Class<?>[] ifcs = new Class<?>[] {controllerType, MethodInvocationInfo.class};
return (T) Proxy.newProxyInstance(classLoader, ifcs, interceptor);
}