Author: hardy.ferentschik
Date: 2009-04-14 09:51:42 -0400 (Tue, 14 Apr 2009)
New Revision: 16313
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorDefinitionsCache.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataCache.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.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/ValidatorContextImpl.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/ValidatorImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java
Log:
HV-139
Made BeanMetaDataCache non static and merged ConstraintValidatorDefinitionsCache into
ConstraintHelper
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -17,9 +17,8 @@
*/
package org.hibernate.validation.engine;
-import java.lang.reflect.Field;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
-import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import javax.validation.BeanDescriptor;
@@ -56,7 +55,7 @@
* @return A list of <code>MetaConstraint</code> instances encapsulating the
information of all the constraints
* defined on the bean.
*/
- List<MetaConstraint<T, ?>> geMetaConstraintList();
+ List<MetaConstraint<T, ? extends Annotation>> geMetaConstraintList();
/**
* Return <code>PropertyDescriptor</code> for the given property.
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataCache.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataCache.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataCache.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -21,6 +21,8 @@
import java.util.concurrent.ConcurrentHashMap;
/**
+ * Cache for created instances of <code>BeanMetaData</code>.
+ *
* @author Hardy Ferentschik
*/
public class BeanMetaDataCache {
@@ -28,19 +30,19 @@
* A map for the meta data for each entity. The key is the class and the value the bean
meta data for this
* entity.
*/
- private static Map<Class<?>, BeanMetaDataImpl<?>> metadataProviders
- = new ConcurrentHashMap<Class<?>, BeanMetaDataImpl<?>>( 10 );
+ private Map<Class<?>, BeanMetaDataImpl<?>> metadataProviders = new
ConcurrentHashMap<Class<?>, BeanMetaDataImpl<?>>(
+ 10
+ );
- public static <T> BeanMetaDataImpl<T> getBeanMetaData(Class<T>
beanClass) {
+ @SuppressWarnings("unchecked")
+ public <T> BeanMetaDataImpl<T> getBeanMetaData(Class<T> beanClass) {
if ( beanClass == null ) {
throw new IllegalArgumentException( "Class cannot be null" );
}
- @SuppressWarnings("unchecked")
- BeanMetaDataImpl<T> metadata = ( BeanMetaDataImpl<T> )
metadataProviders.get( beanClass );
- return metadata;
+ return ( BeanMetaDataImpl<T> ) metadataProviders.get( beanClass );
}
- static void addBeanMetaData(Class<?> beanClass, BeanMetaDataImpl<?>
metaData) {
+ public <T> void addBeanMetaData(Class<T> beanClass,
BeanMetaDataImpl<T> metaData) {
metadataProviders.put( beanClass, metaData );
}
}
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-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -68,7 +68,7 @@
/**
* List of constraints.
*/
- private List<MetaConstraint<T, ?>> metaConstraintList = new
ArrayList<MetaConstraint<T, ?>>();
+ private List<MetaConstraint<T, ? extends Annotation>> metaConstraintList =
new ArrayList<MetaConstraint<T, ? extends Annotation>>();
/**
* List of cascaded members.
@@ -86,10 +86,11 @@
private List<Class<?>> defaultGroupSequence = new
ArrayList<Class<?>>();
/**
- * Object keeping track of all builtin constraints.
+ * Object keeping track of all constraints.
*/
private final ConstraintHelper constraintHelper;
+
public BeanMetaDataImpl(Class<T> beanClass, ConstraintHelper constraintHelper) {
this(
beanClass,
@@ -116,11 +117,11 @@
return cascadedMembers;
}
- public List<MetaConstraint<T, ?>> geMetaConstraintList() {
+ public List<MetaConstraint<T, ? extends Annotation>> geMetaConstraintList()
{
return metaConstraintList;
}
- public void addMetaConstraint(MetaConstraint<?, ?> metaConstraint) {
+ public void addMetaConstraint(MetaConstraint<?, ? extends Annotation>
metaConstraint) {
metaConstraintList.add( ( MetaConstraint<T, ?> ) metaConstraint );
}
@@ -323,10 +324,14 @@
Class<?>[] groups = ReflectionHelper.getAnnotationParameter( annotation,
"groups", Class[].class );
ConstraintDescriptorImpl constraintDescriptor;
if ( clazz.isInterface() ) {
- constraintDescriptor = new ConstraintDescriptorImpl( annotation, groups,
constraintHelper, clazz );
+ constraintDescriptor = new ConstraintDescriptorImpl(
+ annotation, groups, constraintHelper, clazz
+ );
}
else {
- constraintDescriptor = new ConstraintDescriptorImpl( annotation, groups,
constraintHelper );
+ constraintDescriptor = new ConstraintDescriptorImpl(
+ annotation, groups, constraintHelper
+ );
}
return constraintDescriptor;
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -67,10 +67,10 @@
private static final Logger log = LoggerFactory.make();
private static final String VALIDATION_XML_FILE = "/META-INF/validation.xml";
private static final String VALIDATION_CONFIGURATION_XSD =
"META-INF/validation-configuration-1.0.xsd";
- private static final MessageInterpolator defaultMessageInterpolator = new
ResourceBundleMessageInterpolator();
- private static final TraversableResolver defaultTraversableResolver = new
DefaultTraversableResolver();
- private static final ConstraintValidatorFactory defaultValidatorFactory = new
ConstraintValidatorFactoryImpl();
+ private final MessageInterpolator defaultMessageInterpolator = new
ResourceBundleMessageInterpolator();
+ private final TraversableResolver defaultTraversableResolver = new
DefaultTraversableResolver();
+ private final ConstraintValidatorFactory defaultValidatorFactory = new
ConstraintValidatorFactoryImpl();
private final ValidationProviderResolver providerResolver;
private ParameterHolder parameterHolder;
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -119,11 +119,9 @@
}
private void findConstraintValidatorClasses() {
- if ( ConstraintValidatorDefinitionsCache.containsConstraintValidatorDefinition(
annotation.annotationType())) {
- for ( Class<? extends ConstraintValidator<? extends Annotation, ?>>
validator : ConstraintValidatorDefinitionsCache
- .getConstraintValidatorDefinition(
- annotation.annotationType()
- ) ) {
+ if ( constraintHelper.containsConstraintValidatorDefinition(
annotation.annotationType() ) ) {
+ for ( Class<? extends ConstraintValidator<? extends Annotation, ?>>
validator : constraintHelper
+ .getConstraintValidatorDefinition( annotation.annotationType() ) ) {
constraintValidatorDefinitonClasses.add( ( Class<? extends
ConstraintValidator<T, ?>> ) validator );
}
return;
@@ -141,7 +139,7 @@
constraintDefinitonClasses.addAll( Arrays.asList( validatedBy ) );
}
- ConstraintValidatorDefinitionsCache.addConstraintValidatorDefinition(
+ constraintHelper.addConstraintValidatorDefinition(
annotation.annotationType(), constraintDefinitonClasses
);
@@ -304,7 +302,9 @@
}
}
U annotationProxy = AnnotationFactory.create( annotationDescriptor );
- return new ConstraintDescriptorImpl<U>( annotationProxy, groups, constraintHelper
);
+ return new ConstraintDescriptorImpl<U>(
+ annotationProxy, groups, constraintHelper
+ );
}
private class ClassIndexWrapper {
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-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -64,7 +64,7 @@
import org.hibernate.validation.util.ReflectionHelper;
/**
- * Keeps track of builtin constraints and their validator implementations.
+ * Keeps track of builtin constraints and their validator implementations, as well as
already resolved validator definitions.
*
* @author Hardy Ferentschik
* @author Alaa Nassef
@@ -76,6 +76,9 @@
private final Map<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<?, ?>>>> builtinConstraints =
new HashMap<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<?, ?>>>>();
+ private final Map<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>>
constraintValidatorDefinitons =
+ new HashMap<Class<? extends Annotation>, List<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>>();
+
public ConstraintHelper() {
List<Class<? extends ConstraintValidator<?, ?>>> constraintList =
@@ -152,7 +155,7 @@
return constraints;
}
- private List<Class<? extends ConstraintValidator<? , ?>>>
getBuiltInFromAnnotationType(Class<? extends Annotation> annotationType) {
+ private List<Class<? extends ConstraintValidator<?, ?>>>
getBuiltInFromAnnotationType(Class<? extends Annotation> annotationType) {
return builtinConstraints.get( annotationType );
}
@@ -234,7 +237,7 @@
}
/**
- * Checks whehter the specified annotation is a valid constraint annotation. A
constraint annotations has to
+ * Checks whether the specified annotation is a valid constraint annotation. A
constraint annotations has to
* fulfill the following conditions:
* <ul>
* <li>Has to contain a <code>ConstraintValidator</code>
implementation.</li>
@@ -284,4 +287,19 @@
}
return true;
}
+
+ public List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> getConstraintValidatorDefinition(Class<? extends Annotation>
annotationClass) {
+ if ( annotationClass == null ) {
+ throw new IllegalArgumentException( "Class cannot be null" );
+ }
+ return constraintValidatorDefinitons.get( annotationClass );
+ }
+
+ public <A extends Annotation> void addConstraintValidatorDefinition(Class<A>
annotationClass, List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> definitionClasses) {
+ constraintValidatorDefinitons.put( annotationClass, definitionClasses );
+ }
+
+ public boolean containsConstraintValidatorDefinition(Class<? extends Annotation>
annotationClass) {
+ return constraintValidatorDefinitons.containsKey( annotationClass );
+ }
}
Deleted:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorDefinitionsCache.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorDefinitionsCache.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorDefinitionsCache.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -1,48 +0,0 @@
-// $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;
-
-import java.lang.annotation.Annotation;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import javax.validation.ConstraintValidator;
-
-/**
- * Caches the constraint classes for a given annotation.
- *
- * @author Hardy Ferentschik
- */
-public class ConstraintValidatorDefinitionsCache {
- private static final Map<Class<? extends Annotation>, List<Class<?
extends ConstraintValidator<? extends Annotation, ?>>>>
constraintValidatorDefinitons = new HashMap<Class<? extends Annotation>,
List<Class<? extends ConstraintValidator<? extends Annotation,
?>>>>();
-
- public static List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> getConstraintValidatorDefinition(Class<? extends Annotation>
annotationClass) {
- if ( annotationClass == null ) {
- throw new IllegalArgumentException( "Class cannot be null" );
- }
- return constraintValidatorDefinitons.get( annotationClass );
- }
-
- public static <A extends Annotation> void
addConstraintValidatorDefinition(Class<A> annotationClass, List<Class<?
extends ConstraintValidator<? extends Annotation, ?>>> definitionClasses) {
- constraintValidatorDefinitons.put( annotationClass, definitionClasses );
- }
-
- public static boolean containsConstraintValidatorDefinition(Class<? extends
Annotation> annotationClass) {
- return constraintValidatorDefinitons.containsKey( annotationClass );
- }
-}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorContextImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorContextImpl.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorContextImpl.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -17,15 +17,18 @@
private final TraversableResolver factoryTraversableResolver;
private final ConstraintValidatorFactory constraintValidatorFactory;
private final ConstraintHelper constraintHelper;
+ private final BeanMetaDataCache beanMetaDataCache;
public ValidatorContextImpl(ConstraintValidatorFactory constraintValidatorFactory,
MessageInterpolator factoryMessageInterpolator,
TraversableResolver factoryTraversableResolver,
- ConstraintHelper constraintHelper) {
+ ConstraintHelper constraintHelper,
+ BeanMetaDataCache beanMetaDataCache) {
this.constraintValidatorFactory = constraintValidatorFactory;
this.factoryMessageInterpolator = factoryMessageInterpolator;
this.factoryTraversableResolver = factoryTraversableResolver;
this.constraintHelper = constraintHelper;
+ this.beanMetaDataCache = beanMetaDataCache;
messageInterpolator( factoryMessageInterpolator );
traversableResolver( factoryTraversableResolver );
}
@@ -61,7 +64,11 @@
*/
public Validator getValidator() {
return new ValidatorImpl(
- constraintValidatorFactory, messageInterpolator, traversableResolver,
constraintHelper
+ constraintValidatorFactory,
+ messageInterpolator,
+ traversableResolver,
+ constraintHelper,
+ beanMetaDataCache
);
}
}
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-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -71,6 +71,8 @@
import org.hibernate.validation.xml.ValidatedByType;
/**
+ * Factory returning initialized <code>Validator</code> instances.
+ *
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
@@ -85,24 +87,26 @@
private final MessageInterpolator messageInterpolator;
private final TraversableResolver traversableResolver;
private final ConstraintValidatorFactory constraintValidatorFactory;
- private final ConstraintHelper constraintHelper = new ConstraintHelper();
+ private final ConstraintHelper constraintHelper;
+ private final BeanMetaDataCache beanMetaDataCache;
private final Set<Class<?>> processedClasses = new
HashSet<Class<?>>();
+ private final AnnotationIgnores annotationIgnores;
+ private final Map<Class<?>, List<MetaConstraint<?, ? extends
Annotation>>> constraintMap;
+ private final Map<Class<?>, List<Member>> cascadedMembers;
+ private final Map<Class<?>, List<Class<?>>> defaultSequences;
- private AnnotationIgnores annotationIgnores;
- private Map<Class<?>, List<MetaConstraint<?, ?>>>
constraintMap;
- private Map<Class<?>, List<Member>> cascadedMembers;
- private Map<Class<?>, List<Class<?>>> defaultSequences;
-
public ValidatorFactoryImpl(ConfigurationState configurationState) {
this.messageInterpolator = configurationState.getMessageInterpolator();
this.constraintValidatorFactory = configurationState.getConstraintValidatorFactory();
this.traversableResolver = configurationState.getTraversableResolver();
+ this.constraintHelper = new ConstraintHelper();
+ this.beanMetaDataCache = new BeanMetaDataCache();
- annotationIgnores = new AnnotationIgnores();
- constraintMap = new HashMap<Class<?>, List<MetaConstraint<?,
?>>>();
- cascadedMembers = new HashMap<Class<?>, List<Member>>();
- defaultSequences = new HashMap<Class<?>, List<Class<?>>>();
+ this.annotationIgnores = new AnnotationIgnores();
+ this.constraintMap = new HashMap<Class<?>, List<MetaConstraint<?, ?
extends Annotation>>>();
+ this.cascadedMembers = new HashMap<Class<?>, List<Member>>();
+ this.defaultSequences = new HashMap<Class<?>,
List<Class<?>>>();
parseMappingFiles( configurationState.getMappingStreams() );
initBeanMetaData();
@@ -127,7 +131,11 @@
*/
public ValidatorContext usingContext() {
return new ValidatorContextImpl(
- constraintValidatorFactory, messageInterpolator, traversableResolver,
constraintHelper
+ constraintValidatorFactory,
+ messageInterpolator,
+ traversableResolver,
+ constraintHelper,
+ beanMetaDataCache
);
}
@@ -171,6 +179,7 @@
}
}
+ @SuppressWarnings("unchecked")
private void parseConstraintDefinitions(List<ConstraintDefinitionType>
constraintDefinitionList) {
for ( ConstraintDefinitionType constraintDefinition : constraintDefinitionList ) {
String annotationClassName = constraintDefinition.getAnnotation();
@@ -184,6 +193,10 @@
throw new ValidationException( "Unable to load class " +
annotationClassName );
}
+ if ( !annotationClass.isAnnotation() ) {
+ throw new ValidationException( annotationClassName + " is not an
annotation" );
+ }
+
ValidatedByType validatedByType = constraintDefinition.getValidatedBy();
List<Class<? extends ConstraintValidator<? extends Annotation, ?>>>
constraintValidatorClasses = new ArrayList<Class<? extends ConstraintValidator<?
extends Annotation, ?>>>();
if ( validatedByType.isIncludeExistingValidators() != null &&
validatedByType.isIncludeExistingValidators() ) {
@@ -192,7 +205,6 @@
for ( String validatorClassName : validatedByType.getValue() ) {
Class<? extends ConstraintValidator<?, ?>> validatorClass;
try {
- // TODO validate this class!
validatorClass = ( Class<? extends ConstraintValidator<?, ?>> )
ReflectionHelper.classForName(
validatorClassName,
this.getClass()
@@ -201,9 +213,14 @@
catch ( ClassNotFoundException e ) {
throw new ValidationException( "Unable to load class " +
validatorClassName );
}
+
+ if ( !ConstraintValidator.class.isAssignableFrom(validatorClass) ) {
+ throw new ValidationException( validatorClass + " is not a constraint validator
class" );
+ }
+
constraintValidatorClasses.add( validatorClass );
}
- ConstraintValidatorDefinitionsCache.addConstraintValidatorDefinition(
+ constraintHelper.addConstraintValidatorDefinition(
annotationClass, constraintValidatorClasses
);
}
@@ -311,7 +328,7 @@
constraintMap.get( beanClass ).add( metaConstraint );
}
else {
- List<MetaConstraint<?, ?>> constraintList = new
ArrayList<MetaConstraint<?, ?>>();
+ List<MetaConstraint<?, ? extends Annotation>> constraintList = new
ArrayList<MetaConstraint<?, ? extends Annotation>>();
constraintList.add( metaConstraint );
constraintMap.put( beanClass, constraintList );
}
@@ -585,10 +602,12 @@
return constraintMappings;
}
- private void initBeanMetaData() {
+ private <T> void initBeanMetaData() {
for ( Class<?> beanClass : processedClasses ) {
- BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl( beanClass,
constraintHelper, annotationIgnores );
- for ( MetaConstraint<?, ?> constraint : constraintMap.get( beanClass ) ) {
+ BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl<T>(
+ ( Class<T> ) beanClass, constraintHelper, annotationIgnores
+ );
+ for ( MetaConstraint<?, ? extends Annotation> constraint : constraintMap.get(
beanClass ) ) {
metaData.addMetaConstraint( constraint );
}
for ( Member m : cascadedMembers.get( beanClass ) ) {
@@ -597,7 +616,7 @@
if ( defaultSequences.containsKey( beanClass ) ) {
metaData.setDefaultGroupSequence( defaultSequences.get( beanClass ) );
}
- BeanMetaDataCache.addBeanMetaData( beanClass, metaData );
+ beanMetaDataCache.addBeanMetaData( ( Class<T> ) beanClass, (
BeanMetaDataImpl<T> ) metaData );
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -19,6 +19,7 @@
import java.lang.reflect.Member;
import java.lang.reflect.Type;
+import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -77,18 +78,18 @@
private GroupChainGenerator groupChainGenerator;
private final ConstraintValidatorFactory constraintValidatorFactory;
-
private final MessageInterpolator messageInterpolator;
-
private final TraversableResolver traversableResolver;
-
private final ConstraintHelper constraintHelper;
+ private final BeanMetaDataCache beanMetaDataCache;
- public ValidatorImpl(ConstraintValidatorFactory constraintValidatorFactory,
MessageInterpolator messageInterpolator, TraversableResolver traversableResolver,
ConstraintHelper constraintHelper) {
+
+ public ValidatorImpl(ConstraintValidatorFactory constraintValidatorFactory,
MessageInterpolator messageInterpolator, TraversableResolver traversableResolver,
ConstraintHelper constraintHelper, BeanMetaDataCache beanMetaDataCache) {
this.constraintValidatorFactory = constraintValidatorFactory;
this.messageInterpolator = messageInterpolator;
this.traversableResolver = traversableResolver;
this.constraintHelper = constraintHelper;
+ this.beanMetaDataCache = beanMetaDataCache;
groupChainGenerator = new GroupChainGenerator();
}
@@ -516,7 +517,7 @@
throw new IllegalArgumentException( "Invalid property path." );
}
- List<MetaConstraint<T, ?>> metaConstraintList = getBeanMetaData( clazz
).geMetaConstraintList();
+ List<MetaConstraint<T, ? extends Annotation>> metaConstraintList =
getBeanMetaData( clazz ).geMetaConstraintList();
for ( MetaConstraint<T, ?> metaConstraint : metaConstraintList ) {
if ( metaConstraint.getPropertyName().equals( propertyIter.getHead() ) ) {
metaConstraints.add( metaConstraint );
@@ -551,10 +552,10 @@
* {@inheritDoc}
*/
private <T> BeanMetaData<T> getBeanMetaData(Class<T> beanClass) {
- BeanMetaDataImpl<T> metadata = BeanMetaDataCache.getBeanMetaData( beanClass );
+ BeanMetaDataImpl<T> metadata = beanMetaDataCache.getBeanMetaData( beanClass );
if ( metadata == null ) {
- metadata = new BeanMetaDataImpl<T>( beanClass, constraintHelper );
- BeanMetaDataCache.addBeanMetaData( beanClass, metadata );
+ metadata = new BeanMetaDataImpl<T>( beanClass, constraintHelper);
+ beanMetaDataCache.addBeanMetaData( beanClass, metadata );
}
return metadata;
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java 2009-04-13
23:29:04 UTC (rev 16312)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java 2009-04-14
13:51:42 UTC (rev 16313)
@@ -45,26 +45,30 @@
private ResourceBundleMessageInterpolator interpolator;
private NotNull notNull;
+ private ConstraintDescriptorImpl<NotNull> notNullDescriptor;
private Size size;
+ private ConstraintDescriptorImpl<Size> sizeDescriptor;
@BeforeTest
public void setUp() {
// Create some annotations for testing using AnnotationProxies
AnnotationDescriptor<NotNull> descriptor = new
AnnotationDescriptor<NotNull>( NotNull.class );
notNull = AnnotationFactory.create( descriptor );
+ notNullDescriptor = new ConstraintDescriptorImpl<NotNull>(
+ notNull, new Class<?>[] { }, new ConstraintHelper()
+ );
- AnnotationDescriptor<Size> sizeDescriptor = new AnnotationDescriptor<Size>(
Size.class );
- size = AnnotationFactory.create( sizeDescriptor );
+ AnnotationDescriptor<Size> sizeAnnotationDescriptor = new
AnnotationDescriptor<Size>( Size.class );
+ size = AnnotationFactory.create( sizeAnnotationDescriptor );
+ sizeDescriptor = new ConstraintDescriptorImpl<Size>(
+ size, new Class<?>[] { }, new ConstraintHelper()
+ );
}
@Test
public void testSuccessfulInterpolation() {
- ConstraintDescriptorImpl<NotNull> descriptor = new
ConstraintDescriptorImpl<NotNull>(
- notNull, new Class<?>[] { }, new ConstraintHelper()
- );
-
interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
- MessageInterpolator.Context context = new MessageInterpolatorContext( descriptor, null
);
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
String expected = "replacement worked";
String actual = interpolator.interpolate( "{foo}", context );
assertEquals( actual, expected, "Wrong substitution" );
@@ -84,12 +88,8 @@
@Test
public void testUnSuccessfulInterpolation() {
- ConstraintDescriptorImpl<NotNull> descriptor = new
ConstraintDescriptorImpl<NotNull>(
- notNull, new Class<?>[] { }, new ConstraintHelper()
- );
-
interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
- MessageInterpolator.Context context = new MessageInterpolatorContext( descriptor, null
);
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
String expected = "foo"; // missing {}
String actual = interpolator.interpolate( "foo", context );
@@ -102,12 +102,8 @@
@Test
public void testUnkownTokenInterpolation() {
- ConstraintDescriptorImpl<NotNull> descriptor = new
ConstraintDescriptorImpl<NotNull>(
- notNull, new Class<?>[] { }, new ConstraintHelper()
- );
-
interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
- MessageInterpolator.Context context = new MessageInterpolatorContext( descriptor, null
);
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
String expected = "{bar}"; // unkown token {}
String actual = interpolator.interpolate( "{bar}", context );
@@ -116,20 +112,13 @@
@Test
public void testDefaultInterpolation() {
- ConstraintDescriptorImpl<NotNull> descriptor = new
ConstraintDescriptorImpl<NotNull>(
- notNull, new Class<?>[] { }, new ConstraintHelper()
- );
-
interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
- MessageInterpolator.Context context = new MessageInterpolatorContext( descriptor, null
);
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
String expected = "may not be null";
String actual = interpolator.interpolate( notNull.message(), context );
assertEquals( actual, expected, "Wrong substitution" );
- ConstraintDescriptorImpl<Size> sizeDescriptor = new
ConstraintDescriptorImpl<Size>(
- size, new Class<?>[] { }, new ConstraintHelper()
- );
expected = "size must be between 0 and 2147483647"; // unkown token {}
context = new MessageInterpolatorContext( sizeDescriptor, null );
actual = interpolator.interpolate( size.message(), context );
@@ -138,26 +127,18 @@
@Test
public void testMessageInterpolationWithLocale() {
- ConstraintDescriptorImpl<NotNull> descriptor = new
ConstraintDescriptorImpl<NotNull>(
- notNull, new Class<?>[] { }, new ConstraintHelper()
- );
-
interpolator = new ResourceBundleMessageInterpolator();
String expected = "kann nicht null sein";
- MessageInterpolator.Context context = new MessageInterpolatorContext( descriptor, null
);
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
String actual = interpolator.interpolate( notNull.message(), context, Locale.GERMAN );
assertEquals( actual, expected, "Wrong substitution" );
}
@Test
public void testFallbackToDefaultLocale() {
- ConstraintDescriptorImpl<NotNull> descriptor = new
ConstraintDescriptorImpl<NotNull>(
- notNull, new Class<?>[] { }, new ConstraintHelper()
- );
-
interpolator = new ResourceBundleMessageInterpolator();
- MessageInterpolator.Context context = new MessageInterpolatorContext( descriptor, null
);
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
String expected = "may not be null";
String actual = interpolator.interpolate( notNull.message(), context, Locale.JAPAN );
@@ -166,12 +147,8 @@
@Test
public void testUserResourceBundle() {
- ConstraintDescriptorImpl<NotNull> descriptor = new
ConstraintDescriptorImpl<NotNull>(
- notNull, new Class<?>[] { }, new ConstraintHelper()
- );
-
interpolator = new ResourceBundleMessageInterpolator();
- MessageInterpolator.Context context = new MessageInterpolatorContext( descriptor, null
);
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
String expected = "no puede ser null";
String actual = interpolator.interpolate( notNull.message(), context, new Locale(
"es", "ES" ) );