Skip to content

Commit

Permalink
Skip AOT processing for validation constraint with missing dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Nov 23, 2024
1 parent 5e08a88 commit 8753789
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -124,18 +124,19 @@ private static void processAheadOfTime(Class<?> clazz, Collection<Class<?>> vali
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,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;
Expand Down Expand Up @@ -121,6 +122,14 @@ void shouldProcessTransitiveGenericTypeLevelConstraint() {
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).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) {
Expand Down Expand Up @@ -244,4 +253,31 @@ public void setExclude(List<Exclude> exclude) {
}
}

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);
}
}

}

0 comments on commit 8753789

Please sign in to comment.