Force AOT resolution of Spring Factories constructors

This commit forces the resolution of all declared constructors when
registering `RuntimeHints` for Spring factories. Resolving constructors
can throw additional `NoClassDefFoundError` at runtime and during native
image compilation, if a type exposed by the constructor is missing from
the current classloader.

See gh-27955
This commit is contained in:
Brian Clozel 2022-05-11 12:11:14 +02:00
parent 8b6dcb9ccc
commit e6c0152916
1 changed files with 6 additions and 2 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.core.io.support;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@ -88,9 +89,12 @@ class SpringFactoriesLoaderRuntimeHintsRegistrar implements RuntimeHintsRegistra
@Nullable
private Class<?> resolveClassName(ClassLoader classLoader, String factoryClassName) {
try {
return ClassUtils.resolveClassName(factoryClassName, classLoader);
Class<?> className = ClassUtils.resolveClassName(factoryClassName, classLoader);
// Force resolution of all constructors to catch
Constructor<?>[] constructors = className.getDeclaredConstructors();
return className;
}
catch (Exception ex) {
catch (Throwable ex) {
return null;
}
}