public static Object stringToParam ( String string , String typeString ) throws IllegalArgumentException { if ( typeString . equals ( "boolean" ) || typeString . equals ( "java.lang.Boolean" ) ) { return Boolean . parseBoolean ( string ) ; } else if ( typeString . equals ( "char" ) || typeString . equals ( "java.lang.Character" ) ) { if ( string . length ( ) == 0 ) { // not sure what to do here return ' ' ; } else { return string . toCharArray ( ) [ 0 ] ; } } else if ( typeString . equals ( "byte" ) || typeString . equals ( "java.lang.Byte" ) ) { return Byte . parseByte ( string ) ; } else if ( typeString . equals ( "short" ) || typeString . equals ( "java.lang.Short" ) ) { return Short . parseShort ( string ) ; } else if ( typeString . equals ( "int" ) || typeString . equals ( "java.lang.Integer" ) ) { return Integer . parseInt ( string ) ; } else if ( typeString . equals ( "long" ) || typeString . equals ( "java.lang.Long" ) ) { return Long . parseLong ( string ) ; } else if ( typeString . equals ( "java.lang.String" ) ) { return string ; } else if ( typeString . equals ( "float" ) || typeString . equals ( "java.lang.Float" ) ) { return Float . parseFloat ( string ) ; } else if ( typeString . equals ( "double" ) || typeString . equals ( "java.lang.Double" ) ) { return Double . parseDouble ( string ) ; } else { Constructor < ? > constr = getConstructor ( typeString ) ; try { return constr . newInstance ( new Object [ ] { string } ) ; } catch ( Exception e ) { throw new IllegalArgumentException ( "Could not get new instance using string constructor for type " + typeString ) ; } } }
Convert a string to an object based on the type string .