Skip to content

Commit

Permalink
Rely on standard parameter name resolution in Bean Validation 3.0
Browse files Browse the repository at this point in the history
Just configuring additional Kotlin reflection if Kotlin is present.

Closes gh-29566
  • Loading branch information
jhoeller committed Nov 24, 2022
1 parent cbf25b7 commit 284cf3e
Showing 1 changed file with 14 additions and 3 deletions.
Expand Up @@ -49,7 +49,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.KotlinDetector;
import org.springframework.core.KotlinReflectionParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -100,7 +101,7 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
private ConstraintValidatorFactory constraintValidatorFactory;

@Nullable
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
private ParameterNameDiscoverer parameterNameDiscoverer;

@Nullable
private Resource[] mappingLocations;
Expand All @@ -117,6 +118,13 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
private ValidatorFactory validatorFactory;


public LocalValidatorFactoryBean() {
if (KotlinDetector.isKotlinReflectPresent()) {
this.parameterNameDiscoverer = new KotlinReflectionParameterNameDiscoverer();
}
}


/**
* Specify the desired provider class, if any.
* <p>If not specified, JSR-303's default search mechanism will be used.
Expand Down Expand Up @@ -188,7 +196,10 @@ public void setConstraintValidatorFactory(ConstraintValidatorFactory constraintV
/**
* Set the ParameterNameDiscoverer to use for resolving method and constructor
* parameter names if needed for message interpolation.
* <p>Default is a {@link org.springframework.core.DefaultParameterNameDiscoverer}.
* <p>Default is Hibernate Validator's own internal use of standard Java reflection,
* with an additional {@link KotlinReflectionParameterNameDiscoverer} if Kotlin
* is present. This may be overridden with a custom subclass or a Spring-controlled
* {@link org.springframework.core.DefaultParameterNameDiscoverer} if necessary,
*/
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) {
this.parameterNameDiscoverer = parameterNameDiscoverer;
Expand Down

0 comments on commit 284cf3e

Please sign in to comment.