Search in sources :

Example 1 with ParameterNameProvider

use of jakarta.validation.ParameterNameProvider in project today-infrastructure by TAKETODAY.

the class LocalValidatorFactoryBean method configureParameterNameProvider.

private void configureParameterNameProvider(ParameterNameDiscoverer discoverer, Configuration<?> configuration) {
    final ParameterNameProvider defaultProvider = configuration.getDefaultParameterNameProvider();
    configuration.parameterNameProvider(new ParameterNameProvider() {

        @Override
        public List<String> getParameterNames(Constructor<?> constructor) {
            String[] paramNames = discoverer.getParameterNames(constructor);
            return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(constructor));
        }

        @Override
        public List<String> getParameterNames(Method method) {
            String[] paramNames = discoverer.getParameterNames(method);
            return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(method));
        }
    });
}
Also used : ParameterNameProvider(jakarta.validation.ParameterNameProvider) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method)

Example 2 with ParameterNameProvider

use of jakarta.validation.ParameterNameProvider in project hibernate-validator by hibernate.

the class ParameterlessMethodValidationTest method testEmptyParameters.

@Test
@TestForIssue(jiraKey = "HV-966")
public void testEmptyParameters() throws NoSuchMethodException {
    HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
    ParameterNameProvider providerMock = new ParameterNameProvider() {

        @Override
        public List<String> getParameterNames(Constructor<?> constructor) {
            throw new IllegalStateException("this method shouldn't be invoked");
        }

        @Override
        public List<String> getParameterNames(Method method) {
            if (method.getParameters().length == 0) {
                throw new IllegalStateException("getParameterNames() shouldn't be invoked for parameterless method" + method.getName());
            }
            return Arrays.asList(new String[method.getParameterCount()]);
        }
    };
    Validator validator = config.parameterNameProvider(providerMock).buildValidatorFactory().getValidator();
    Bar bar = new Bar();
    assertNoViolations(validator.forExecutables().validateParameters(bar, bar.getClass().getMethod("getString"), new Object[] {}));
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ParameterNameProvider(jakarta.validation.ParameterNameProvider) Constructor(java.lang.reflect.Constructor) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Method(java.lang.reflect.Method) HibernateValidator(org.hibernate.validator.HibernateValidator) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with ParameterNameProvider

use of jakarta.validation.ParameterNameProvider in project spring-framework by spring-projects.

the class LocalValidatorFactoryBean method configureParameterNameProvider.

private void configureParameterNameProvider(ParameterNameDiscoverer discoverer, Configuration<?> configuration) {
    final ParameterNameProvider defaultProvider = configuration.getDefaultParameterNameProvider();
    configuration.parameterNameProvider(new ParameterNameProvider() {

        @Override
        public List<String> getParameterNames(Constructor<?> constructor) {
            String[] paramNames = discoverer.getParameterNames(constructor);
            return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(constructor));
        }

        @Override
        public List<String> getParameterNames(Method method) {
            String[] paramNames = discoverer.getParameterNames(method);
            return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(method));
        }
    });
}
Also used : ParameterNameProvider(jakarta.validation.ParameterNameProvider) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method)

Example 4 with ParameterNameProvider

use of jakarta.validation.ParameterNameProvider in project hibernate-validator by hibernate.

the class ValidatorFactoryBean method createParameterNameProvider.

private ParameterNameProvider createParameterNameProvider(Configuration<?> config) {
    BootstrapConfiguration bootstrapConfiguration = config.getBootstrapConfiguration();
    String parameterNameProviderFqcn = bootstrapConfiguration.getParameterNameProviderClassName();
    if (parameterNameProviderFqcn == null) {
        return config.getDefaultParameterNameProvider();
    }
    @SuppressWarnings("unchecked") Class<? extends ParameterNameProvider> parameterNameProviderClass = (Class<? extends ParameterNameProvider>) run(LoadClass.action(parameterNameProviderFqcn, null));
    return createInstance(parameterNameProviderClass);
}
Also used : ParameterNameProvider(jakarta.validation.ParameterNameProvider) BootstrapConfiguration(jakarta.validation.BootstrapConfiguration) LoadClass(org.hibernate.validator.internal.util.privilegedactions.LoadClass)

Example 5 with ParameterNameProvider

use of jakarta.validation.ParameterNameProvider in project beanvalidation-tck by eclipse-ee4j.

the class ManagedObjectsTest method testParameterNameProviderIsSubjectToDependencyInjection.

@Test
@SpecAssertion(section = Sections.INTEGRATION_GENERAL_OBJECTSLIFECYCLE, id = "d")
@SpecAssertion(section = Sections.INTEGRATION_DEPENDENCYINJECTION_CUSTOMCONFIGURATION, id = "a")
@SpecAssertion(section = Sections.INTEGRATION_DEPENDENCYINJECTION, id = "a")
public void testParameterNameProviderIsSubjectToDependencyInjection() {
    assertNotNull(defaultValidatorFactory);
    ParameterNameProvider parameterNameProvider = defaultValidatorFactory.getParameterNameProvider();
    assertEquals(parameterNameProvider.getParameterNames((Constructor<?>) null), Arrays.asList(Greeter.MESSAGE));
}
Also used : ParameterNameProvider(jakarta.validation.ParameterNameProvider) Constructor(java.lang.reflect.Constructor) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test) IntegrationTest(org.hibernate.beanvalidation.tck.util.IntegrationTest) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Aggregations

ParameterNameProvider (jakarta.validation.ParameterNameProvider)8 Method (java.lang.reflect.Method)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.testng.annotations.Test)3 Constructor (java.lang.reflect.Constructor)2 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)2 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)2 BootstrapConfiguration (jakarta.validation.BootstrapConfiguration)1 ClockProvider (jakarta.validation.ClockProvider)1 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)1 MessageInterpolator (jakarta.validation.MessageInterpolator)1 TraversableResolver (jakarta.validation.TraversableResolver)1 ValidationException (jakarta.validation.ValidationException)1 Validator (jakarta.validation.Validator)1 ExecutableType (jakarta.validation.executable.ExecutableType)1 ValueExtractor (jakarta.validation.valueextraction.ValueExtractor)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1