Skip to content

Commit

Permalink
Skip validation constraint hint inference if no provider available
Browse files Browse the repository at this point in the history
Closes gh-30130
  • Loading branch information
jhoeller committed Mar 17, 2023
1 parent 3af8efb commit 88bc504
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.NoProviderFoundException;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.metadata.BeanDescriptor;
Expand Down Expand Up @@ -48,6 +49,7 @@
* required for {@link ConstraintValidator}s.
*
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 6.0.5
*/
class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
Expand All @@ -57,21 +59,39 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP

private static final Log logger = LogFactory.getLog(BeanValidationBeanRegistrationAotProcessor.class);

@Nullable

@Override
@Nullable
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
if (isBeanValidationPresent) {
return BeanValidationDelegate.processAheadOfTime(registeredBean);
}
return null;
}


private static class BeanValidationDelegate {

private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
@Nullable
private static final Validator validator = getValidatorIfAvailable();

@Nullable
private static Validator getValidatorIfAvailable() {
try {
return Validation.buildDefaultValidatorFactory().getValidator();
}
catch (NoProviderFoundException ex) {
logger.info("No Bean Validation provider available - skipping validation constraint hint inference");
return null;
}
}

@Nullable
public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
if (validator == null) {
return null;
}

BeanDescriptor descriptor;
try {
descriptor = validator.getConstraintsForClass(registeredBean.getBeanClass());
Expand All @@ -88,6 +108,7 @@ public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean
}
return null;
}

Set<ConstraintDescriptor<?>> constraintDescriptors = new HashSet<>();
for (MethodDescriptor methodDescriptor : descriptor.getConstrainedMethods(MethodType.NON_GETTER, MethodType.GETTER)) {
for (ParameterDescriptor parameterDescriptor : methodDescriptor.getParameterDescriptors()) {
Expand All @@ -107,9 +128,9 @@ public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean
}
return null;
}

}


private static class BeanValidationBeanRegistrationAotContribution implements BeanRegistrationAotContribution {

private final Collection<ConstraintDescriptor<?>> constraintDescriptors;
Expand Down

0 comments on commit 88bc504

Please sign in to comment.