Author: hardy.ferentschik
Date: 2009-04-08 10:58:09 -0400 (Wed, 08 Apr 2009)
New Revision: 16281
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/Optional.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
Log:
HV-112
GroupSequences
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-04-08
13:21:22 UTC (rev 16280)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-04-08
14:58:09 UTC (rev 16281)
@@ -136,6 +136,28 @@
return defaultGroupSequence;
}
+ public void setDefaultGroupSequence(List<Class<?>> groupSequence) {
+ defaultGroupSequence = new ArrayList<Class<?>>();
+ for ( Class<?> group : groupSequence ) {
+ if ( group.getName().equals( beanClass.getName() ) ) {
+ defaultGroupSequence.add( Default.class );
+ }
+ else if ( group.getName().equals( Default.class.getName() ) ) {
+ throw new ValidationException( "'Default.class' cannot appear in default
group sequence list." );
+ }
+ else {
+ defaultGroupSequence.add( group );
+ }
+ }
+ if ( log.isDebugEnabled() ) {
+ log.debug(
+ "Bean {} redefines the Default group. Members of the default group sequence
are: {}",
+ beanClass.getName(),
+ defaultGroupSequence
+ );
+ }
+ }
+
public Set<PropertyDescriptor> getConstrainedProperties() {
return Collections.unmodifiableSet( new HashSet<PropertyDescriptor>(
propertyDescriptors.values() ) );
}
@@ -148,7 +170,7 @@
*/
private void createMetaData(AnnotationIgnores annotationIgnores) {
beanDescriptor = new BeanDescriptorImpl<T>( this );
- initDefaultGroupSequence( beanClass );
+ initDefaultGroupSequence();
List<Class> classes = new ArrayList<Class>();
computeClassHierarchy( beanClass, classes );
for ( Class current : classes ) {
@@ -186,35 +208,17 @@
/**
* Checks whether there is a default group sequence defined for this class.
* See HV-113.
- *
- * @param clazz The class to check for the <code>GroupSequence</code>
annotation.
*/
- private void initDefaultGroupSequence(Class<?> clazz) {
- GroupSequence groupSequenceAnnotation = clazz.getAnnotation( GroupSequence.class );
+ private void initDefaultGroupSequence() {
+ List<Class<?>> groupSequence = new ArrayList<Class<?>>();
+ GroupSequence groupSequenceAnnotation = beanClass.getAnnotation( GroupSequence.class
);
if ( groupSequenceAnnotation == null ) {
- defaultGroupSequence.add( Default.class );
+ groupSequence.add( beanClass );
}
else {
- List<Class<?>> groupSequenceList = Arrays.asList(
groupSequenceAnnotation.value() );
- for ( Class<?> group : groupSequenceList ) {
- if ( group.getName().equals( clazz.getName() ) ) {
- defaultGroupSequence.add( Default.class );
- }
- else if ( group.getName().equals( Default.class.getName() ) ) {
- throw new ValidationException( "'Default.class' cannot appear in
default group sequence list." );
- }
- else {
- defaultGroupSequence.add( group );
- }
- }
- if ( log.isDebugEnabled() ) {
- log.debug(
- "Bean {} redefines the Default group. Members of the default group sequence
are: {}",
- clazz.getName(),
- defaultGroupSequence
- );
- }
+ groupSequence.addAll( Arrays.asList( groupSequenceAnnotation.value() ) );
}
+ setDefaultGroupSequence( groupSequence );
}
private <A extends Annotation> void initFieldConstraints(Class clazz,
AnnotationIgnores annotationIgnores) {
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-08
13:21:22 UTC (rev 16280)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-08
14:58:09 UTC (rev 16281)
@@ -62,6 +62,7 @@
import org.hibernate.validation.xml.ElementType;
import org.hibernate.validation.xml.FieldType;
import org.hibernate.validation.xml.GetterType;
+import org.hibernate.validation.xml.GroupSequenceType;
import org.hibernate.validation.xml.GroupsType;
/**
@@ -81,9 +82,12 @@
private final ConstraintHelper constraintHelper = new ConstraintHelper();
private static final String PACKAGE_SEPERATOR = ".";
+ private final Set<Class<?>> processedClasses = new
HashSet<Class<?>>();
+
private AnnotationIgnores annotationIgnores;
private Map<Class<?>, List<MetaConstraint<?, ?>>>
constraintMap;
- private List<Member> cascadedMembers = new ArrayList<Member>();
+ private Map<Class<?>, List<Member>> cascadedMembers;
+ private Map<Class<?>, List<Class<?>>> defaultSequences;
public ValidatorFactoryImpl(ConfigurationState configurationState) {
this.messageInterpolator = configurationState.getMessageInterpolator();
@@ -92,6 +96,8 @@
annotationIgnores = new AnnotationIgnores();
constraintMap = new HashMap<Class<?>, List<MetaConstraint<?,
?>>>();
+ cascadedMembers = new HashMap<Class<?>, List<Member>>();
+ defaultSequences = new HashMap<Class<?>, List<Class<?>>>();
parseMappingFiles( configurationState.getMappingStreams() );
initBeanMetaData();
@@ -134,7 +140,6 @@
}
private void parseMappingFiles(Set<InputStream> mappingStreams) {
- Set<Class<?>> processedClasses = new HashSet<Class<?>>();
for ( InputStream in : mappingStreams ) {
try {
ConstraintMappingsType mappings = getValidationConfig( in );
@@ -182,7 +187,7 @@
// valid
if ( fieldType.getValid() != null ) {
- cascadedMembers.add( field );
+ addCascadedMember( beanClass, field );
}
// constraints
@@ -211,7 +216,7 @@
// valid
if ( getterType.getValid() != null ) {
- cascadedMembers.add( method );
+ addCascadedMember( beanClass, method );
}
// constraints
@@ -228,16 +233,35 @@
if ( classType == null ) {
return;
}
+
+ // ignore annotation
boolean ignoreClassAnnotation = classType.isIgnoreAnnotations() == null ? false :
classType.isIgnoreAnnotations();
if ( ignoreClassAnnotation ) {
annotationIgnores.setIgnoreAnnotationsOnClass( beanClass );
}
+
+ // group sequence
+ List<Class<?>> groupSequence = createGroupSequence(
classType.getGroupSequence(), beanClass, defaultPackage );
+ if ( !groupSequence.isEmpty() ) {
+ defaultSequences.put( beanClass, groupSequence );
+ }
+
+ // constraints
for ( ConstraintType constraint : classType.getConstraint() ) {
MetaConstraint<?, ?> metaConstraint = createMetaConstraint( constraint,
beanClass, null, defaultPackage );
addMetaConstraint( beanClass, metaConstraint );
}
}
+ private List<Class<?>> createGroupSequence(GroupSequenceType
groupSequenceType, Class<?> beanClass, String defaultPackage) {
+ List<Class<?>> groupSequence = new ArrayList<Class<?>>();
+ for ( String groupName : groupSequenceType.getValue() ) {
+ Class<?> group = getClass( groupName, defaultPackage );
+ groupSequence.add( group );
+ }
+ return groupSequence;
+ }
+
private void addMetaConstraint(Class<?> beanClass, MetaConstraint<?, ?>
metaConstraint) {
if ( constraintMap.containsKey( beanClass ) ) {
constraintMap.get( beanClass ).add( metaConstraint );
@@ -249,6 +273,17 @@
}
}
+ private void addCascadedMember(Class<?> beanClass, Member member) {
+ if ( cascadedMembers.containsKey( beanClass ) ) {
+ cascadedMembers.get( beanClass ).add( member );
+ }
+ else {
+ List<Member> tmpList = new ArrayList<Member>();
+ tmpList.add( member );
+ cascadedMembers.put( beanClass, tmpList );
+ }
+ }
+
private <A extends Annotation, T> MetaConstraint<?, ?>
createMetaConstraint(ConstraintType constraint, Class<T> beanClass, Member member,
String defaultPackage) {
@SuppressWarnings("unchecked")
Class<A> annotationClass = ( Class<A> ) getClass(
constraint.getAnnotation(), defaultPackage );
@@ -473,15 +508,19 @@
}
private void initBeanMetaData() {
- for ( Map.Entry<Class<?>, List<MetaConstraint<?, ?>>> entry :
constraintMap.entrySet() ) {
- BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl( entry.getKey(),
constraintHelper, annotationIgnores );
- for ( MetaConstraint<?, ?> metaConstraint : entry.getValue() ) {
- metaData.addMetaConstraint( metaConstraint );
+
+ for ( Class<?> beanClass : processedClasses ) {
+ BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl( beanClass,
constraintHelper, annotationIgnores );
+ for ( MetaConstraint<?, ?> constraint : constraintMap.get( beanClass ) ) {
+ metaData.addMetaConstraint( constraint );
}
- for ( Member m : cascadedMembers ) {
+ for ( Member m : cascadedMembers.get( beanClass ) ) {
metaData.addCascadedMember( m );
}
- BeanMetaDataCache.addBeanMetaData( entry.getKey(), metaData );
+ if ( defaultSequences.containsKey( beanClass ) ) {
+ metaData.setDefaultGroupSequence( defaultSequences.get( beanClass ) );
+ }
+ BeanMetaDataCache.addBeanMetaData( beanClass, metaData );
}
}
}
Copied:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/Optional.java
(from rev 16271,
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/TestGroup.java)
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/Optional.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/Optional.java 2009-04-08
14:58:09 UTC (rev 16281)
@@ -0,0 +1,24 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validation.engine.xml;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public interface Optional {
+}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java 2009-04-08
13:21:22 UTC (rev 16280)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java 2009-04-08
14:58:09 UTC (rev 16281)
@@ -17,7 +17,7 @@
private CreditCard creditcard;
- @Pattern(regexp = "[0-9 -]+", message = "A phone number can only contain
numbers, whitespaces and dashes.")
+ @Pattern(regexp = "[0-9 -]+", message = "A phone number can only contain
numbers, whitespaces and dashes.", groups = Optional.class)
private String phoneNumber;
@NotNull(groups = Default.class)
Modified:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
===================================================================
---
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-08
13:21:22 UTC (rev 16280)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-08
14:58:09 UTC (rev 16281)
@@ -5,7 +5,8 @@
<bean class="User" ignore-annotations="false">
<class ignore-annotations="true">
<group-sequence>
- <value>value0</value>
+ <value>User</value>
+ <value>Optional</value>
</group-sequence>
<constraint
annotation="org.hibernate.validation.engine.xml.ConsistentUserInformation">
<message>Message from xml</message>