diff --git a/engine/src/main/java/org/hibernate/validator/internal/metadata/location/BeanConstraintLocation.java b/engine/src/main/java/org/hibernate/validator/internal/metadata/location/BeanConstraintLocation.java index 411370ad14..54cbc85c54 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/metadata/location/BeanConstraintLocation.java +++ b/engine/src/main/java/org/hibernate/validator/internal/metadata/location/BeanConstraintLocation.java @@ -89,7 +89,7 @@ public Type typeOfAnnotatedElement() { else { t = ReflectionHelper.typeOf( member ); if ( t instanceof Class && ( (Class) t ).isPrimitive() ) { - t = ReflectionHelper.boxedType( t ); + t = ReflectionHelper.boxedType( (Class) t ); } } diff --git a/engine/src/main/java/org/hibernate/validator/internal/metadata/location/MethodConstraintLocation.java b/engine/src/main/java/org/hibernate/validator/internal/metadata/location/MethodConstraintLocation.java index 0322c08e89..3f0bab2b4c 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/metadata/location/MethodConstraintLocation.java +++ b/engine/src/main/java/org/hibernate/validator/internal/metadata/location/MethodConstraintLocation.java @@ -71,7 +71,7 @@ public Type typeOfAnnotatedElement() { } if ( t instanceof Class && ( (Class) t ).isPrimitive() ) { - t = ReflectionHelper.boxedType( t ); + t = ReflectionHelper.boxedType( (Class) t ); } return t; diff --git a/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java b/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java index 6db4b946b0..e39ca2fc6c 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java +++ b/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java @@ -31,6 +31,7 @@ import java.security.AccessController; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -54,6 +55,7 @@ import org.hibernate.validator.internal.util.privilegedactions.NewInstance; import org.hibernate.validator.internal.util.privilegedactions.SetAccessibility; +import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap; import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES; /** @@ -70,6 +72,24 @@ public final class ReflectionHelper { private static String[] PROPERTY_ACCESSOR_PREFIXES = { "is", "get", "has" }; + private static final Map, Class> PRIMITIVE_TO_WRAPPER_TYPES; + + static { + Map, Class> temp = newHashMap( 9 ); + + temp.put( boolean.class, Boolean.class ); + temp.put( char.class, Character.class ); + temp.put( double.class, Double.class ); + temp.put( float.class, Float.class ); + temp.put( long.class, Long.class ); + temp.put( int.class, Integer.class ); + temp.put( short.class, Short.class ); + temp.put( byte.class, Byte.class ); + temp.put( Void.TYPE, Void.TYPE ); + + PRIMITIVE_TO_WRAPPER_TYPES = Collections.unmodifiableMap( temp ); + } + /** * Private constructor in order to avoid instantiation. */ @@ -683,54 +703,26 @@ public static boolean haveSameSignature(Method method1, Method method2) { } /** - * Returns the autoboxed type of a primitive type. - * - * @param primitiveType - * the primitive type - * - * @return the autoboxed type of a primitive type. In case {@link Void} is + * Returns the auto-boxed type of a primitive type. + * + * @param primitiveType the primitive type + * + * @return the auto-boxed type of a primitive type. In case {@link Void} is * passed (which is considered as primitive type by * {@link Class#isPrimitive()}), {@link Void} will be returned. - * - * @throws IllegalArgumentException - * in case the parameter {@code primitiveType} does not - * represent a primitive type. + * + * @throws IllegalArgumentException in case the parameter {@code primitiveType} does not + * represent a primitive type. */ - public static Class boxedType(Type primitiveType) { - if ( !( primitiveType instanceof Class ) && !( (Class) primitiveType ).isPrimitive() ) { + public static Class boxedType(Class primitiveType) { + + Class wrapperType = PRIMITIVE_TO_WRAPPER_TYPES.get( primitiveType ); + + if ( wrapperType == null ) { throw log.getHasToBeAPrimitiveTypeException( primitiveType.getClass() ); } - if ( primitiveType == boolean.class ) { - return Boolean.class; - } - else if ( primitiveType == char.class ) { - return Character.class; - } - else if ( primitiveType == double.class ) { - return Double.class; - } - else if ( primitiveType == float.class ) { - return Float.class; - } - else if ( primitiveType == long.class ) { - return Long.class; - } - else if ( primitiveType == int.class ) { - return Integer.class; - } - else if ( primitiveType == short.class ) { - return Short.class; - } - else if ( primitiveType == byte.class ) { - return Byte.class; - } - else if ( primitiveType == Void.TYPE ) { - return Void.TYPE; - } - else { - throw log.getUnhandledPrimitiveTypeException(); - } + return wrapperType; } /** diff --git a/engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java b/engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java index 838c881eea..ed38c298e7 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java +++ b/engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java @@ -346,9 +346,6 @@ public interface Log extends BasicLogger { @Message(id = 91, value = "%s has to be a primitive type.") IllegalArgumentException getHasToBeAPrimitiveTypeException(Class clazz); - @Message(id = 92, value = "Unhandled primitive type.") - RuntimeException getUnhandledPrimitiveTypeException(); - @Message(id = 93, value = "null is an invalid type for a constraint validator.") ValidationException getNullIsAnInvalidTypeForAConstraintValidatorException();