Author: hardy.ferentschik
Date: 2009-06-16 11:29:59 -0400 (Tue, 16 Jun 2009)
New Revision: 16797
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/ConstraintHelper.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/XmlMappingParser.java
Log:
HV-140 - removed some unchecked warnings
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-06-16
14:35:22 UTC (rev 16796)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-06-16
15:29:59 UTC (rev 16797)
@@ -30,12 +30,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
-import javax.validation.metadata.BeanDescriptor;
-import javax.validation.metadata.PropertyDescriptor;
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;
@@ -45,11 +45,10 @@
/**
- * This class encapsulates all meta data needed for validation. Implementations of
<code>Validator</code> interface can
+ * This class encapsulates all meta data needed for validation. Implementations of {@code
Validator} interface can
* instantiate an instance of this class and delegate the metadata extraction to it.
*
* @author Hardy Ferentschik
- * @todo check the unchecked assignment warnings in this class
*/
public class BeanMetaDataImpl<T> implements BeanMetaData<T> {
@@ -122,8 +121,8 @@
return Collections.unmodifiableList( metaConstraintList );
}
- public void addMetaConstraint(MetaConstraint<?, ? extends Annotation>
metaConstraint) {
- metaConstraintList.add( ( MetaConstraint<T, ?> ) metaConstraint );
+ public void addMetaConstraint(MetaConstraint<T, ? extends Annotation>
metaConstraint) {
+ metaConstraintList.add( metaConstraint );
}
public void addCascadedMember(Member member) {
@@ -223,17 +222,15 @@
setDefaultGroupSequence( groupSequence );
}
- private <A extends Annotation> void initFieldConstraints(Class clazz,
AnnotationIgnores annotationIgnores) {
+ private void initFieldConstraints(Class clazz, AnnotationIgnores annotationIgnores) {
for ( Field field : clazz.getDeclaredFields() ) {
- List<ConstraintDescriptorImpl> fieldMetadata = findConstraints( field );
- for ( ConstraintDescriptorImpl constraintDescription : fieldMetadata ) {
+ List<ConstraintDescriptorImpl<?>> fieldMetadata = findConstraints( field
);
+ for ( ConstraintDescriptorImpl<?> constraintDescription : fieldMetadata ) {
if ( annotationIgnores.isIgnoreAnnotations( field ) ) {
break;
}
ReflectionHelper.setAccessibility( field );
- MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>(
- field, beanClass, constraintDescription
- );
+ MetaConstraint<T, ?> metaConstraint = createMetaConstraint( field,
constraintDescription );
metaConstraintList.add( metaConstraint );
}
if ( field.isAnnotationPresent( Valid.class ) ) {
@@ -244,17 +241,15 @@
}
}
- private <A extends Annotation> void initMethodConstraints(Class clazz,
AnnotationIgnores annotationIgnores) {
+ private void initMethodConstraints(Class clazz, AnnotationIgnores annotationIgnores) {
for ( Method method : clazz.getDeclaredMethods() ) {
- List<ConstraintDescriptorImpl> methodMetadata = findConstraints( method );
- for ( ConstraintDescriptorImpl constraintDescription : methodMetadata ) {
+ List<ConstraintDescriptorImpl<?>> methodMetadata = findConstraints( method
);
+ for ( ConstraintDescriptorImpl<?> constraintDescription : methodMetadata ) {
if ( annotationIgnores.isIgnoreAnnotations( method ) ) {
break;
}
ReflectionHelper.setAccessibility( method );
- MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>(
- method, beanClass, constraintDescription
- );
+ MetaConstraint<T, ?> metaConstraint = createMetaConstraint( method,
constraintDescription );
metaConstraintList.add( metaConstraint );
}
if ( method.isAnnotationPresent( Valid.class ) ) {
@@ -281,17 +276,25 @@
return propertyDescriptor;
}
- private <A extends Annotation> void initClassConstraints(Class<?> clazz,
AnnotationIgnores annotationIgnores) {
+ private void initClassConstraints(Class<?> clazz, AnnotationIgnores
annotationIgnores) {
if ( annotationIgnores.isIgnoreAnnotations( clazz ) ) {
return;
}
- List<ConstraintDescriptorImpl> classMetadata = findClassLevelConstraints( clazz
);
- for ( ConstraintDescriptorImpl constraintDescription : classMetadata ) {
- MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>( beanClass,
constraintDescription );
+ List<ConstraintDescriptorImpl<?>> classMetadata =
findClassLevelConstraints( clazz );
+ for ( ConstraintDescriptorImpl<?> constraintDescription : classMetadata ) {
+ MetaConstraint<T, ?> metaConstraint = createMetaConstraint(
constraintDescription );
metaConstraintList.add( metaConstraint );
}
}
+ private <A extends Annotation> MetaConstraint<T, ?>
createMetaConstraint(ConstraintDescriptorImpl<A> descriptor) {
+ return new MetaConstraint<T, A>( beanClass, descriptor );
+ }
+
+ private <A extends Annotation> MetaConstraint<T, ?>
createMetaConstraint(Member m, ConstraintDescriptorImpl<A> descriptor) {
+ return new MetaConstraint<T, A>( m, beanClass, descriptor );
+ }
+
/**
* Examines the given annotation to see whether it is a single or multi valued
constraint annotation.
*
@@ -301,8 +304,8 @@
* @return A list of constraint descriptors or the empty list in case
<code>annotation</code> is neither a
* single nor multi value annotation.
*/
- private <A extends Annotation> List<ConstraintDescriptorImpl>
findConstraintAnnotations(Class<?> clazz, A annotation) {
- List<ConstraintDescriptorImpl> constraintDescriptors = new
ArrayList<ConstraintDescriptorImpl>();
+ private <A extends Annotation> List<ConstraintDescriptorImpl<?>>
findConstraintAnnotations(Class<?> clazz, A annotation) {
+ List<ConstraintDescriptorImpl<?>> constraintDescriptors = new
ArrayList<ConstraintDescriptorImpl<?>>();
List<Annotation> constraints = new ArrayList<Annotation>();
if ( constraintHelper.isConstraintAnnotation( annotation ) ||
@@ -322,7 +325,6 @@
@SuppressWarnings("unchecked")
private <A extends Annotation> ConstraintDescriptorImpl
buildConstraintDescriptor(Class<?> clazz, A annotation) {
- Class<?>[] groups = ReflectionHelper.getAnnotationParameter( annotation,
"groups", Class[].class );
ConstraintDescriptorImpl constraintDescriptor;
if ( clazz.isInterface() ) {
constraintDescriptor = new ConstraintDescriptorImpl( annotation, constraintHelper,
clazz );
@@ -341,8 +343,8 @@
*
* @return A list of constraint descriptors for all constraint specified on the given
class.
*/
- private List<ConstraintDescriptorImpl> findClassLevelConstraints(Class<?>
beanClass) {
- List<ConstraintDescriptorImpl> metadata = new
ArrayList<ConstraintDescriptorImpl>();
+ private List<ConstraintDescriptorImpl<?>>
findClassLevelConstraints(Class<?> beanClass) {
+ List<ConstraintDescriptorImpl<?>> metadata = new
ArrayList<ConstraintDescriptorImpl<?>>();
for ( Annotation annotation : beanClass.getAnnotations() ) {
metadata.addAll( findConstraintAnnotations( beanClass, annotation ) );
}
@@ -361,10 +363,10 @@
*
* @return A list of constraint descriptors for all constraint specified for the given
field or method.
*/
- private List<ConstraintDescriptorImpl> findConstraints(Member member) {
+ private List<ConstraintDescriptorImpl<?>> findConstraints(Member member) {
assert member instanceof Field || member instanceof Method;
- List<ConstraintDescriptorImpl> metadata = new
ArrayList<ConstraintDescriptorImpl>();
+ List<ConstraintDescriptorImpl<?>> metadata = new
ArrayList<ConstraintDescriptorImpl<?>>();
for ( Annotation annotation : ( ( AnnotatedElement ) member ).getAnnotations() ) {
metadata.addAll( findConstraintAnnotations( member.getDeclaringClass(), annotation )
);
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-16
14:35:22 UTC (rev 16796)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-16
15:29:59 UTC (rev 16797)
@@ -21,7 +21,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -31,6 +30,8 @@
import javax.validation.ValidationException;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
+import javax.validation.constraints.DecimalMax;
+import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
@@ -40,11 +41,13 @@
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
-import javax.validation.constraints.DecimalMax;
-import javax.validation.constraints.DecimalMin;
import org.hibernate.validation.constraints.impl.AssertFalseValidator;
import org.hibernate.validation.constraints.impl.AssertTrueValidator;
+import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForNumber;
+import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForString;
+import org.hibernate.validation.constraints.impl.DecimalMinValidatorForNumber;
+import org.hibernate.validation.constraints.impl.DecimalMinValidatorForString;
import org.hibernate.validation.constraints.impl.DigitsValidatorForNumber;
import org.hibernate.validation.constraints.impl.DigitsValidatorForString;
import org.hibernate.validation.constraints.impl.FutureValidatorForCalendar;
@@ -69,10 +72,6 @@
import org.hibernate.validation.constraints.impl.SizeValidatorForCollection;
import org.hibernate.validation.constraints.impl.SizeValidatorForMap;
import org.hibernate.validation.constraints.impl.SizeValidatorForString;
-import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForNumber;
-import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForString;
-import org.hibernate.validation.constraints.impl.DecimalMinValidatorForNumber;
-import org.hibernate.validation.constraints.impl.DecimalMinValidatorForString;
import org.hibernate.validation.util.ReflectionHelper;
/**
@@ -83,14 +82,10 @@
*/
public class ConstraintHelper {
- private final Map<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<?, ?>>>> builtinConstraints =
+ private final static Map<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 =
- new ConcurrentHashMap<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>>();
-
- public ConstraintHelper() {
-
+ static {
List<Class<? extends ConstraintValidator<?, ?>>> constraintList =
new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
constraintList.add( AssertFalseValidator.class );
@@ -162,6 +157,9 @@
builtinConstraints.put( Size.class, constraintList );
}
+ private final Map<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>>
constraintValidatorDefinitons =
+ new ConcurrentHashMap<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>>();
+
public List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> getBuiltInConstraints(Class<? extends Annotation> annotationType) {
final List<Class<? extends ConstraintValidator<?, ?>>> builtInList =
getBuiltInFromAnnotationType(
annotationType
@@ -276,7 +274,7 @@
*
* @return <code>true</code> if the annotation fulfills the above condtions,
<code>false</code> otherwise.
*/
- public static boolean isConstraintAnnotation(Annotation annotation) {
+ public boolean isConstraintAnnotation(Annotation annotation) {
Constraint constraint = annotation.annotationType()
.getAnnotation( Constraint.class );
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-06-16
14:35:22 UTC (rev 16796)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-06-16
15:29:59 UTC (rev 16797)
@@ -57,16 +57,10 @@
initBeanMetaData( configurationState.getMappingStreams() );
}
- /**
- * {@inheritDoc}
- */
public Validator getValidator() {
return usingContext().getValidator();
}
- /**
- * {@inheritDoc}
- */
public MessageInterpolator getMessageInterpolator() {
return messageInterpolator;
}
@@ -75,9 +69,6 @@
throw new ValidationException( "Type " + type + " not supported");
}
- /**
- * {@inheritDoc}
- */
public ValidatorContext usingContext() {
return new ValidatorContextImpl(
constraintValidatorFactory,
@@ -95,11 +86,11 @@
AnnotationIgnores annotationIgnores = mappingParser.getAnnotationIgnores();
for ( Class<?> beanClass : mappingParser.getProcessedClasses() ) {
- BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl<T>(
+ BeanMetaDataImpl<T> metaData = new BeanMetaDataImpl<T>(
( Class<T> ) beanClass, constraintHelper, annotationIgnores
);
- for ( MetaConstraint<?, ? extends Annotation> constraint :
mappingParser.getConstraintsForClass( beanClass ) ) {
+ for ( MetaConstraint<T, ? extends Annotation> constraint :
mappingParser.getConstraintsForClass( ( Class<T> ) beanClass ) ) {
metaData.addMetaConstraint( constraint );
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/XmlMappingParser.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/XmlMappingParser.java 2009-06-16
14:35:22 UTC (rev 16796)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/XmlMappingParser.java 2009-06-16
15:29:59 UTC (rev 16797)
@@ -128,9 +128,15 @@
return annotationIgnores;
}
- public List<MetaConstraint<?, ? extends Annotation>>
getConstraintsForClass(Class<?> beanClass) {
+ public <T> List<MetaConstraint<T, ? extends Annotation>>
getConstraintsForClass(Class<T> beanClass) {
+ List<MetaConstraint<T, ? extends Annotation>> list = new
ArrayList<MetaConstraint<T, ? extends Annotation>>();
if ( constraintMap.containsKey( beanClass ) ) {
- return constraintMap.get( beanClass );
+ for ( MetaConstraint<?, ? extends Annotation> metaConstraint :
constraintMap.get( beanClass ) ) {
+ @SuppressWarnings( "unchecked") // safe cast since the list of meta
constraints is always specific to the bean type
+ MetaConstraint<T, ? extends Annotation> boundMetaConstraint = (
MetaConstraint<T, ? extends Annotation> ) metaConstraint;
+ list.add( boundMetaConstraint );
+ }
+ return list;
}
else {
return Collections.emptyList();