Skip runtime hint registration for constraint with missing dependencies
Prior to this commit, AOT processing for bean validation failed with a NoClassDefFoundError for constraints with missing dependencies. With this commit, the processing no longer fails, and a warning is logged instead. See gh-33940 Closes gh-33949 Co-authored-by: Sam Brannen <sam.brannen@broadcom.com>
This commit is contained in:
committed by
Sam Brannen
parent
f3753e6d64
commit
9b0253e117
@@ -44,6 +44,7 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.core.OverridingClassLoader;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
||||
@@ -134,6 +135,14 @@ class BeanValidationBeanRegistrationAotProcessorTests {
|
||||
.withMemberCategory(MemberCategory.DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
|
||||
}
|
||||
|
||||
@Test // gh-33940
|
||||
void shouldSkipConstraintWithMissingDependency() throws Exception {
|
||||
FilteringClassLoader classLoader = new FilteringClassLoader(getClass().getClassLoader());
|
||||
Class<?> beanClass = classLoader.loadClass(ConstraintWithMissingDependency.class.getName());
|
||||
process(beanClass);
|
||||
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).isEmpty();
|
||||
}
|
||||
|
||||
private void process(Class<?> beanClass) {
|
||||
BeanRegistrationAotContribution contribution = createContribution(beanClass);
|
||||
if (contribution != null) {
|
||||
@@ -269,4 +278,31 @@ class BeanValidationBeanRegistrationAotProcessorTests {
|
||||
Optional<BeanWithRecursiveOptional> optional;
|
||||
}
|
||||
|
||||
static class ConstraintWithMissingDependency {
|
||||
|
||||
private final Filtered filtered = new Filtered();
|
||||
}
|
||||
|
||||
static class Filtered {}
|
||||
|
||||
static class FilteringClassLoader extends OverridingClassLoader {
|
||||
|
||||
FilteringClassLoader(ClassLoader parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isEligibleForOverriding(String className) {
|
||||
return className.startsWith(BeanValidationBeanRegistrationAotProcessorTests.class.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException {
|
||||
if (name.contains("Filtered")) {
|
||||
throw new NoClassDefFoundError(name);
|
||||
}
|
||||
return super.loadClassForOverriding(name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user