org.apache.bval.jsr303
Class ClassValidator

java.lang.Object
  extended by org.apache.bval.jsr303.ClassValidator
All Implemented Interfaces:
Validator, CascadingPropertyValidator
Direct Known Subclasses:
MethodValidatorImpl

public class ClassValidator
extends Object
implements CascadingPropertyValidator

Objects of this class are able to validate bean instances (and the associated object graphs).

Implementation is thread-safe.

API class

Version:
$Rev: 1166451 $ $Date: 2011-09-08 00:32:26 +0200 (Thu, 08 Sep 2011) $
Author:
Roman Stumm, Carlos Vara

Nested Class Summary
protected  class ClassValidator.Jsr303ValidationCallback
          Dispatches a call from ClassValidator.Jsr303ValidationCallback.validate() to validateBeanNet(GroupValidationContext) with the current context set.
 
Field Summary
protected  ApacheFactoryContext factoryContext
          ApacheFactoryContext used
protected  GroupsComputer groupsComputer
          GroupsComputer used
 
Constructor Summary
ClassValidator(ApacheFactoryContext factoryContext)
          Create a new ClassValidator instance.
ClassValidator(ApacheValidatorFactory factory)
          Deprecated. provided for backward compatibility
 
Method Summary
protected  BeanDescriptorImpl createBeanDescriptor(MetaBean metaBean)
          Create a BeanDescriptorImpl
protected
<T> GroupValidationContext<T>
createContext(MetaBean metaBean, T object, Class<T> objectClass, Class<?>... groups)
          Create a GroupValidationContext.
protected  ValidationContextTraversal createValidationContextTraversal(GroupValidationContext<?> validationContext)
          Create a ValidationContextTraversal instance for this ClassValidator.
 BeanDescriptor getConstraintsForClass(Class<?> clazz)
           Return the descriptor object describing bean constraints.
protected  MetaBeanFinder getMetaBeanFinder()
          Get the metabean finder associated with this validator.
 boolean isTreatMapsLikeBeans()
          Behavior configuration -

 void setTreatMapsLikeBeans(boolean treatMapsLikeBeans)
          Set whether maps are to be treated like beans.
protected static RuntimeException unrecoverableValidationError(RuntimeException ex, Object object)
          Generate an unrecoverable validation error
<T> T
unwrap(Class<T> type)
           Return an instance of the specified type allowing access to provider-specific APIs.
<T> Set<ConstraintViolation<T>>
validate(T object, Class<?>... groups)
           Validates all constraints on object.
protected  void validateBeanNet(GroupValidationContext<?> context)
          Validates a bean and all its cascaded related beans for the currently defined group.
<T> Set<ConstraintViolation<T>>
validateProperty(T object, String propertyName, boolean cascade, Class<?>... groups)
          Validates all constraints placed on object's propertyName property, with optional validation cascading.
<T> Set<ConstraintViolation<T>>
validateProperty(T object, String propertyName, Class<?>... groups)
           Validates all constraints placed on the property of object named propertyName.
<T> Set<ConstraintViolation<T>>
validateValue(Class<T> beanType, String propertyName, Object value, boolean cascade, Class<?>... groups)
          Validates all constraints placed on object's propertyName property, with optional validation cascading, given a hypothetical property value.
<T> Set<ConstraintViolation<T>>
validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups)
           Validates all constraints placed on the property named propertyName of the class beanType would the property value be value

ConstraintViolation objects return null for ConstraintViolation.getRootBean() and ConstraintViolation.getLeafBean()

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factoryContext

protected final ApacheFactoryContext factoryContext
ApacheFactoryContext used


groupsComputer

protected final GroupsComputer groupsComputer
GroupsComputer used

Constructor Detail

ClassValidator

public ClassValidator(ApacheFactoryContext factoryContext)
Create a new ClassValidator instance.

Parameters:
factoryContext -

ClassValidator

public ClassValidator(ApacheValidatorFactory factory)
Deprecated. provided for backward compatibility

Create a new ClassValidator instance.

Parameters:
factory -
Method Detail

getMetaBeanFinder

protected MetaBeanFinder getMetaBeanFinder()
Get the metabean finder associated with this validator.

Returns:
a MetaBeanFinder
See Also:
MetaBeanManagerFactory.getFinder()

validate

public <T> Set<ConstraintViolation<T>> validate(T object,
                                                Class<?>... groups)
Validates all constraints on object.

Specified by:
validate in interface Validator
Parameters:
object - object to validate
groups - group or list of groups targeted for validation (default to Default)
Returns:
constraint violations or an empty Set if none
Throws:
IllegalArgumentException - if object is null or if null is passed to the varargs groups
ValidationException - if a non recoverable error happens during the validation process

validateProperty

public <T> Set<ConstraintViolation<T>> validateProperty(T object,
                                                        String propertyName,
                                                        Class<?>... groups)
Validates all constraints placed on the property of object named propertyName.

Specified by:
validateProperty in interface Validator
Parameters:
object - object to validate
propertyName - property to validate (ie field and getter constraints). Nested properties may be referenced (e.g. prop[2].subpropA.subpropB)
groups - group or list of groups targeted for validation (default to Default)
Returns:
constraint violations or an empty Set if none
Throws:
IllegalArgumentException - if object is null, if propertyName null, empty or not a valid object property or if null is passed to the varargs groups
ValidationException - if a non recoverable error happens during the validation process

validateProperty

public <T> Set<ConstraintViolation<T>> validateProperty(T object,
                                                        String propertyName,
                                                        boolean cascade,
                                                        Class<?>... groups)
Validates all constraints placed on object's propertyName property, with optional validation cascading.

Specified by:
validateProperty in interface CascadingPropertyValidator
Returns:
the resulting Set of ConstraintViolations.

validateValue

public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType,
                                                     String propertyName,
                                                     Object value,
                                                     Class<?>... groups)
Validates all constraints placed on the property named propertyName of the class beanType would the property value be value

ConstraintViolation objects return null for ConstraintViolation.getRootBean() and ConstraintViolation.getLeafBean()

Specified by:
validateValue in interface Validator
Parameters:
beanType - the bean type
propertyName - property to validate
value - property value to validate
groups - group or list of groups targeted for validation (default to Default)
Returns:
constraint violations or an empty Set if none
Throws:
IllegalArgumentException - if beanType is null, if propertyName null, empty or not a valid object property or if null is passed to the varargs groups
ValidationException - if a non recoverable error happens during the validation process

validateValue

public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType,
                                                     String propertyName,
                                                     Object value,
                                                     boolean cascade,
                                                     Class<?>... groups)
Validates all constraints placed on object's propertyName property, with optional validation cascading, given a hypothetical property value.

Specified by:
validateValue in interface CascadingPropertyValidator
Returns:
the resulting Set of ConstraintViolations.

getConstraintsForClass

public BeanDescriptor getConstraintsForClass(Class<?> clazz)
Return the descriptor object describing bean constraints. The returned object (and associated objects including ConstraintDescriptors) are immutable.

Specified by:
getConstraintsForClass in interface Validator
Parameters:
clazz - class or interface type evaluated
Returns:
the bean descriptor for the specified class.
Throws:
IllegalArgumentException - if clazz is null
ValidationException - if a non recoverable error happens during the metadata discovery or if some constraints are invalid.

unwrap

public <T> T unwrap(Class<T> type)
Return an instance of the specified type allowing access to provider-specific APIs. If the Bean Validation provider implementation does not support the specified class, ValidationException is thrown.

Specified by:
unwrap in interface Validator
Parameters:
type - the class of the object to be returned.
Returns:
an instance of the specified class
Throws:
ValidationException - if the provider does not support the call.

validateBeanNet

protected void validateBeanNet(GroupValidationContext<?> context)
Validates a bean and all its cascaded related beans for the currently defined group.

Special code is present to manage the Default group.

Parameters:
validationContext - The current context of this validation call. Must have its GroupValidationContext.getCurrentGroup() field set.

unrecoverableValidationError

protected static RuntimeException unrecoverableValidationError(RuntimeException ex,
                                                               Object object)
Generate an unrecoverable validation error

Parameters:
ex -
object -
Returns:
a RuntimeException of the appropriate type

createContext

protected <T> GroupValidationContext<T> createContext(MetaBean metaBean,
                                                      T object,
                                                      Class<T> objectClass,
                                                      Class<?>... groups)
Create a GroupValidationContext.

Type Parameters:
T -
Parameters:
metaBean -
object -
objectClass -
groups -
Returns:
GroupValidationContext instance

createBeanDescriptor

protected BeanDescriptorImpl createBeanDescriptor(MetaBean metaBean)
Create a BeanDescriptorImpl

Parameters:
metaBean -
Returns:
BeanDescriptorImpl instance

isTreatMapsLikeBeans

public boolean isTreatMapsLikeBeans()
Behavior configuration -

Returns:
treatMapsLikeBeans - true (validate maps like beans, so that you can use Maps to validate dynamic classes or beans for which you have the MetaBean but no instances) - false (default), validate maps like collections (validating the values only)

(is still configuration to better in BeanValidationContext?)


setTreatMapsLikeBeans

public void setTreatMapsLikeBeans(boolean treatMapsLikeBeans)
Set whether maps are to be treated like beans.

Parameters:
treatMapsLikeBeans - - true (validate maps like beans, so that you can use Maps to validate dynamic classes or beans for which you have the MetaBean but no instances) - false (default), validate maps like collections (validating the values only)

createValidationContextTraversal

protected ValidationContextTraversal createValidationContextTraversal(GroupValidationContext<?> validationContext)
Create a ValidationContextTraversal instance for this ClassValidator.

Parameters:
validationContext -
Returns:
ValidationContextTraversal


Copyright © 2010-2012 Apache Software Foundation. All Rights Reserved.