Author: hardy.ferentschik
Date: 2009-08-19 10:38:01 -0400 (Wed, 19 Aug 2009)
New Revision: 17360
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java
Log:
made sure that corresponding annottation to xml defined constratins are avialable via the
BV meta data API
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-08-19
12:49:16 UTC (rev 17359)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-08-19
14:38:01 UTC (rev 17360)
@@ -18,11 +18,13 @@
package org.hibernate.validation.metadata;
import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.security.AccessController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -31,21 +33,19 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.security.AccessController;
import javax.validation.GroupDefinitionException;
import javax.validation.GroupSequence;
import javax.validation.Valid;
-import javax.validation.ValidationException;
import javax.validation.groups.Default;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.PropertyDescriptor;
import org.slf4j.Logger;
-import org.hibernate.validation.util.LoggerFactory;
-import org.hibernate.validation.util.ReflectionHelper;
import org.hibernate.validation.util.GetDeclaredFields;
import org.hibernate.validation.util.GetDeclaredMethods;
+import org.hibernate.validation.util.LoggerFactory;
+import org.hibernate.validation.util.ReflectionHelper;
import org.hibernate.validation.util.SetAccessibility;
@@ -98,7 +98,7 @@
//updated on the fly, needs to be thread safe
//property name
- private final Set<String> propertyNames = new HashSet<String>(30);
+ private final Set<String> propertyNames = new HashSet<String>( 30 );
public BeanMetaDataImpl(Class<T> beanClass, ConstraintHelper constraintHelper) {
this(
@@ -139,6 +139,7 @@
}
public void addMetaConstraint(Class<?> clazz, MetaConstraint<T, ? extends
Annotation> metaConstraint) {
+ // first we add the meta constraint to our meta constraint map
List<MetaConstraint<T, ? extends Annotation>> constraintList;
if ( !metaConstraints.containsKey( clazz ) ) {
constraintList = new ArrayList<MetaConstraint<T, ? extends
Annotation>>();
@@ -148,6 +149,20 @@
constraintList = metaConstraints.get( clazz );
}
constraintList.add( metaConstraint );
+
+ // but we also have to update the descriptors exposing the BV metadata API
+ if ( metaConstraint.getElementType() == ElementType.TYPE ) {
+ beanDescriptor.addConstraintDescriptor( metaConstraint.getDescriptor() );
+ }
+ else {
+ PropertyDescriptorImpl propertyDescriptor = ( PropertyDescriptorImpl )
propertyDescriptors.get(
+ metaConstraint.getPropertyName()
+ );
+ if ( propertyDescriptor == null ) {
+ propertyDescriptor = addPropertyDescriptorForMember( metaConstraint.getMember() );
+ }
+ propertyDescriptor.addConstraintDescriptor( metaConstraint.getDescriptor() );
+ }
}
public void addCascadedMember(Member member) {
@@ -275,7 +290,7 @@
continue;
}
String name = ReflectionHelper.getPropertyName( field );
- if (name != null) {
+ if ( name != null ) {
propertyNames.add( name );
}
List<ConstraintDescriptorImpl<?>> fieldMetadata = findConstraints( field
);
@@ -297,7 +312,7 @@
private void setAccessibility(Member member) {
SetAccessibility action = SetAccessibility.action( member );
- if (System.getSecurityManager() != null) {
+ if ( System.getSecurityManager() != null ) {
AccessController.doPrivileged( action );
}
else {
@@ -321,7 +336,7 @@
continue;
}
String name = ReflectionHelper.getPropertyName( method );
- if (name != null) {
+ if ( name != null ) {
propertyNames.add( name );
}
@@ -430,9 +445,6 @@
for ( Annotation annotation : beanClass.getAnnotations() ) {
metadata.addAll( findConstraintAnnotations( beanClass, annotation ) );
}
- for ( ConstraintDescriptorImpl constraintDescriptor : metadata ) {
- beanDescriptor.addConstraintDescriptor( constraintDescriptor );
- }
return metadata;
}
@@ -453,19 +465,6 @@
metadata.addAll( findConstraintAnnotations( member.getDeclaringClass(), annotation )
);
}
- String name = ReflectionHelper.getPropertyName( member );
- for ( ConstraintDescriptorImpl constraintDescriptor : metadata ) {
- if ( member instanceof Method && name == null ) { // can happen if member is a
Method which does not follow the bean convention
- throw new ValidationException(
- "Annotated methods must follow the JavaBeans naming convention. " +
member.getName() + "() does not."
- );
- }
- PropertyDescriptorImpl propertyDescriptor = ( PropertyDescriptorImpl )
propertyDescriptors.get( name );
- if ( propertyDescriptor == null ) {
- propertyDescriptor = addPropertyDescriptorForMember( member );
- }
- propertyDescriptor.addConstraintDescriptor( constraintDescriptor );
- }
return metadata;
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java 2009-08-19
12:49:16 UTC (rev 17359)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java 2009-08-19
14:38:01 UTC (rev 17360)
@@ -43,7 +43,7 @@
this.type = type;
}
- public void addConstraintDescriptor(ConstraintDescriptorImpl constraintDescriptor) {
+ public void addConstraintDescriptor(ConstraintDescriptor constraintDescriptor) {
constraintDescriptors.add( constraintDescriptor );
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java 2009-08-19
12:49:16 UTC (rev 17359)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java 2009-08-19
14:38:01 UTC (rev 17360)
@@ -27,6 +27,7 @@
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
import javax.validation.metadata.ConstraintDescriptor;
import org.hibernate.validation.engine.ConstraintTree;
@@ -89,6 +90,11 @@
}
this.member = member;
this.propertyName = ReflectionHelper.getPropertyName( member );
+ if ( member instanceof Method && propertyName == null ) { // can happen if
member is a Method which does not follow the bean convention
+ throw new ValidationException(
+ "Annotated methods must follow the JavaBeans naming convention. " +
member.getName() + "() does not."
+ );
+ }
this.beanClass = beanClass;
constraintTree = new ConstraintTree<A>( constraintDescriptor );
}
@@ -110,6 +116,10 @@
return beanClass;
}
+ public Member getMember() {
+ return member;
+ }
+
/**
* @return The JavaBeans name of the field/property the constraint was placed on. {@code
null} if this is a
* class level constraint.