Author: hardy.ferentschik
Date: 2009-04-07 17:37:10 -0400 (Tue, 07 Apr 2009)
New Revision: 16273
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
Implementations honours the ignore-annotation flags now
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-07
20:49:32 UTC (rev 16272)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-04-07
21:37:10 UTC (rev 16273)
@@ -90,10 +90,32 @@
*/
private final ConstraintHelper constraintHelper;
+ private Map<Class<?>, Boolean> ignoreAnnotationDefaults;
+ private Map<Class<?>, List<Member>> ignoreAnnotationOnMember;
+ private List<Class<?>> ignoreAnnotationOnClass;
+
public BeanMetaDataImpl(Class<T> beanClass, ConstraintHelper constraintHelper) {
+ this(
+ beanClass,
+ constraintHelper,
+ new HashMap<Class<?>, Boolean>(),
+ new ArrayList<Class<?>>(),
+ new HashMap<Class<?>, List<Member>>()
+ );
+ }
+
+ public BeanMetaDataImpl(Class<T> beanClass, ConstraintHelper constraintHelper,
Map<Class<?>, Boolean> ignoreAnnotationDefaults, List<Class<?>>
ignoreAnnotationOnClass, Map<Class<?>, List<Member>>
ignoreAnnotationOnMember) {
this.beanClass = beanClass;
this.constraintHelper = constraintHelper;
+ this.ignoreAnnotationDefaults = ignoreAnnotationDefaults;
+ this.ignoreAnnotationOnClass = ignoreAnnotationOnClass;
+ this.ignoreAnnotationOnMember = ignoreAnnotationOnMember;
+
createMetaData();
+
+ this.ignoreAnnotationDefaults = null;
+ this.ignoreAnnotationOnClass = null;
+ this.ignoreAnnotationOnMember = null;
}
public Class<T> getBeanClass() {
@@ -207,6 +229,9 @@
for ( Field field : clazz.getDeclaredFields() ) {
List<ConstraintDescriptorImpl> fieldMetadata = findConstraints( field );
for ( ConstraintDescriptorImpl constraintDescription : fieldMetadata ) {
+ if ( checkSkipAnnotationProcessing( clazz, field ) ) {
+ break;
+ }
ReflectionHelper.setAccessibility( field );
MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>(
field, beanClass, constraintDescription
@@ -225,6 +250,9 @@
for ( Method method : clazz.getDeclaredMethods() ) {
List<ConstraintDescriptorImpl> methodMetadata = findConstraints( method );
for ( ConstraintDescriptorImpl constraintDescription : methodMetadata ) {
+ if ( checkSkipAnnotationProcessing( clazz, method ) ) {
+ break;
+ }
ReflectionHelper.setAccessibility( method );
MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>(
method, beanClass, constraintDescription
@@ -239,6 +267,29 @@
}
}
+ private boolean checkSkipAnnotationProcessing(Class clazz, Member member) {
+ List<Member> ignoreAnnotationForFields = ignoreAnnotationOnMember.get( clazz );
+ boolean ignoreAnnotation;
+ if ( ignoreAnnotationForFields == null || !ignoreAnnotationForFields.contains( member )
) {
+ ignoreAnnotation = ignoreAnnotationDefaults.containsKey( clazz ) &&
ignoreAnnotationDefaults.get( clazz );
+ }
+ else {
+ ignoreAnnotation = ignoreAnnotationForFields.contains( member );
+ }
+ if ( ignoreAnnotation ) {
+ String type;
+ if ( member instanceof Field ) {
+ type = "Field";
+ }
+ else {
+ type = "Property";
+ }
+ log.debug( type + " level annotations are getting ignored for " +
clazz.getName() + "." + member.getName() );
+ return true;
+ }
+ return false;
+ }
+
private PropertyDescriptorImpl addPropertyDescriptorForMember(Member member) {
String name = ReflectionHelper.getPropertyName( member );
PropertyDescriptorImpl propertyDescriptor = ( PropertyDescriptorImpl )
propertyDescriptors.get(
@@ -256,6 +307,11 @@
}
private <A extends Annotation> void initClassConstraints(Class clazz) {
+ if ( ignoreAnnotationOnClass.contains( clazz ) ||
+ ( ignoreAnnotationDefaults.containsKey( clazz ) &&
ignoreAnnotationDefaults.get( clazz ) ) ) {
+ log.debug( "Class level annotation are getting ignored for " +
clazz.getName() );
+ return;
+ }
List<ConstraintDescriptorImpl> classMetadata = findClassLevelConstraints( clazz
);
for ( ConstraintDescriptorImpl constraintDescription : classMetadata ) {
MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>( clazz,
constraintDescription );
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-07
20:49:32 UTC (rev 16272)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-07
21:37:10 UTC (rev 16273)
@@ -22,9 +22,9 @@
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
+import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
-import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@@ -174,15 +174,28 @@
Field field = ReflectionHelper.getField( beanClass, fieldName );
boolean ignoreFieldAnnotation = fieldType.isIgnoreAnnotations() == null ? false :
fieldType.isIgnoreAnnotations();
if ( ignoreFieldAnnotation ) {
- ignoreAnnotationOnMember.put( beanClass, null );
+ addMemberToIgnoreList( beanClass, field );
}
for ( ConstraintType constraint : fieldType.getConstraint() ) {
- MetaConstraint<?, ?> metaConstraint = createMetaConstraint( constraint,
beanClass, field, defaultPackage );
+ MetaConstraint<?, ?> metaConstraint = createMetaConstraint(
+ constraint, beanClass, field, defaultPackage
+ );
addMetaConstraint( beanClass, metaConstraint );
}
}
}
+ private void addMemberToIgnoreList(Class<?> beanClass, Member member) {
+ if ( ignoreAnnotationOnMember.get( beanClass ) == null ) {
+ List<Member> ignoredAnnotationOnMember = new ArrayList<Member>();
+ ignoredAnnotationOnMember.add( member );
+ ignoreAnnotationOnMember.put( beanClass, ignoredAnnotationOnMember );
+ }
+ else {
+ ignoreAnnotationOnMember.get( beanClass ).add( member );
+ }
+ }
+
private void parsePropertyLevelOverrides(List<GetterType> getters, Class<?>
beanClass, String defaultPackage) {
for ( GetterType getter : getters ) {
String getterName = getter.getName();
@@ -192,10 +205,12 @@
Method method = ReflectionHelper.getMethod( beanClass, getterName );
boolean ignoreGetterAnnotation = getter.isIgnoreAnnotations() == null ? false :
getter.isIgnoreAnnotations();
if ( ignoreGetterAnnotation ) {
- ignoreAnnotationOnMember.put( beanClass, null );
+ addMemberToIgnoreList( beanClass, method );
}
for ( ConstraintType constraint : getter.getConstraint() ) {
- MetaConstraint<?, ?> metaConstraint = createMetaConstraint( constraint,
beanClass, method, defaultPackage );
+ MetaConstraint<?, ?> metaConstraint = createMetaConstraint(
+ constraint, beanClass, method, defaultPackage
+ );
addMetaConstraint( beanClass, metaConstraint );
}
}
@@ -451,7 +466,13 @@
private void initBeanMetaData() {
for ( Map.Entry<Class<?>, List<MetaConstraint<?, ?>>> entry :
constraintMap.entrySet() ) {
- BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl( entry.getKey(),
constraintHelper );
+ BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl(
+ entry.getKey(),
+ constraintHelper,
+ ignoreAnnotationDefaults,
+ ignoreAnnotationOnClass,
+ ignoreAnnotationOnMember
+ );
for ( MetaConstraint<?, ?> metaConstraint : entry.getValue() ) {
metaData.addMetaConstraint( metaConstraint );
}
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-07
20:49:32 UTC (rev 16272)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java 2009-04-07
21:37:10 UTC (rev 16273)
@@ -1,5 +1,8 @@
package org.hibernate.validation.engine.xml;
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
+
/**
* @author Hardy Ferentschik
*/
@@ -9,13 +12,12 @@
private String firstname;
- //@NotNull(groups = Default.class)
private String lastname;
//@Pattern(regexp = "[0-9 -]?", groups = Optional.class)
private String phoneNumber;
-
+ @NotNull(groups = Default.class)
public String getFirstname() {
return firstname;
}
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-07
20:49:32 UTC (rev 16272)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-07
21:37:10 UTC (rev 16273)
@@ -3,7 +3,7 @@
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<default-package>org.hibernate.validation.engine.xml</default-package>
<bean class="User" ignore-annotations="false">
- <class ignore-annotations="false">
+ <class ignore-annotations="true">
<group-sequence>
<value>value0</value>
</group-sequence>
@@ -39,7 +39,7 @@
</element-->
</constraint>
</field>
- <getter name="firstname">
+ <getter name="firstname" ignore-annotations="true">
<valid/>
<constraint annotation="javax.validation.constraints.Size">
<message>Size is limited!</message>