javax.validation.Configuration.parameterNameProvider()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中javax.validation.Configuration.parameterNameProvider()方法的一些代码示例,展示了Configuration.parameterNameProvider()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.parameterNameProvider()方法的具体详情如下:
包路径:javax.validation.Configuration
类名称:Configuration
方法名:parameterNameProvider

Configuration.parameterNameProvider介绍

[英]Defines the parameter name provider. Has priority over the configuration based provider.

If null is passed, the default parameter name provider is used (defined in XML or the specification default).
[中]定义参数名提供程序。优先于基于配置的提供程序。
如果传递null,则使用默认参数名提供程序(在XML或默认规范中定义)。

代码示例

代码示例来源:origin: spring-projects/spring-framework

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

代码示例来源:origin: org.springframework/spring-context

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

代码示例来源:origin: apache/servicecomb-java-chassis

@Override
public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
  Object[] args)
  throws ConstraintViolationException {
 if (null == executableValidator) {
  ValidatorFactory factory =
    Validation.byDefaultProvider()
      .configure()
      .parameterNameProvider(new DefaultParameterNameProvider())
      .buildValidatorFactory();
  executableValidator = factory.getValidator().forExecutables();
 }
 Set<ConstraintViolation<Object>> violations =
   executableValidator.validateParameters(producerOperation.getProducerInstance(),
     producerOperation.getProducerMethod(),
     args,
     Default.class);
 if (violations.size() > 0) {
  LOGGER.warn("Parameter validation failed : " + violations.toString());
  throw new ConstraintViolationException(violations);
 }
}

代码示例来源:origin: hibernate/hibernate-validator

public ValidationExtension() {
  Configuration<?> config = Validation.byDefaultProvider().configure();
  // we use the default ParameterNameProvider here as we cannot use the injected one
  // as it hasn't been turned into a managed bean yet and might not be able to
  // return a parameter name. At this stage, we don't care about the parameter names.
  config.parameterNameProvider( config.getDefaultParameterNameProvider() );
  BootstrapConfiguration bootstrap = config.getBootstrapConfiguration();
  globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes();
  isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled();
  validatorFactory = config.buildValidatorFactory();
  validator = validatorFactory.getValidator();
  getterPropertySelectionStrategyHelper = GetterPropertySelectionStrategyHelper.forValidationFactory( validatorFactory );
  executableHelper = new ExecutableHelper( new TypeResolutionHelper() );
}

代码示例来源:origin: org.apache.tomee/openejb-core

@Override
public T parameterNameProvider(ParameterNameProvider parameterNameProvider) {
  return delegate.parameterNameProvider(parameterNameProvider);
}

代码示例来源:origin: hibernate/hibernate-validator

@Override
public ValidatorFactory create(CreationalContext<ValidatorFactory> ctx) {
  Configuration<?> config = getConfiguration();
  config.constraintValidatorFactory( createConstraintValidatorFactory( config ) );
  config.messageInterpolator( createMessageInterpolator( config ) );
  config.traversableResolver( createTraversableResolver( config ) );
  config.parameterNameProvider( createParameterNameProvider( config ) );
  config.clockProvider( createClockProvider( config ) );
  addValueExtractorBeans( config );
  return config.buildValidatorFactory();
}

代码示例来源:origin: apache/servicemix-bundles

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

代码示例来源:origin: org.juzu/juzu-plugins-validation

@PostConstruct
public void start() {
 ValidatorFactory factory = Validation.byDefaultProvider().configure().parameterNameProvider(this).buildValidatorFactory();
 validator = factory.getValidator();
}

代码示例来源:origin: juzu/juzu

@PostConstruct
public void start() {
 ValidatorFactory factory = Validation.byDefaultProvider().configure().parameterNameProvider(this).buildValidatorFactory();
 validator = factory.getValidator();
}

代码示例来源:origin: apache/cxf

private static void initFactoryConfig(Configuration<?> factoryCfg, ValidationConfiguration cfg) {
  if (cfg != null) {
    factoryCfg.parameterNameProvider(cfg.getParameterNameProvider());
    factoryCfg.messageInterpolator(cfg.getMessageInterpolator());
    factoryCfg.traversableResolver(cfg.getTraversableResolver());
    factoryCfg.constraintValidatorFactory(cfg.getConstraintValidatorFactory());
    for (Map.Entry<String, String> entry : cfg.getProperties().entrySet()) {
      factoryCfg.addProperty(entry.getKey(), entry.getValue());
    }
  }
}

代码示例来源:origin: org.apache.cxf/cxf-core

private static void initFactoryConfig(Configuration<?> factoryCfg, ValidationConfiguration cfg) {
  if (cfg != null) {
    factoryCfg.parameterNameProvider(cfg.getParameterNameProvider());
    factoryCfg.messageInterpolator(cfg.getMessageInterpolator());
    factoryCfg.traversableResolver(cfg.getTraversableResolver());
    factoryCfg.constraintValidatorFactory(cfg.getConstraintValidatorFactory());
    for (Map.Entry<String, String> entry : cfg.getProperties().entrySet()) {
      factoryCfg.addProperty(entry.getKey(), entry.getValue());
    }
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

@Test(expectedExceptions = UnsupportedOperationException.class,
    expectedExceptionsMessageRegExp = "Exception in ParameterNameProvider")
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_PARAMETERCONSTRAINTS_NAMINGPARAMETERS, id = "b")
public void testExceptionInParameterNameProviderIsWrappedIntoValidationException()
    throws Throwable {
  Validator validator = TestUtil.getConfigurationUnderTest()
      .parameterNameProvider( new BrokenCustomParameterNameProvider() )
      .buildValidatorFactory()
      .getValidator();
  try {
    Object object = new User();
    Method method = User.class.getMethod( "setNames", String.class, String.class );
    Object[] parameters = new Object[] { null, null };
    validator.forExecutables().validateParameters( object, method, parameters );
    fail( "Expected exception wasn't thrown" );
  }
  catch ( ValidationException e ) {
    throw e.getCause();
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

@Test
@SpecAssertion(section = Sections.VALIDATIONAPI_BOOTSTRAPPING_VALIDATORFACTORY, id = "e")
public void testGetParameterNameProviderFromValidatorFactory() {
  BrokenCustomParameterNameProvider parameterNameProvider = new BrokenCustomParameterNameProvider();
  ValidatorFactory validatorFactory = TestUtil.getConfigurationUnderTest()
      .parameterNameProvider( parameterNameProvider )
      .buildValidatorFactory();
  assertSame(
      validatorFactory.getParameterNameProvider(),
      parameterNameProvider,
      "getParameterNameProvider() should return the parameter name provider set via configuration"
  );
}

代码示例来源:origin: org.apache.servicecomb/swagger-invocation-validator

@Override
public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
  Object[] args)
  throws ConstraintViolationException {
 if (null == executableValidator) {
  ValidatorFactory factory =
    Validation.byDefaultProvider()
      .configure()
      .parameterNameProvider(new DefaultParameterNameProvider())
      .buildValidatorFactory();
  executableValidator = factory.getValidator().forExecutables();
 }
 Set<ConstraintViolation<Object>> violations =
   executableValidator.validateParameters(producerOperation.getProducerInstance(),
     producerOperation.getProducerMethod(),
     args,
     Default.class);
 if (violations.size() > 0) {
  LOGGER.warn("Parameter validation failed : " + violations.toString());
  throw new ConstraintViolationException(violations);
 }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

.parameterNameProvider( new CustomParameterNameProvider() )
.buildValidatorFactory()
.getValidator()

代码示例来源:origin: org.seedstack.seed/seed-core

private GlobalValidatorFactory() {
  validatorFactory = Validation.byDefaultProvider()
      .configure()
      .parameterNameProvider(new ReflectionParameterNameProvider())
      .messageInterpolator(new SeedMessageInterpolator())
      .buildValidatorFactory();
}

代码示例来源:origin: org.hibernate.validator/hibernate-validator-cdi

public ValidationExtension() {
  Configuration<?> config = Validation.byDefaultProvider().configure();
  // we use the default ParameterNameProvider here as we cannot use the injected one
  // as it hasn't been turned into a managed bean yet and might not be able to
  // return a parameter name. At this stage, we don't care about the parameter names.
  config.parameterNameProvider( config.getDefaultParameterNameProvider() );
  BootstrapConfiguration bootstrap = config.getBootstrapConfiguration();
  globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes();
  isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled();
  validatorFactory = config.buildValidatorFactory();
  validator = validatorFactory.getValidator();
  executableHelper = new ExecutableHelper( new TypeResolutionHelper() );
}

代码示例来源:origin: org.seedstack.seed/seed-core

@Override
  public ValidatorFactory get() {
    return Validation.byDefaultProvider()
        .configure()
        .parameterNameProvider(new ReflectionParameterNameProvider())
        .messageInterpolator(new SeedMessageInterpolator())
        .constraintValidatorFactory(new SeedConstraintValidatorFactory(injector))
        .buildValidatorFactory();
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

@Test
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "aw")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "az")
public void testAddParameterNodeUsingCustomParameterNameProvider() throws Exception {
  ExecutableValidator executableValidator = TestUtil.getConfigurationUnderTest()
      .parameterNameProvider( new CustomParameterNameProvider() )
      .buildValidatorFactory()
      .getValidator()
      .forExecutables();
  Object[] parameterValues = new Object[] { Collections.emptyMap() };
  Set<ConstraintViolation<User>> constraintViolations = executableValidator.validateParameters(
      new User(),
      User.class.getMethod( "setAddresses", Map.class ),
      parameterValues
  );
  assertThat( constraintViolations ).containsOnlyPaths(
      pathWith().method( "setAddresses" ).parameter( "param0", 0 ),
      pathWith().method( "setAddresses" ).parameter( "param0", 0 ).bean(),
      pathWith().method( "setAddresses" )
          .parameter( "param0", 0 )
          .property( "myNode1", true, null, 23 )
          .bean()
  );
}

代码示例来源:origin: org.hibernate.validator/hibernate-validator-cdi

@Override
public ValidatorFactory create(CreationalContext<ValidatorFactory> ctx) {
  Configuration<?> config = getConfiguration();
  config.constraintValidatorFactory( createConstraintValidatorFactory( config ) );
  config.messageInterpolator( createMessageInterpolator( config ) );
  config.traversableResolver( createTraversableResolver( config ) );
  config.parameterNameProvider( createParameterNameProvider( config ) );
  config.clockProvider( createClockProvider( config ) );
  addValueExtractorBeans( config );
  return config.buildValidatorFactory();
}

相关文章