From 9b0253e117049c264f9ca0737eca45319f23b805 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Sat, 23 Nov 2024 16:10:47 +0100 Subject: [PATCH] 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 --- ...alidationBeanRegistrationAotProcessor.java | 9 ++--- ...tionBeanRegistrationAotProcessorTests.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java index d9680e775d86..11d18ad5da6c 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java @@ -127,18 +127,19 @@ private static void processAheadOfTime(Class clazz, Set> visitedClas try { descriptor = validator.getConstraintsForClass(clazz); } - catch (RuntimeException ex) { + catch (RuntimeException | LinkageError ex) { + String className = clazz.getName(); if (KotlinDetector.isKotlinType(clazz) && ex instanceof ArrayIndexOutOfBoundsException) { // See https://hibernate.atlassian.net/browse/HV-1796 and https://youtrack.jetbrains.com/issue/KT-40857 - logger.warn("Skipping validation constraint hint inference for class " + clazz + + logger.warn("Skipping validation constraint hint inference for class " + className + " due to an ArrayIndexOutOfBoundsException at validator level"); } else if (ex instanceof TypeNotPresentException) { logger.debug("Skipping validation constraint hint inference for class " + - clazz + " due to a TypeNotPresentException at validator level: " + ex.getMessage()); + className + " due to a TypeNotPresentException at validator level: " + ex.getMessage()); } else { - logger.warn("Skipping validation constraint hint inference for class " + clazz, ex); + logger.warn("Skipping validation constraint hint inference for class " + className, ex); } return; } diff --git a/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java b/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java index b3f3de83cf85..a062786daa93 100644 --- a/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java @@ -44,6 +44,7 @@ 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 @@ void shouldProcessRecursiveGenericsWithoutInfiniteRecursion(Class beanClass) .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 @@ static class BeanWithRecursiveOptional { Optional 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); + } + } + }