Skip to content

Commit

Permalink
HV-481 Using a message bundle (@MessageBundle) for i18n messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Mar 2, 2012
1 parent 219a0b4 commit 5f0fe31
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 138 deletions.
10 changes: 0 additions & 10 deletions hibernate-validator/pom.xml
Expand Up @@ -110,16 +110,6 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
Expand Down
Expand Up @@ -20,8 +20,8 @@
import org.hibernate.validator.internal.cfg.context.ConstraintMappingContext;
import org.hibernate.validator.internal.cfg.context.TypeConstraintMappingContextImpl;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* Top level class for constraints configured via the programmatic API.
Expand All @@ -31,9 +31,6 @@
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
*/
public class ConstraintMapping {

private static final Log log = LoggerFactory.make();

protected ConstraintMappingContext context;

public ConstraintMapping() {
Expand All @@ -55,7 +52,7 @@ protected ConstraintMapping(ConstraintMapping original) {
*/
public final <C> TypeConstraintMappingContext<C> type(Class<C> beanClass) {

Contracts.assertNotNull( beanClass, log.beanTypeMustNotBeNull() );
Contracts.assertNotNull( beanClass, MESSAGES.beanTypeMustNotBeNull() );

return new TypeConstraintMappingContextImpl<C>( beanClass, context );
}
Expand Down
Expand Up @@ -28,6 +28,8 @@
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* Base class for implementations of constraint mapping creational context types.
*
Expand All @@ -48,7 +50,7 @@ public ConstraintMappingContextImplBase(Class<?> beanClass, ConstraintMappingCon

public <C> TypeConstraintMappingContext<C> type(Class<C> type) {

Contracts.assertNotNull( beanClass, log.beanTypeMustNotBeNull() );
Contracts.assertNotNull( beanClass, MESSAGES.beanTypeMustNotBeNull() );

return new TypeConstraintMappingContextImpl<C>( type, mapping );
}
Expand All @@ -57,7 +59,7 @@ public PropertyConstraintMappingContext property(String property, ElementType el

Contracts.assertNotNull( property, "The property name must not be null." );
Contracts.assertNotNull( elementType, "The element type must not be null." );
Contracts.assertNotEmpty( property, log.propertyNameMustNotBeEmpty() );
Contracts.assertNotEmpty( property, MESSAGES.propertyNameMustNotBeEmpty() );

Member member = ReflectionHelper.getMember(
beanClass, property, elementType
Expand All @@ -72,14 +74,14 @@ public PropertyConstraintMappingContext property(String property, ElementType el

public MethodConstraintMappingContext method(String name, Class<?>... parameterTypes) {

Contracts.assertNotNull( name, log.methodNameMustNotBeNull() );
Contracts.assertNotNull( name, MESSAGES.methodNameMustNotBeNull() );

Method method = ReflectionHelper.getDeclaredMethod( beanClass, name, parameterTypes );

if ( method == null ) {
StringBuilder sb = new StringBuilder();
for ( Class<?> oneParameterType : parameterTypes ) {
sb.append( oneParameterType.getName() + ", " );
sb.append( oneParameterType.getName() ).append( ", " );
}

String parameterTypesAsString = sb.length() > 2 ? sb.substring( 0, sb.length() - 2 ) : sb.toString();
Expand Down
Expand Up @@ -28,6 +28,8 @@
import org.hibernate.validator.internal.util.scriptengine.ScriptEvaluator;
import org.hibernate.validator.internal.util.scriptengine.ScriptEvaluatorFactory;

import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* Validator for the {@link ScriptAssert} constraint annotation.
*
Expand All @@ -36,9 +38,9 @@
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
*/
public class ScriptAssertValidator implements ConstraintValidator<ScriptAssert, Object> {

private static final Log log = LoggerFactory.make();

private String script;
private String languageName;
private String alias;
Expand Down Expand Up @@ -86,8 +88,8 @@ public boolean isValid(Object value, ConstraintValidatorContext constraintValida
}

private void validateParameters(ScriptAssert constraintAnnotation) {
Contracts.assertNotEmpty( constraintAnnotation.script(), log.parameterMustNotBeEmpty( "script" ) );
Contracts.assertNotEmpty( constraintAnnotation.lang(), log.parameterMustNotBeEmpty( "lang" ) );
Contracts.assertNotEmpty( constraintAnnotation.alias(), log.parameterMustNotBeEmpty( "alias" ) );
Contracts.assertNotEmpty( constraintAnnotation.script(), MESSAGES.parameterMustNotBeEmpty( "script" ) );
Contracts.assertNotEmpty( constraintAnnotation.lang(), MESSAGES.parameterMustNotBeEmpty( "lang" ) );
Contracts.assertNotEmpty( constraintAnnotation.alias(), MESSAGES.parameterMustNotBeEmpty( "alias" ) );
}
}
Expand Up @@ -44,6 +44,8 @@
import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator;
import org.hibernate.validator.resourceloading.ResourceBundleLocator;

import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* Hibernate specific {@code Configuration} implementation.
*
Expand Down Expand Up @@ -134,8 +136,8 @@ public final ConfigurationImpl constraintValidatorFactory(ConstraintValidatorFac
}

public final HibernateValidatorConfiguration addMapping(InputStream stream) {
Contracts.assertNotNull( stream, log.parameterMustNotBeNull( "stream" ) );

Contracts.assertNotNull( stream, MESSAGES.parameterMustNotBeNull( "stream" ) );

validationBootstrapParameters.addMapping( stream );
return this;
Expand All @@ -147,9 +149,9 @@ public final HibernateValidatorConfiguration failFast(boolean failFast) {
}

public final HibernateValidatorConfiguration addMapping(ConstraintMapping mapping) {
Contracts.assertNotNull( mapping, log.parameterMustNotBeNull( "mapping" ) );
Contracts.assertNotNull( mapping, MESSAGES.parameterMustNotBeNull( "mapping" ) );

this.programmaticMappings.add(mapping);
this.programmaticMappings.add( mapping );
return this;
}

Expand Down
Expand Up @@ -30,6 +30,8 @@
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* @author Hardy Ferentschik
* @author Gunnar Morling
Expand Down Expand Up @@ -74,8 +76,8 @@ public final class PathImpl implements Path, Serializable {
* {@code property} cannot be parsed.
*/
public static PathImpl createPathFromString(String propertyPath) {
Contracts.assertNotNull( propertyPath, log.propertyPathCannotBeNull() );

Contracts.assertNotNull( propertyPath, MESSAGES.propertyPathCannotBeNull() );

if ( propertyPath.length() == 0 ) {
return createNewPath( null );
Expand Down
Expand Up @@ -56,6 +56,7 @@

import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* The main Bean Validation class. This is the core processing class of Hibernate Validator.
Expand Down Expand Up @@ -118,7 +119,7 @@ public ValidatorImpl(ConstraintValidatorFactory constraintValidatorFactory, Mess

public final <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups) {

Contracts.assertNotNull( object, log.validatedObjectMustNotBeNull() );
Contracts.assertNotNull( object, MESSAGES.validatedObjectMustNotBeNull() );

GroupChain groupChain = determineGroupExecutionOrder( groups );

Expand All @@ -133,7 +134,7 @@ object, messageInterpolator, constraintValidatorFactory, getCachingTraversableRe

public final <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups) {

Contracts.assertNotNull( object, log.validatedObjectMustNotBeNull() );
Contracts.assertNotNull( object, MESSAGES.validatedObjectMustNotBeNull() );

sanityCheckPropertyPath( propertyName );
GroupChain groupChain = determineGroupExecutionOrder( groups );
Expand All @@ -151,7 +152,7 @@ public final <T> Set<ConstraintViolation<T>> validateProperty(T object, String p

public final <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) {

Contracts.assertNotNull( beanType, log.beanTypeCannotBeNull() );
Contracts.assertNotNull( beanType, MESSAGES.beanTypeCannotBeNull() );

sanityCheckPropertyPath( propertyName );
GroupChain groupChain = determineGroupExecutionOrder( groups );
Expand All @@ -169,8 +170,8 @@ public final <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, St

public final <T> Set<MethodConstraintViolation<T>> validateParameter(T object, Method method, Object parameterValue, int parameterIndex, Class<?>... groups) {

Contracts.assertNotNull( object, log.validatedObjectMustNotBeNull() );
Contracts.assertNotNull( method, log.validatedMethodMustNotBeNull() );
Contracts.assertNotNull( object, MESSAGES.validatedObjectMustNotBeNull() );
Contracts.assertNotNull( method, MESSAGES.validatedMethodMustNotBeNull() );

GroupChain groupChain = determineGroupExecutionOrder( groups );

Expand All @@ -194,8 +195,8 @@ public final <T> Set<MethodConstraintViolation<T>> validateParameter(T object, M

public final <T> Set<MethodConstraintViolation<T>> validateAllParameters(T object, Method method, Object[] parameterValues, Class<?>... groups) {

Contracts.assertNotNull( object, log.validatedObjectMustNotBeNull() );
Contracts.assertNotNull( method, log.validatedMethodMustNotBeNull() );
Contracts.assertNotNull( object, MESSAGES.validatedObjectMustNotBeNull() );
Contracts.assertNotNull( method, MESSAGES.validatedMethodMustNotBeNull() );

//this might be the case for parameterless methods
if ( parameterValues == null ) {
Expand All @@ -220,7 +221,7 @@ public final <T> Set<MethodConstraintViolation<T>> validateAllParameters(T objec

public <T> Set<MethodConstraintViolation<T>> validateReturnValue(T object, Method method, Object returnValue, Class<?>... groups) {

Contracts.assertNotNull( method, log.validatedMethodMustNotBeNull() );
Contracts.assertNotNull( method, MESSAGES.validatedMethodMustNotBeNull() );

GroupChain groupChain = determineGroupExecutionOrder( groups );

Expand Down Expand Up @@ -261,7 +262,7 @@ private void sanityCheckPropertyPath(String propertyName) {

private GroupChain determineGroupExecutionOrder(Class<?>[] groups) {

Contracts.assertNotNull( groups, log.groupMustNotBeNull() );
Contracts.assertNotNull( groups, MESSAGES.groupMustNotBeNull() );

Class<?>[] tmpGroups = groups;
// if no groups is specified use the default
Expand Down
Expand Up @@ -21,8 +21,8 @@
import javax.validation.groups.Default;

import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* An instance of this class is used to collect all the relevant information for validating a single class, property or
Expand All @@ -32,9 +32,6 @@
* @author Gunnar Morling
*/
public class ValueContext<T, V> {

private static final Log log = LoggerFactory.make();

/**
* The current bean which gets validated. This is the bean hosting the constraints which get validated.
*/
Expand Down Expand Up @@ -168,8 +165,8 @@ public final void setPropertyPath(PathImpl propertyPath) {
*/
public final void appendNode(String node) {

Contracts.assertNotNull( node, log.mustNotBeNull( "node" ) );
Contracts.assertNotNull( node, MESSAGES.mustNotBeNull( "node" ) );

propertyPath = PathImpl.createCopy( propertyPath );
propertyPath.addNode( node );
}
Expand Down
Expand Up @@ -21,10 +21,9 @@

import org.hibernate.validator.internal.metadata.aggregated.BeanMetaData;
import org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* Cache for created instances of {@code BeanMetaData}.
Expand All @@ -33,9 +32,6 @@
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
*/
public class BeanMetaDataCache {

private static final Log log = LoggerFactory.make();

/**
* A map for the meta data for each entity. The key is the class and the value the bean meta data for this
* entity.
Expand All @@ -46,15 +42,15 @@ public class BeanMetaDataCache {

@SuppressWarnings("unchecked")
public <T> BeanMetaData<T> getBeanMetaData(Class<T> beanClass) {
assertNotNull( beanClass, log.mustNotBeNull( "Class" ) );
assertNotNull( beanClass, MESSAGES.mustNotBeNull( "Class" ) );

return (BeanMetaDataImpl<T>) metadataProviders.get( beanClass );
}

@SuppressWarnings("unchecked")
public <T> BeanMetaData<T> addBeanMetaData(Class<T> beanClass, BeanMetaData<T> metaData) {
assertNotNull( beanClass, log.mustNotBeNull( "Class" ) );
assertNotNull( metaData, log.mustNotBeNull( "MetaData" ) );
assertNotNull( beanClass, MESSAGES.mustNotBeNull( "Class" ) );
assertNotNull( metaData, MESSAGES.mustNotBeNull( "MetaData" ) );

return (BeanMetaData<T>) metadataProviders.putIfAbsent( beanClass, metaData );
}
Expand Down
Expand Up @@ -93,6 +93,7 @@
import org.hibernate.validator.internal.util.logging.LoggerFactory;

import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* Keeps track of builtin constraints and their validator implementations, as well as already resolved validator definitions.
Expand Down Expand Up @@ -386,7 +387,7 @@ private void assertMessageParameterExists(Class<? extends Annotation> annotation
public <T extends Annotation> List<Class<? extends ConstraintValidator<T, ?>>> getConstraintValidatorDefinition
(Class<T> annotationClass) {

Contracts.assertNotNull( annotationClass, log.classCannotBeNull() );
Contracts.assertNotNull( annotationClass, MESSAGES.classCannotBeNull() );

final List<Class<? extends ConstraintValidator<? extends Annotation, ?>>> list = constraintValidatorDefinitions.get(
annotationClass
Expand Down
Expand Up @@ -27,14 +27,13 @@
import javax.validation.metadata.PropertyDescriptor;

import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.method.metadata.MethodDescriptor;
import org.hibernate.validator.method.metadata.ParameterDescriptor;
import org.hibernate.validator.method.metadata.TypeDescriptor;

import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* Describes a validated bean.
Expand All @@ -44,9 +43,6 @@
* @author Gunnar Morling
*/
public class BeanDescriptorImpl<T> extends ElementDescriptorImpl implements BeanDescriptor, TypeDescriptor {

private static final Log log = LoggerFactory.make();

private final Map<String, PropertyDescriptor> constrainedProperties;
private final Map<String, MethodDescriptor> methods;
private final Set<MethodDescriptor> constrainedMethods;
Expand Down Expand Up @@ -90,7 +86,7 @@ public Set<MethodDescriptor> getConstrainedMethods() {
//a descriptor if the given method is constrained.
public MethodDescriptor getConstraintsForMethod(String methodName, Class<?>... parameterTypes) {

Contracts.assertNotNull( methodName, log.methodNameMustNotBeNull() );
Contracts.assertNotNull( methodName, MESSAGES.methodNameMustNotBeNull() );

return methods.get( methodName + Arrays.toString( parameterTypes ) );
}
Expand Down

0 comments on commit 5f0fe31

Please sign in to comment.