Skip to content

Commit

Permalink
HV-481 Use JBoss logging logger as an exception factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpollet authored and hferentschik committed Mar 2, 2012
1 parent 0451243 commit 7f645c3
Show file tree
Hide file tree
Showing 54 changed files with 846 additions and 381 deletions.
Expand Up @@ -19,6 +19,9 @@
import org.hibernate.validator.cfg.context.TypeConstraintMappingContext;
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;

/**
* Top level class for constraints configured via the programmatic API.
Expand All @@ -29,6 +32,8 @@
*/
public class ConstraintMapping {

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

protected ConstraintMappingContext context;

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

if ( beanClass == null ) {
throw new IllegalArgumentException( "The bean type must not be null when creating a constraint mapping." );
}
Contracts.assertNotNull( beanClass, log.beanTypeMustNotBeNull() );

return new TypeConstraintMappingContextImpl<C>( beanClass, context );
}
Expand Down
Expand Up @@ -38,5 +38,4 @@ public GenericConstraintDef<A> param(String key, Object value) {
addParameter( key, value );
return this;
}

}
Expand Up @@ -20,14 +20,15 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Map;
import javax.validation.ValidationException;

import org.hibernate.validator.cfg.ConstraintDef;
import org.hibernate.validator.internal.metadata.location.BeanConstraintLocation;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.metadata.location.MethodConstraintLocation;
import org.hibernate.validator.internal.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotationfactory.AnnotationFactory;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

/**
* Represents a programmatically configured constraint and meta-data
Expand All @@ -37,6 +38,8 @@
*/
public class ConfiguredConstraint<A extends Annotation, L extends ConstraintLocation> {

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

private final ConstraintDefAccessor<A> constraint;
private final L location;

Expand Down Expand Up @@ -98,9 +101,7 @@ public A createAnnotationProxy() {
return AnnotationFactory.create( annotationDescriptor );
}
catch ( RuntimeException e ) {
throw new ValidationException(
"Unable to create annotation for configured constraint: " + e.getMessage(), e
);
throw log.unableToCreateAnnotationForConfiguredConstraint( e.getMessage(), e );
}
}

Expand Down
Expand Up @@ -19,13 +19,14 @@
import java.lang.annotation.ElementType;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import javax.validation.ValidationException;

import org.hibernate.validator.cfg.context.MethodConstraintMappingContext;
import org.hibernate.validator.cfg.context.PropertyConstraintMappingContext;
import org.hibernate.validator.cfg.context.TypeConstraintMappingContext;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.ReflectionHelper;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

/**
* Base class for implementations of constraint mapping creational context types.
Expand All @@ -34,8 +35,9 @@
*/
public abstract class ConstraintMappingContextImplBase {

protected final Class<?> beanClass;
private static final Log log = LoggerFactory.make();

protected final Class<?> beanClass;
protected final ConstraintMappingContext mapping;

public ConstraintMappingContextImplBase(Class<?> beanClass, ConstraintMappingContext mapping) {
Expand All @@ -46,7 +48,7 @@ public ConstraintMappingContextImplBase(Class<?> beanClass, ConstraintMappingCon

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

Contracts.assertNotNull( beanClass, "The bean type must not be null when creating a constraint mapping." );
Contracts.assertNotNull( beanClass, log.beanTypeMustNotBeNull() );

return new TypeConstraintMappingContextImpl<C>( type, mapping );
}
Expand All @@ -55,28 +57,22 @@ 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." );

if ( property.length() == 0 ) {
throw new IllegalArgumentException( "The property name must not be empty." );
}
Contracts.assertNotEmpty( property, log.propertyNameMustNotBeEmpty() );

Member member = ReflectionHelper.getMember(
beanClass, property, elementType
);

if ( member == null ) {
throw new ValidationException(
"The class " + beanClass + " does not have a property '"
+ property + "' with access " + elementType
);
throw log.unableToFindPropertyWithAccess( beanClass, property, elementType );
}

return new PropertyConstraintMappingContextImpl( beanClass, member, mapping );
}

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

Contracts.assertNotNull( name, "The method name must not be null." );
Contracts.assertNotNull( name, log.methodNameMustNotBeNull() );

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

Expand All @@ -88,12 +84,10 @@ public MethodConstraintMappingContext method(String name, Class<?>... parameterT

String parameterTypesAsString = sb.length() > 2 ? sb.substring( 0, sb.length() - 2 ) : sb.toString();

throw new IllegalArgumentException(
String.format( "Type %s doesn't have a method %s(%s).", beanClass, name, parameterTypesAsString )
);
throw log.unableToFindMethod( beanClass, name, parameterTypesAsString );
}

return new MethodConstraintMappingContextImpl( beanClass, method, mapping );
}

}
}
Expand Up @@ -22,6 +22,8 @@
import org.hibernate.validator.cfg.context.ParameterConstraintMappingContext;
import org.hibernate.validator.cfg.context.ReturnValueConstraintMappingContext;
import org.hibernate.validator.internal.metadata.location.MethodConstraintLocation;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

/**
* Constraint mapping creational context which allows to configure the constraints for one method parameter.
Expand All @@ -34,6 +36,8 @@ public final class ParameterConstraintMappingContextImpl
extends ConstraintMappingContextImplBase
implements ParameterConstraintMappingContext {

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

private final Method method;
private final int parameterIndex;

Expand All @@ -42,7 +46,7 @@ public ParameterConstraintMappingContextImpl(Class<?> beanClass, Method method,
super( beanClass, mapping );

if ( parameterIndex < 0 || parameterIndex >= method.getParameterTypes().length ) {
throw new IllegalArgumentException( "A valid parameter index has to be specified for method '" + method.getName() + "'" );
throw log.invalidMethodParameterIndex( method.getName() );
}

this.method = method;
Expand Down
Expand Up @@ -21,6 +21,9 @@
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.DecimalMax;

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

/**
* Check that the character sequence (e.g. string) being validated represents a number, and has a value
* less than or equal to the maximum value specified.
Expand All @@ -29,16 +32,16 @@
*/
public class DecimalMaxValidatorForCharSequence implements ConstraintValidator<DecimalMax, CharSequence> {

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

private BigDecimal maxValue;

public void initialize(DecimalMax maxValue) {
try {
this.maxValue = new BigDecimal( maxValue.value() );
}
catch ( NumberFormatException nfe ) {
throw new IllegalArgumentException(
maxValue.value() + " does not represent a valid BigDecimal format", nfe
);
throw log.invalidBigDecimalFormat( maxValue.value(), nfe );
}
}

Expand Down
Expand Up @@ -22,6 +22,9 @@
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.DecimalMax;

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

/**
* Check that the number being validated is less than or equal to the maximum
* value specified.
Expand All @@ -30,16 +33,16 @@
*/
public class DecimalMaxValidatorForNumber implements ConstraintValidator<DecimalMax, Number> {

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

private BigDecimal maxValue;

public void initialize(DecimalMax maxValue) {
try {
this.maxValue = new BigDecimal( maxValue.value() );
}
catch ( NumberFormatException nfe ) {
throw new IllegalArgumentException(
maxValue.value() + " does not represent a valid BigDecimal format", nfe
);
throw log.invalidBigDecimalFormat( maxValue.value(), nfe );
}
}

Expand Down
Expand Up @@ -21,21 +21,24 @@
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.DecimalMin;

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

/**
* @author Hardy Ferentschik
*/
public class DecimalMinValidatorForCharSequence implements ConstraintValidator<DecimalMin, CharSequence> {

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

private BigDecimal minValue;

public void initialize(DecimalMin minValue) {
try {
this.minValue = new BigDecimal( minValue.value() );
}
catch ( NumberFormatException nfe ) {
throw new IllegalArgumentException(
minValue.value() + " does not represent a valid BigDecimal format", nfe
);
throw log.invalidBigDecimalFormat( minValue.value(), nfe );
}
}

Expand Down
Expand Up @@ -22,6 +22,9 @@
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.DecimalMin;

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

/**
* Check that the number being validated is less than or equal to the maximum
* value specified.
Expand All @@ -30,16 +33,16 @@
*/
public class DecimalMinValidatorForNumber implements ConstraintValidator<DecimalMin, Number> {

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

private BigDecimal minValue;

public void initialize(DecimalMin minValue) {
try {
this.minValue = new BigDecimal( minValue.value() );
}
catch ( NumberFormatException nfe ) {
throw new IllegalArgumentException(
minValue.value() + " does not represent a valid BigDecimal format", nfe
);
throw log.invalidBigDecimalFormat( minValue.value(), nfe );
}
}

Expand Down
Expand Up @@ -21,6 +21,9 @@
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Digits;

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

/**
* Validates that the character sequence (e.g. string) being validated consists of digits,
* and matches the pattern defined in the constraint.
Expand All @@ -30,6 +33,8 @@
*/
public class DigitsValidatorForCharSequence implements ConstraintValidator<Digits, CharSequence> {

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

private int maxIntegerLength;
private int maxFractionLength;

Expand Down Expand Up @@ -69,10 +74,10 @@ private BigDecimal getBigDecimalValue(CharSequence charSequence) {

private void validateParameters() {
if ( maxIntegerLength < 0 ) {
throw new IllegalArgumentException( "The length of the integer part cannot be negative." );
throw log.invalidLengthForIntegerPart();
}
if ( maxFractionLength < 0 ) {
throw new IllegalArgumentException( "The length of the fraction part cannot be negative." );
throw log.invalidLengthForFractionPart();
}
}
}
Expand Up @@ -21,6 +21,9 @@
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Digits;

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

/**
* Validates that the <code>Number</code> being validates matches the pattern
* defined in the constraint.
Expand All @@ -30,6 +33,8 @@
*/
public class DigitsValidatorForNumber implements ConstraintValidator<Digits, Number> {

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

private int maxIntegerLength;
private int maxFractionLength;

Expand Down Expand Up @@ -61,10 +66,10 @@ public boolean isValid(Number num, ConstraintValidatorContext constraintValidato

private void validateParameters() {
if ( maxIntegerLength < 0 ) {
throw new IllegalArgumentException( "The length of the integer part cannot be negative." );
throw log.invalidLengthForIntegerPart();
}
if ( maxFractionLength < 0 ) {
throw new IllegalArgumentException( "The length of the fraction part cannot be negative." );
throw log.invalidLengthForFractionPart();
}
}
}

0 comments on commit 7f645c3

Please sign in to comment.