public static Object parsePrimitiveValue ( Type type , String stringValue ) { Class < ? > primitiveClass = type instanceof Class < ? > ? ( Class < ? > ) type : null ; if ( type == null || primitiveClass != null ) { if ( primitiveClass == Void . class ) { return null ; } if ( stringValue == null || primitiveClass == null || primitiveClass . isAssignableFrom ( String . class ) ) { return stringValue ; } if ( primitiveClass == Character . class || primitiveClass == char . class ) { if ( stringValue . length ( ) != 1 ) { throw new IllegalArgumentException ( "expected type Character/char but got " + primitiveClass ) ; } return stringValue . charAt ( 0 ) ; } if ( primitiveClass == Boolean . class || primitiveClass == boolean . class ) { return Boolean . valueOf ( stringValue ) ; } if ( primitiveClass == Byte . class || primitiveClass == byte . class ) { return Byte . valueOf ( stringValue ) ; } if ( primitiveClass == Short . class || primitiveClass == short . class ) { return Short . valueOf ( stringValue ) ; } if ( primitiveClass == Integer . class || primitiveClass == int . class ) { return Integer . valueOf ( stringValue ) ; } if ( primitiveClass == Long . class || primitiveClass == long . class ) { return Long . valueOf ( stringValue ) ; } if ( primitiveClass == Float . class || primitiveClass == float . class ) { return Float . valueOf ( stringValue ) ; } if ( primitiveClass == Double . class || primitiveClass == double . class ) { return Double . valueOf ( stringValue ) ; } if ( primitiveClass == DateTime . class ) { return DateTime . parseRfc3339 ( stringValue ) ; } if ( primitiveClass == BigInteger . class ) { return new BigInteger ( stringValue ) ; } if ( primitiveClass == BigDecimal . class ) { return new BigDecimal ( stringValue ) ; } if ( primitiveClass . isEnum ( ) ) { if ( ! ClassInfo . of ( primitiveClass ) . names . contains ( stringValue ) ) { throw new IllegalArgumentException ( String . format ( "given enum name %s not part of " + "enumeration" , stringValue ) ) ; } @ SuppressWarnings ( { "unchecked" , "rawtypes" } ) Enum result = ClassInfo . of ( primitiveClass ) . getFieldInfo ( stringValue ) . < Enum > enumValue ( ) ; return result ; } } throw new IllegalArgumentException ( "expected primitive class, but got: " + type ) ; }
Parses the given string value based on the given primitive type .