@ SuppressWarnings ( "unchecked" ) public static < T > T parse ( Class < T > clz , String objectValue ) { switch ( clz . getName ( ) ) { case "int" : return ( T ) Integer . valueOf ( objectValue ) ; case "short" : return ( T ) Short . valueOf ( objectValue ) ; case "long" : if ( objectValue . endsWith ( "L" ) || objectValue . endsWith ( "l" ) ) { objectValue = objectValue . substring ( 0 , objectValue . length ( ) - 1 ) ; } return ( T ) Long . valueOf ( objectValue ) ; case "double" : return ( T ) Double . valueOf ( objectValue ) ; case "float" : return ( T ) Float . valueOf ( objectValue ) ; case "boolean" : if ( objectValue . equalsIgnoreCase ( Boolean . TRUE . toString ( ) ) ) { return ( T ) Boolean . TRUE ; } else if ( objectValue . equalsIgnoreCase ( Boolean . FALSE . toString ( ) ) ) { return ( T ) Boolean . FALSE ; } else { throw new IllegalArgumentException ( String . format ( "The String %s cannot parse as boolean." , objectValue ) ) ; } case "char" : if ( objectValue . length ( ) == 1 ) { return ( T ) new Character ( objectValue . charAt ( 0 ) ) ; } else { throw new IllegalArgumentException ( String . format ( "The String %s cannot parse as char." , objectValue ) ) ; } case "byte" : return ( T ) Byte . valueOf ( objectValue ) ; default : return null ; } }
Parse the string value to a primitive type