Skip to content

Commit

Permalink
HV-573: Using map-based look up in ReflectionHelper#boxedType()
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Apr 13, 2012
1 parent 86b46d4 commit a84695a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
Expand Up @@ -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 );
}
}

Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -70,6 +72,24 @@ public final class ReflectionHelper {

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.
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Expand Up @@ -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();

Expand Down

0 comments on commit a84695a

Please sign in to comment.