Author: hardy.ferentschik
Date: 2009-06-17 11:17:31 -0400 (Wed, 17 Jun 2009)
New Revision: 16822
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintDescriptorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintHelper.java
Log:
HV-169 HV-140
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintDescriptorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintDescriptorImpl.java 2009-06-17
15:12:54 UTC (rev 16821)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintDescriptorImpl.java 2009-06-17
15:17:31 UTC (rev 16822)
@@ -122,20 +122,20 @@
}
private void findConstraintValidatorClasses() {
- if ( constraintHelper.containsConstraintValidatorDefinition(
annotation.annotationType() ) ) {
- for ( Class<? extends ConstraintValidator<? extends Annotation, ?>>
validator : constraintHelper
- .getConstraintValidatorDefinition( annotation.annotationType() ) ) {
- constraintValidatorDefinitonClasses.add( ( Class<? extends
ConstraintValidator<T, ?>> ) validator );
+ final Class<T> annotationType = getAnnotationType();
+ if ( constraintHelper.containsConstraintValidatorDefinition( annotationType ) ) {
+ for ( Class<? extends ConstraintValidator<T, ?>> validator :
constraintHelper
+ .getConstraintValidatorDefinition( annotationType ) ) {
+ constraintValidatorDefinitonClasses.add( validator );
}
return;
}
List<Class<? extends ConstraintValidator<? extends Annotation, ?>>>
constraintDefinitonClasses = new ArrayList<Class<? extends ConstraintValidator<?
extends Annotation, ?>>>();
if ( constraintHelper.isBuiltinConstraint( annotation.annotationType() ) ) {
- constraintDefinitonClasses.addAll( constraintHelper.getBuiltInConstraints(
annotation.annotationType() ) );
+ constraintDefinitonClasses.addAll( constraintHelper.getBuiltInConstraints(
annotationType ) );
}
else {
- final Class<? extends Annotation> annotationType = annotation.annotationType();
Class<? extends ConstraintValidator<?, ?>>[] validatedBy = annotationType
.getAnnotation( Constraint.class )
.validatedBy();
@@ -153,6 +153,11 @@
}
}
+ @SuppressWarnings("unchecked")
+ private Class<T> getAnnotationType() {
+ return ( Class<T> ) annotation.annotationType();
+ }
+
public T getAnnotation() {
return annotation;
}
@@ -354,6 +359,7 @@
}
@Override
+ @SuppressWarnings("SimplifiableIfStatement")
public boolean equals(Object o) {
if ( this == o ) {
return true;
@@ -362,7 +368,8 @@
return false;
}
- ClassIndexWrapper that = ( ClassIndexWrapper ) o;
+ @SuppressWarnings("unchecked") // safe due to the check above
+ ClassIndexWrapper that = ( ClassIndexWrapper ) o;
if ( index != that.index ) {
return false;
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintHelper.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintHelper.java 2009-06-17
15:12:54 UTC (rev 16821)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ConstraintHelper.java 2009-06-17
15:17:31 UTC (rev 16822)
@@ -22,7 +22,6 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.Constraint;
import javax.validation.ConstraintDefinitionException;
@@ -82,10 +81,10 @@
*/
public class ConstraintHelper {
- private final Map<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<?, ?>>>> builtinConstraints =
+ private final ConcurrentHashMap<Class<? extends Annotation>, List<Class<?
extends ConstraintValidator<?, ?>>>> builtinConstraints =
new ConcurrentHashMap<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<?, ?>>>>();
- private final Map<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>>
constraintValidatorDefinitons =
+ private final ConcurrentHashMap<Class<? extends Annotation>, List<Class<?
extends ConstraintValidator<? extends Annotation, ?>>>>
constraintValidatorDefinitons =
new ConcurrentHashMap<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>>();
public ConstraintHelper() {
@@ -160,14 +159,13 @@
builtinConstraints.put( Size.class, constraintList );
}
- public List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> getBuiltInConstraints(Class<? extends Annotation> annotationType) {
- final List<Class<? extends ConstraintValidator<?, ?>>> builtInList =
getBuiltInFromAnnotationType(
- annotationType
- );
+ public List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> getBuiltInConstraints(Class<? extends Annotation> annotationClass) {
+ final List<Class<? extends ConstraintValidator<?, ?>>> builtInList =
builtinConstraints.get( annotationClass );
if ( builtInList == null || builtInList.size() == 0 ) {
- throw new ValidationException( "Unable to find constraints for " +
annotationType );
+ throw new ValidationException( "Unable to find constraints for " +
annotationClass );
}
+
List<Class<? extends ConstraintValidator<? extends Annotation, ?>>>
constraints =
new ArrayList<Class<? extends ConstraintValidator<? extends Annotation,
?>>>( builtInList.size() );
for ( Class<? extends ConstraintValidator<?, ?>> validatorClass :
builtInList ) {
@@ -180,10 +178,6 @@
return constraints;
}
- private List<Class<? extends ConstraintValidator<?, ?>>>
getBuiltInFromAnnotationType(Class<? extends Annotation> annotationType) {
- return builtinConstraints.get( annotationType );
- }
-
public boolean isBuiltinConstraint(Class<? extends Annotation> annotationType) {
return builtinConstraints.containsKey( annotationType );
}
@@ -318,15 +312,27 @@
return true;
}
- public List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> getConstraintValidatorDefinition(Class<? extends Annotation>
annotationClass) {
+ public <T extends Annotation> List<Class<? extends ConstraintValidator<T,
?>>> getConstraintValidatorDefinition(Class<T> annotationClass) {
if ( annotationClass == null ) {
throw new IllegalArgumentException( "Class cannot be null" );
}
- return constraintValidatorDefinitons.get( annotationClass );
+
+ final List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> list = constraintValidatorDefinitons.get( annotationClass );
+
+ List<Class<? extends ConstraintValidator<T, ?>>>
constraintsValidators =
+ new ArrayList<Class<? extends ConstraintValidator<T, ?>>>(
list.size() );
+ for ( Class<? extends ConstraintValidator<?, ?>> validatorClass : list ) {
+ //safe cause all CV for a given annotation A are CV<A, ?>
+ @SuppressWarnings("unchecked")
+ Class<ConstraintValidator<T, ?>> safeValdiatorClass = (
Class<ConstraintValidator<T, ?>> ) validatorClass;
+ constraintsValidators.add( safeValdiatorClass );
+ }
+
+ return constraintsValidators;
}
public <A extends Annotation> void addConstraintValidatorDefinition(Class<A>
annotationClass, List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> definitionClasses) {
- constraintValidatorDefinitons.put( annotationClass, definitionClasses );
+ constraintValidatorDefinitons.putIfAbsent( annotationClass, definitionClasses );
}
public boolean containsConstraintValidatorDefinition(Class<? extends Annotation>
annotationClass) {