Skip to content

Commit

Permalink
Force AOT resolution of Spring Factories constructors
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bclozel committed May 11, 2022
1 parent 8b6dcb9 commit e6c0152
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,9 +89,12 @@ private void registerHints(RuntimeHints hints, ClassLoader classLoader,
@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;
}
}
Expand Down

0 comments on commit e6c0152

Please sign in to comment.