[hibernate-commits] Hibernate SVN: r15318 - in validator/trunk: hibernate-validator/src/main/java/org/hibernate/validation/impl and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Oct 10 16:09:50 EDT 2008


Author: epbernard
Date: 2008-10-10 16:09:49 -0400 (Fri, 10 Oct 2008)
New Revision: 15318

Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ElementDescriptorImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/impl/ResourceBundleMessageResolverTest.java
   validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java
   validator/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java
   validator/trunk/validation-api/src/main/java/javax/validation/InvalidConstraint.java
   validator/trunk/validation-api/src/main/java/javax/validation/Validator.java
Log:
BVAL-37 ConstraintDescriptor.getLeafValue()

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -108,7 +108,7 @@
 	 * for this validator and create meta data.
 	 */
 	private void createMetaData() {
-		beanDescriptor = new ElementDescriptorImpl( ElementType.TYPE, beanClass, false, "" );
+		beanDescriptor = new ElementDescriptorImpl( beanClass, false, "" );
 		List<Class> classes = new ArrayList<Class>();
 		computeClassHierarchy( beanClass, classes );
 		for ( Class current : classes ) {
@@ -286,7 +286,7 @@
 			throw new ValidationException( "Unable to intialize " + constraintValidator.value(), e );
 		}
 
-		return new ConstraintDescriptorImpl( annotation, groups, constraint );
+		return new ConstraintDescriptorImpl( annotation, groups, constraint, constraintValidator.value() );
 	}
 
 	private <A extends Annotation> String getMessage(A annotation) {
@@ -387,7 +387,6 @@
 			ElementDescriptorImpl elementDescriptor = ( ElementDescriptorImpl ) propertyDescriptors.get( methodName );
 			if ( elementDescriptor == null ) {
 				elementDescriptor = new ElementDescriptorImpl(
-						ElementType.METHOD,
 						method.getReturnType(),
 						method.isAnnotationPresent( Valid.class ),
 						methodName
@@ -420,7 +419,6 @@
 			ElementDescriptorImpl elementDescriptor = ( ElementDescriptorImpl ) propertyDescriptors.get( fieldName );
 			if ( elementDescriptor == null ) {
 				elementDescriptor = new ElementDescriptorImpl(
-						ElementType.FIELD,
 						field.getType(),
 						field.isAnnotationPresent( Valid.class ),
 						fieldName

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	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -173,7 +173,8 @@
 				continue;
 			}
 
-			Object value = metaData.getValue( context.peekValidatedObject() );
+			final Object leafBeanInstance = context.peekValidatedObject();
+			Object value = metaData.getValue( leafBeanInstance );
 			ContextImpl contextImpl = new ContextImpl(constraintDescriptor);
 
 			if ( !constraintDescriptor.getConstraintImplementation().isValid( value, contextImpl ) ) {
@@ -181,12 +182,13 @@
 					String message = messageResolver.interpolate(
 							error.getMessage(),
 							constraintDescriptor,
-							context.peekValidatedObject()
+							leafBeanInstance
 					);
 					InvalidConstraintImpl<T> failingConstraint = new InvalidConstraintImpl<T>(
 							message,
 							context.getRootBean(),
 							metaDataProvider.getBeanClass(),
+							leafBeanInstance,
 							value,
 							context.peekPropertyPath(), //FIXME use error.getProperty()
 							context.getCurrentGroup()
@@ -319,6 +321,7 @@
 								message,
 								object,
 								( Class<T> ) object.getClass(),
+								object,
 								wrapper.value,
 								propertyIter.getOriginalProperty(), //FIXME use error.getProperty()
 								group
@@ -380,6 +383,7 @@
 								message,
 								null,
 								null,
+								null,
 								object,
 								propertyIter.getOriginalProperty(),  //FIXME use error.getProperty()
 								""
@@ -507,7 +511,7 @@
 		return metaDataProvider.getConstraintMetaDataList().size() > 0;
 	}
 
-	public ElementDescriptor getBeanConstraints() {
+	public ElementDescriptor getConstraintsForBean() {
 		return metaDataProvider.getBeanDescriptor();
 	}
 

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -39,12 +39,12 @@
 public class ConstraintDescriptorImpl implements ConstraintDescriptor {
 	private final Annotation annotation;
 	private final Constraint constraintImplementation;
+	private final Class<? extends Constraint> constraintClass;
 	private final Set<String> contexts;
 	private final Map<String, Object> parameters;
 	private final boolean isReportAsSingleInvalidConstraint;
 
-
-	public ConstraintDescriptorImpl(Annotation annotation, String[] contexts, Constraint validator) {
+	public ConstraintDescriptorImpl(Annotation annotation, String[] contexts, Constraint validator, Class<? extends Constraint> constraintClass) {
 		this.annotation = annotation;
 		if ( contexts.length == 0 ) {
 			contexts = new String[] { "default" };
@@ -56,6 +56,7 @@
 		this.isReportAsSingleInvalidConstraint = annotation.annotationType().isAnnotationPresent(
 				ReportAsSingleInvalidConstraint.class
 		);
+		this.constraintClass = constraintClass;
 	}
 
 	/**
@@ -72,12 +73,17 @@
 		return contexts;
 	}
 
+	public Class<? extends Constraint> getContstraintClass() {
+		return constraintClass;
+	}
+
 	public boolean isInGroups(String group) {
 		return contexts.contains( group );
 	}
 
 	/**
 	 * Return the constraint implementation routine
+	 * //FIXME should we get rid of that and call the ConstraintFactory each time??
 	 */
 	public Constraint getConstraintImplementation() {
 		return constraintImplementation;

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ElementDescriptorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ElementDescriptorImpl.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ElementDescriptorImpl.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -33,15 +33,13 @@
  * @todo Handle problem in descirbing cyclic dependecies for propertyPath
  */
 public class ElementDescriptorImpl implements ElementDescriptor {
-	private final ElementType elementType;
 	private final Class returnType;
 	private final boolean cascaded;
 	private final List<ConstraintDescriptor> constraintDescriptors = new ArrayList<ConstraintDescriptor>();
 	private final String propertyPath;
 
 
-	public ElementDescriptorImpl(ElementType elementType, Class returnType, boolean cascaded, String propertyPath) {
-		this.elementType = elementType;
+	public ElementDescriptorImpl(Class returnType, boolean cascaded, String propertyPath) {
 		this.returnType = returnType;
 		this.cascaded = cascaded;
 		this.propertyPath = propertyPath;
@@ -53,17 +51,10 @@
 
 	/**
 	 * {@inheritDoc}
-	 */
-	public ElementType getElementType() {
-		return elementType;
-	}
-
-	/**
-	 * {@inheritDoc}
 	 *
 	 * @todo Generic type or regular type?
 	 */
-	public Class getReturnType() {
+	public Class getType() {
 		return returnType;
 	}
 

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -32,9 +32,10 @@
 	private Object value;
 	private String propertyPath;
 	private HashSet<String> groups;
+	private Object leafBeanInstance;
 
 
-	public InvalidConstraintImpl(String message, T rootBean, Class<T> beanClass, Object value, String propertyPath, String group) {
+	public InvalidConstraintImpl(String message, T rootBean, Class<T> beanClass, Object leafBeanInstance, Object value, String propertyPath, String group) {
 		this.message = message;
 		this.rootBean = rootBean;
 		this.beanClass = beanClass;
@@ -42,6 +43,7 @@
 		this.propertyPath = propertyPath;
 		groups = new HashSet<String>();
 		groups.add( group );
+		this.leafBeanInstance = leafBeanInstance;
 	}
 
 	/**
@@ -58,6 +60,10 @@
 		return rootBean;
 	}
 
+	public Object getLeafBean() {
+		return leafBeanInstance;
+	}
+
 	/**
 	 * {@inheritDoc}
 	 */

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/impl/ResourceBundleMessageResolverTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/impl/ResourceBundleMessageResolverTest.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/impl/ResourceBundleMessageResolverTest.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -90,7 +90,7 @@
 	@Test
 	public void testSuccessfulInterpolation() {
 		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl(
-				notNull, new String[] { }, new NotNullConstraint()
+				notNull, new String[] { }, new NotNullConstraint(), NotNullConstraint.class
 		);
 
 		String expected = "replacement worked";
@@ -113,7 +113,7 @@
 	@Test
 	public void testUnSuccessfulInterpolation() {
 		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl(
-				notNull, new String[] { }, new NotNullConstraint()
+				notNull, new String[] { }, new NotNullConstraint(), NotNullConstraint.class
 		);
 		String expected = "foo";  // missing {}
 		String actual = resolver.interpolate( "foo", desciptor, null );
@@ -127,7 +127,7 @@
 	@Test
 	public void testUnkownTokenInterpolation() {
 		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl(
-				notNull, new String[] { }, new NotNullConstraint()
+				notNull, new String[] { }, new NotNullConstraint(), NotNullConstraint.class
 		);
 		String expected = "{bar}";  // unkown token {}
 		String actual = resolver.interpolate( "{bar}", desciptor, null );
@@ -137,13 +137,13 @@
 	@Test
 	public void testDefaultInterpolation() {
 		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl(
-				notNull, new String[] { }, new NotNullConstraint()
+				notNull, new String[] { }, new NotNullConstraint(), NotNullConstraint.class
 		);
 		String expected = "may not be null";
 		String actual = resolver.interpolate( notNull.message(), desciptor, null );
 		assertEquals( "Wrong substitution", expected, actual );
 
-		desciptor = new ConstraintDescriptorImpl( length, new String[] { }, new NotNullConstraint() );
+		desciptor = new ConstraintDescriptorImpl( length, new String[] { }, new NotNullConstraint(), NotNullConstraint.class );
 		expected = "length must be between 0 and 2147483647";  // unkown token {}
 		actual = resolver.interpolate( length.message(), desciptor, null );
 		assertEquals( "Wrong substitution", expected, actual );

Modified: validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -43,9 +43,9 @@
 	Set<String> getGroups();
 
 	/**
-	 * @return The actual constraint implementation.
+	 * @return the constraint implementation class
 	 */
-	Constraint getConstraintImplementation();
+	Class<? extends Constraint> getContstraintClass();
 
 	/**
 	 * Returns a map containing the annotation paramter names as keys and the annotation parameter values

Modified: validator/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -27,17 +27,13 @@
  * @author Hardy Ferentschik
  */
 public interface ElementDescriptor {
-	/**
-	 * @return The element type.
-	 */
-	ElementType getElementType();
 
 	/**
 	 * @return Statically defined returned type.
 	 *
 	 * @todo should it be Type or even completly removed
 	 */
-	Class getReturnType();
+	Class getType();
 
 	/**
 	 * @return Returns <code>true</code> if the association is cascaded, <code>false</code> otherwise.

Modified: validator/trunk/validation-api/src/main/java/javax/validation/InvalidConstraint.java
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/InvalidConstraint.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/validation-api/src/main/java/javax/validation/InvalidConstraint.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -39,6 +39,21 @@
 	T getRootBean();
 
 	/**
+	 * If a bean constraint, the bean instance the constraint is applied on
+	 * If a property constraint, the bean instance hosting the property the constraint is applied on
+	 * 
+	 * @return the leaf bean the constraint is applied on or null if Validator#validateValue is used
+	 */
+	Object getLeafBean();
+
+	/**
+	 * @return the property path to the value from <code>rootBean</code>
+	 *         <code>null</code> if the value is the <code>rootBean<code> itself.
+	 */
+	String getPropertyPath();
+
+
+	/**
 	 * @return the bean type being validated.
 	 */
 	Class<T> getBeanClass();
@@ -49,14 +64,10 @@
 	Object getValue();
 
 	/**
-	 * @return the property path to the value from <code>rootBean</code>
-	 *         <code>null</code> if the value is the <code>rootBean<code> itself.
-	 */
-	String getPropertyPath();
-
-	/**
 	 * @return the list of groups that the triggered constraint applies on and which also are
 	 *         within the list of groups requested for validation.
+	 *
+	 * TODO: considering removal, if you think it's important, speak up
 	 */
 	Set<String> getGroups();
 }

Modified: validator/trunk/validation-api/src/main/java/javax/validation/Validator.java
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/Validator.java	2008-10-10 09:58:32 UTC (rev 15317)
+++ validator/trunk/validation-api/src/main/java/javax/validation/Validator.java	2008-10-10 20:09:49 UTC (rev 15318)
@@ -79,13 +79,13 @@
 	/**
 	 * return the class level constraints
 	 */
-	ElementDescriptor getBeanConstraints();
+	ElementDescriptor getConstraintsForBean();
 
 	/**
 	 * return the property level constraints for a given propertyName
 	 * or null if either the property does not exist or has no constraint
 	 */
-	ElementDescriptor getConstraintsForProperty(String propertyName);
+	ElementDescriptor getConstraintsForProperty(String propertyName);	
 
 	/**
 	 * return the property names having at least a constraint defined




More information about the hibernate-commits mailing list