public static Object convertToWrappedPrimitive ( Object source , Class < ? > wrapper ) { if ( source == null || wrapper == null ) { return null ; } if ( wrapper . isInstance ( source ) ) { return source ; } if ( wrapper . isAssignableFrom ( source . getClass ( ) ) ) { return source ; } if ( source instanceof Number ) { return convertNumberToWrapper ( ( Number ) source , wrapper ) ; } else { //ensure we dont try to convert text to a number, prevent NumberFormatException if ( Number . class . isAssignableFrom ( wrapper ) ) { //test for int or fp number if ( ! source . toString ( ) . matches ( NUMERIC_TYPE ) ) { throw new ConversionException ( String . format ( "Unable to convert string %s its not a number type: %s" , source , wrapper ) ) ; } } return convertStringToWrapper ( source . toString ( ) , wrapper ) ; } }
Convert to wrapped primitive