Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit a84695a83d99a6da4b575bfbcce5d0698be4cdb6 1 parent 86b46d4
Gunnar Morling gunnarmorling authored
2  engine/src/main/java/org/hibernate/validator/internal/metadata/location/BeanConstraintLocation.java
@@ -89,7 +89,7 @@ public Type typeOfAnnotatedElement() {
89 89 else {
90 90 t = ReflectionHelper.typeOf( member );
91 91 if ( t instanceof Class && ( (Class<?>) t ).isPrimitive() ) {
92   - t = ReflectionHelper.boxedType( t );
  92 + t = ReflectionHelper.boxedType( (Class<?>) t );
93 93 }
94 94 }
95 95
2  engine/src/main/java/org/hibernate/validator/internal/metadata/location/MethodConstraintLocation.java
@@ -71,7 +71,7 @@ public Type typeOfAnnotatedElement() {
71 71 }
72 72
73 73 if ( t instanceof Class && ( (Class<?>) t ).isPrimitive() ) {
74   - t = ReflectionHelper.boxedType( t );
  74 + t = ReflectionHelper.boxedType( (Class<?>) t );
75 75 }
76 76
77 77 return t;
76 engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java
@@ -31,6 +31,7 @@
31 31 import java.security.AccessController;
32 32 import java.util.ArrayList;
33 33 import java.util.Arrays;
  34 +import java.util.Collections;
34 35 import java.util.HashSet;
35 36 import java.util.Iterator;
36 37 import java.util.List;
@@ -54,6 +55,7 @@
54 55 import org.hibernate.validator.internal.util.privilegedactions.NewInstance;
55 56 import org.hibernate.validator.internal.util.privilegedactions.SetAccessibility;
56 57
  58 +import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;
57 59 import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
58 60
59 61 /**
@@ -70,6 +72,24 @@
70 72
71 73 private static String[] PROPERTY_ACCESSOR_PREFIXES = { "is", "get", "has" };
72 74
  75 + private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPES;
  76 +
  77 + static {
  78 + Map<Class<?>, Class<?>> temp = newHashMap( 9 );
  79 +
  80 + temp.put( boolean.class, Boolean.class );
  81 + temp.put( char.class, Character.class );
  82 + temp.put( double.class, Double.class );
  83 + temp.put( float.class, Float.class );
  84 + temp.put( long.class, Long.class );
  85 + temp.put( int.class, Integer.class );
  86 + temp.put( short.class, Short.class );
  87 + temp.put( byte.class, Byte.class );
  88 + temp.put( Void.TYPE, Void.TYPE );
  89 +
  90 + PRIMITIVE_TO_WRAPPER_TYPES = Collections.unmodifiableMap( temp );
  91 + }
  92 +
73 93 /**
74 94 * Private constructor in order to avoid instantiation.
75 95 */
@@ -683,54 +703,26 @@ public static boolean haveSameSignature(Method method1, Method method2) {
683 703 }
684 704
685 705 /**
686   - * Returns the autoboxed type of a primitive type.
687   - *
688   - * @param primitiveType
689   - * the primitive type
690   - *
691   - * @return the autoboxed type of a primitive type. In case {@link Void} is
  706 + * Returns the auto-boxed type of a primitive type.
  707 + *
  708 + * @param primitiveType the primitive type
  709 + *
  710 + * @return the auto-boxed type of a primitive type. In case {@link Void} is
692 711 * passed (which is considered as primitive type by
693 712 * {@link Class#isPrimitive()}), {@link Void} will be returned.
694   - *
695   - * @throws IllegalArgumentException
696   - * in case the parameter {@code primitiveType} does not
697   - * represent a primitive type.
  713 + *
  714 + * @throws IllegalArgumentException in case the parameter {@code primitiveType} does not
  715 + * represent a primitive type.
698 716 */
699   - public static Class<?> boxedType(Type primitiveType) {
700   - if ( !( primitiveType instanceof Class ) && !( (Class<?>) primitiveType ).isPrimitive() ) {
  717 + public static Class<?> boxedType(Class<?> primitiveType) {
  718 +
  719 + Class<?> wrapperType = PRIMITIVE_TO_WRAPPER_TYPES.get( primitiveType );
  720 +
  721 + if ( wrapperType == null ) {
701 722 throw log.getHasToBeAPrimitiveTypeException( primitiveType.getClass() );
702 723 }
703 724
704   - if ( primitiveType == boolean.class ) {
705   - return Boolean.class;
706   - }
707   - else if ( primitiveType == char.class ) {
708   - return Character.class;
709   - }
710   - else if ( primitiveType == double.class ) {
711   - return Double.class;
712   - }
713   - else if ( primitiveType == float.class ) {
714   - return Float.class;
715   - }
716   - else if ( primitiveType == long.class ) {
717   - return Long.class;
718   - }
719   - else if ( primitiveType == int.class ) {
720   - return Integer.class;
721   - }
722   - else if ( primitiveType == short.class ) {
723   - return Short.class;
724   - }
725   - else if ( primitiveType == byte.class ) {
726   - return Byte.class;
727   - }
728   - else if ( primitiveType == Void.TYPE ) {
729   - return Void.TYPE;
730   - }
731   - else {
732   - throw log.getUnhandledPrimitiveTypeException();
733   - }
  725 + return wrapperType;
734 726 }
735 727
736 728 /**
3  engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java
@@ -346,9 +346,6 @@
346 346 @Message(id = 91, value = "%s has to be a primitive type.")
347 347 IllegalArgumentException getHasToBeAPrimitiveTypeException(Class<?> clazz);
348 348
349   - @Message(id = 92, value = "Unhandled primitive type.")
350   - RuntimeException getUnhandledPrimitiveTypeException();
351   -
352 349 @Message(id = 93, value = "null is an invalid type for a constraint validator.")
353 350 ValidationException getNullIsAnInvalidTypeForAConstraintValidatorException();
354 351

0 comments on commit a84695a

Please sign in to comment.
Something went wrong with that request. Please try again.