Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

HV-573: Using map-based look up in ReflectionHelper#boxedType()

  • Loading branch information...
commit a84695a83d99a6da4b575bfbcce5d0698be4cdb6 1 parent 86b46d4
@gunnarmorling gunnarmorling authored
View
2  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 );
}
}
View
2  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;
View
76 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 @@
private static String[] PROPERTY_ACCESSOR_PREFIXES = { "is", "get", "has" };
+ private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPES;
+
+ static {
+ Map<Class<?>, 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;
}
/**
View
3  engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java
@@ -346,9 +346,6 @@
@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();
Please sign in to comment.
Something went wrong with that request. Please try again.