Author: hardy.ferentschik
Date: 2009-04-22 10:09:42 -0400 (Wed, 22 Apr 2009)
New Revision: 16401
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java
Log:
HV-144
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java 2009-04-22
11:12:04 UTC (rev 16400)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java 2009-04-22
14:09:42 UTC (rev 16401)
@@ -54,6 +54,11 @@
private final T rootBean;
/**
+ * The root bean class of the validation.
+ */
+ private Class<T> rootBeanClass;
+
+ /**
* Maps a group to an identity set to keep track of already validated objects. We have
to make sure
* that each object gets only validated once per group and property path.
*/
@@ -106,12 +111,36 @@
*/
private final TraversableResolver traversableResolver;
- public ExecutionContext(T object, MessageInterpolator messageInterpolator,
ConstraintValidatorFactory constraintValidatorFactory, TraversableResolver
traversableResolver) {
- this( object, object, messageInterpolator, constraintValidatorFactory,
traversableResolver );
+ public static <T> ExecutionContext<T> getContextForValidate(T object,
MessageInterpolator messageInterpolator, ConstraintValidatorFactory
constraintValidatorFactory, TraversableResolver traversableResolver) {
+ @SuppressWarnings("unchecked")
+ Class<T> rootBeanClass = ( Class<T> ) object.getClass();
+ return new ExecutionContext<T>(
+ rootBeanClass, object, object, messageInterpolator, constraintValidatorFactory,
traversableResolver
+ );
}
- public ExecutionContext(T rootBean, Object object, MessageInterpolator
messageInterpolator, ConstraintValidatorFactory constraintValidatorFactory,
TraversableResolver traversableResolver) {
+ public static <T> ExecutionContext<T>
getContextForValidateValue(Class<T> rootBeanClass, Object object,
MessageInterpolator messageInterpolator, ConstraintValidatorFactory
constraintValidatorFactory, TraversableResolver traversableResolver) {
+ return new ExecutionContext<T>(
+ rootBeanClass,
+ null,
+ object,
+ messageInterpolator,
+ constraintValidatorFactory,
+ traversableResolver
+ );
+ }
+
+ public static <T> ExecutionContext<T> getContextForValidateProperty(T
rootBean, Object object, MessageInterpolator messageInterpolator,
ConstraintValidatorFactory constraintValidatorFactory, TraversableResolver
traversableResolver) {
+ @SuppressWarnings("unchecked")
+ Class<T> rootBeanClass = ( Class<T> ) rootBean.getClass();
+ return new ExecutionContext<T>(
+ rootBeanClass, rootBean, object, messageInterpolator, constraintValidatorFactory,
traversableResolver
+ );
+ }
+
+ private ExecutionContext(Class<T> rootBeanClass, T rootBean, Object object,
MessageInterpolator messageInterpolator, ConstraintValidatorFactory
constraintValidatorFactory, TraversableResolver traversableResolver) {
this.rootBean = rootBean;
+ this.rootBeanClass = rootBeanClass;
this.messageInterpolator = messageInterpolator;
this.constraintValidatorFactory = constraintValidatorFactory;
this.traversableResolver = traversableResolver;
@@ -147,6 +176,12 @@
return rootBean;
}
+ @SuppressWarnings("unchecked")
+ // safe case since rootBean is of type T!
+ public Class<T> getRootBeanClass() {
+ return rootBeanClass;
+ }
+
public Class<?> getCurrentGroup() {
return currentGroup;
}
@@ -283,7 +318,7 @@
return new ConstraintViolationImpl<T>(
messageTemplate,
interpolatedMessage,
- null, //FIXME get the rootBeanClass
+ getRootBeanClass(),
getRootBean(),
peekCurrentBean(),
value,
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-04-22
11:12:04 UTC (rev 16400)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-04-22
14:09:42 UTC (rev 16401)
@@ -103,7 +103,7 @@
}
GroupChain groupChain = determineGroupExecutionOrder( groups );
- ExecutionContext<T> context = new ExecutionContext<T>(
+ ExecutionContext<T> context = ExecutionContext.getContextForValidate(
object, messageInterpolator, constraintValidatorFactory, traversableResolver
);
@@ -399,7 +399,7 @@
for ( Class<?> groupClass : groupList ) {
for ( MetaConstraint<T, ?> metaConstraint : metaConstraints ) {
- ExecutionContext<T> context = new ExecutionContext<T>(
+ ExecutionContext<T> context = ExecutionContext.getContextForValidateProperty(
object,
hostingBeanInstance,
messageInterpolator,
@@ -434,6 +434,7 @@
while ( groupIterator.hasNext() ) {
Group group = groupIterator.next();
validateValueForGroup(
+ beanType,
value,
propertyIter,
failingConstraintViolations,
@@ -449,6 +450,7 @@
int numberOfConstraintViolations = failingConstraintViolations.size();
for ( Group group : sequence ) {
validateValueForGroup(
+ beanType,
value,
propertyIter,
failingConstraintViolations,
@@ -463,7 +465,7 @@
}
}
- private <T> void validateValueForGroup(Object value, PropertyIterator
propertyIter, List<ConstraintViolation<T>> failingConstraintViolations,
Set<MetaConstraint<T, ?>> metaConstraints, Group group) {
+ private <T> void validateValueForGroup(Class<T> beanType, Object value,
PropertyIterator propertyIter, List<ConstraintViolation<T>>
failingConstraintViolations, Set<MetaConstraint<T, ?>> metaConstraints, Group
group) {
int numberOfConstraintViolations = failingConstraintViolations.size();
BeanMetaData<T> beanMetaData = getBeanMetaData(
metaConstraints.iterator().next().getBeanClass() );
@@ -478,8 +480,8 @@
for ( Class<?> groupClass : groupList ) {
for ( MetaConstraint<T, ?> metaConstraint : metaConstraints ) {
- ExecutionContext<T> context = new ExecutionContext<T>(
- ( T ) value, messageInterpolator, constraintValidatorFactory, traversableResolver
+ ExecutionContext<T> context = ExecutionContext.getContextForValidateValue(
+ beanType, value, messageInterpolator, constraintValidatorFactory,
traversableResolver
);
context.pushProperty( propertyIter.getOriginalProperty() );
context.setCurrentGroup( groupClass );
@@ -554,7 +556,7 @@
private <T> BeanMetaData<T> getBeanMetaData(Class<T> beanClass) {
BeanMetaDataImpl<T> metadata = beanMetaDataCache.getBeanMetaData( beanClass );
if ( metadata == null ) {
- metadata = new BeanMetaDataImpl<T>( beanClass, constraintHelper);
+ metadata = new BeanMetaDataImpl<T>( beanClass, constraintHelper );
beanMetaDataCache.addBeanMetaData( beanClass, metadata );
}
return metadata;
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java 2009-04-22
11:12:04 UTC (rev 16400)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java 2009-04-22
14:09:42 UTC (rev 16401)
@@ -266,10 +266,11 @@
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
assertEquals( constraintViolation.getRootBean(), null, "Wrong root entity"
);
+ assertEquals( constraintViolation.getRootBeanClass(), Customer.class, "Wrong root
bean class" );
assertEquals( constraintViolation.getInvalidValue(), null, "Wrong value" );
assertEquals( "orderList[0].orderNumber",
constraintViolation.getPropertyPath(), "Wrong propertyName" );
- constraintViolations = validator.validateValue( Customer.class,
"orderList[0].orderNumber", "1234" );
+ constraintViolations = validator.validateValue( Customer.class,
"orderList[0].orderNumber", 1234 );
assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
}