org.springframework.validation.beanvalidation
Class SpringValidatorAdapter

java.lang.Object
  extended by org.springframework.validation.beanvalidation.SpringValidatorAdapter
All Implemented Interfaces:
Validator
Direct Known Subclasses:
CustomValidatorBean, LocalValidatorFactoryBean

public class SpringValidatorAdapter
extends java.lang.Object
implements Validator

Adapter that takes a JSR-303 javax.validator.Validator and exposes it as a Spring Validator while also exposing the original JSR-303 Validator interface itself.

Can be used as a programmatic wrapper. Also serves as base class for CustomValidatorBean and LocalValidatorFactoryBean.

Since:
3.0
Author:
Juergen Hoeller

Field Summary
private static java.util.Set<java.lang.String> internalAnnotationAttributes
           
private  javax.validation.Validator targetValidator
           
 
Constructor Summary
SpringValidatorAdapter()
           
SpringValidatorAdapter(javax.validation.Validator targetValidator)
          Create a new SpringValidatorAdapter for the given JSR-303 Validator.
 
Method Summary
protected  java.lang.Object[] getArgumentsForConstraint(java.lang.String objectName, java.lang.String field,  descriptor)
          Return FieldError arguments for a validation error on the given field.
 BeanDescriptor getConstraintsForClass(java.lang.Class<?> clazz)
           
(package private)  void setTargetValidator(javax.validation.Validator targetValidator)
           
 boolean supports(java.lang.Class<?> clazz)
          Can this Validator validate instances of the supplied clazz?
<T> T
unwrap(java.lang.Class<T> type)
           
 void validate(java.lang.Object target, Errors errors)
          Validate the supplied target object, which must be of a Class for which the Validator.supports(Class) method typically has (or would) return true.
<T> java.util.Set<>
validate(T object, java.lang.Class<?>... groups)
           
<T> java.util.Set<>
validateProperty(T object, java.lang.String propertyName, java.lang.Class<?>... groups)
           
<T> java.util.Set<>
validateValue(java.lang.Class<T> beanType, java.lang.String propertyName, java.lang.Object value, java.lang.Class<?>... groups)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

internalAnnotationAttributes

private static final java.util.Set<java.lang.String> internalAnnotationAttributes

targetValidator

private javax.validation.Validator targetValidator
Constructor Detail

SpringValidatorAdapter

public SpringValidatorAdapter(javax.validation.Validator targetValidator)
Create a new SpringValidatorAdapter for the given JSR-303 Validator.

Parameters:
targetValidator - the JSR-303 Validator to wrap

SpringValidatorAdapter

SpringValidatorAdapter()
Method Detail

setTargetValidator

void setTargetValidator(javax.validation.Validator targetValidator)

supports

public boolean supports(java.lang.Class<?> clazz)
Description copied from interface: Validator
Can this Validator validate instances of the supplied clazz?

This method is typically implemented like so:

return Foo.class.isAssignableFrom(clazz);
(Where Foo is the class (or superclass) of the actual object instance that is to be validated.)

Specified by:
supports in interface Validator
Parameters:
clazz - the Class that this Validator is being asked if it can validate
Returns:
true if this Validator can indeed validate instances of the supplied clazz

validate

public void validate(java.lang.Object target,
                     Errors errors)
Description copied from interface: Validator
Validate the supplied target object, which must be of a Class for which the Validator.supports(Class) method typically has (or would) return true.

The supplied errors instance can be used to report any resulting validation errors.

Specified by:
validate in interface Validator
Parameters:
target - the object that is to be validated (can be null)
errors - contextual state about the validation process (never null)
See Also:
ValidationUtils

getArgumentsForConstraint

protected java.lang.Object[] getArgumentsForConstraint(java.lang.String objectName,
                                                       java.lang.String field,
                                                        descriptor)
Return FieldError arguments for a validation error on the given field. Invoked for each violated constraint.

The default implementation returns a first argument indicating the field name (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes). Afterwards, it adds all actual constraint annotation attributes (i.e. excluding "message", "groups" and "payload") in alphabetical order of their attribute names.

Can be overridden to e.g. add further attributes from the constraint descriptor.

Parameters:
objectName - the name of the target object
field - the field that caused the binding error
descriptor - the JSR-303 constraint descriptor
Returns:
the Object array that represents the FieldError arguments
See Also:
DefaultMessageSourceResolvable.getArguments(), DefaultMessageSourceResolvable, DefaultBindingErrorProcessor.getArgumentsForBindError(java.lang.String, java.lang.String)

validate

public <T> java.util.Set<> validate(T object,
                                         java.lang.Class<?>... groups)

validateProperty

public <T> java.util.Set<> validateProperty(T object,
                                                 java.lang.String propertyName,
                                                 java.lang.Class<?>... groups)

validateValue

public <T> java.util.Set<> validateValue(java.lang.Class<T> beanType,
                                              java.lang.String propertyName,
                                              java.lang.Object value,
                                              java.lang.Class<?>... groups)

getConstraintsForClass

public BeanDescriptor getConstraintsForClass(java.lang.Class<?> clazz)

unwrap

public <T> T unwrap(java.lang.Class<T> type)