I moved
hasConstraints() to the ElementDescritpor
which means that BeanDescriptor, PropertyDescritpor do inherit from
it. Make sense.
I also added a isBeanConstrained() to BeanDescriptor to know if a
given class will be impacted by validation (namely if any of it's
field, method or class / superclass / interfaces is annotated with a
constraint or with @Valid). It is a flag to take care of or ignore a
given class.
I also renamed BeanDescriptor.getPropertiesWithConstraints to
BeanDescriptor.getConstrainedProperties.
I am not too happy with the namings though, especially
isBeanConstrained(), so any improvement proposal is welcome.
5.2. ElementDescriptor
ElementDescriptor is the root interface describing elements hosting
constraints. It is used to describe the list of constraints for a
given element (whether it be a field, a method or a class).
/**
* Describes a validated element (class, field or property).
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
public interface ElementDescriptor {
/**
* return true if at least one constraint declaration is present on
the element.
*/
boolean hasConstraints();
/**
* @return Statically defined returned type.
*
* @todo should it be Type or even completly removed
*/
Class<?> getType();
/**
* @return All the constraint descriptors for this element.
*/
List<ConstraintDescriptor> getConstraintDescriptors();
}
getType returns either the object type for a class, or the returned
type for a property (TODO problem of generics resolution).
getConstraintDescriptors returns the ConstraintDescriptors (see
Section 5.5, “ConstraintDescriptor”), each
ConstraintDescriptordescribing one of the constraints declared on the
given element.
hasConstraints returns true if the element (class, field or property)
holds at lease one constraint declaration.
5.3. BeanDescriptor
The BeanDescriptor interface describes a constrained Java Bean. This
interface is returned by Validator.getConstraintsForClass(Class<?>).
/**
* Describe a constrained Java Bean and the constraints associated to
it.
*
* @author Emmanuel Bernard
*/
public interface BeanDescriptor extends ElementDescriptor {
/**
* Returns true if the bean involves validation:
* - a constraint is hosted on the bean itself
* - a constraint is hosted on one of the bean properties
* - or a bean property is marked for cascade (@Valid)
*
* @return true if the bean nvolves validation
*
*/
boolean isBeanConstrained();
/**
* Return the property level constraints for a given propertyName
* or null if either the property does not exist or has no constraint
* The returned object (and associated objects including
ConstraintDescriptors)
* are immutable.
*
* @param propertyName property evaludated
*/
PropertyDescriptor getConstraintsForProperty(String propertyName);
/**
* return the property names having at least a constraint defined or
marked
* as cascaded (@Valid)
*/
Set<String> getConstrainedProperties();
}
isBeanConstrained returns true if the given class (and superclasses
and interfaces) host at least one validation declaration (either
constraint or@Valid annotation). If the method returns false, the Bean
Validation engine can safely ignore the bean as it will not be
impacted by validation.
getConstraintsForProperty returns a PropertyDescriptor object
describing the property level constraints (See Section 3.1.2, “Field
and property validation”). The property is uniquely identified by its
name as per the JavaBeans convention: field level and getter level
constraints of the given name are all returned.
getConstrainedProperties returns the names of the bean properties
having at least one constraint or being cascaded (@Valid annotation).
5.4. PropertyDescriptor
The PropertyDescriptor interface describes a constrained property of a
Java Bean. This interface is returned
byBeanDescriptor.getConstraintsForProperty(String). Constraints
declared on the attribute and the getter of the same name according to
the Java Bean rules are returned by this descriptor.
/**
* Describes a Java Bean property hosting validation constraints.
*
* Constraints placed on the attribute and the getter for a given
property
* are all referenced by this object.
*
* @author Emmanuel Bernard
*/
public interface PropertyDescriptor extends ElementDescriptor {
/**
* Is the property marked by the <code>@Valid</code> annotation.
* @return true if the annotation is present
*/
boolean isCascaded();
/**
* Name of the property acording to the Java Bean specification.
* @return property name
*/
String getPropertyName();
}
The isCascaded method returns true if the property is marked with
@Valid.
getPropertyName returns the property name as described in Section 4.2,
“ConstraintViolation”.