@ SuppressWarnings ( "unchecked" ) public < T > T get ( String key , Class < T > targetType , boolean required ) throws WebApplicationException { String value = get ( key ) ; if ( value == null ) { if ( required ) { throw new BadRequestException ( "Missing parameter: " + key ) ; } Object result = null ; if ( targetType . isPrimitive ( ) ) { if ( targetType == boolean . class ) { result = Boolean . FALSE ; } else if ( targetType == int . class ) { result = Integer . valueOf ( 0 ) ; } else if ( targetType == long . class ) { result = Long . valueOf ( 0 ) ; } else if ( targetType == double . class ) { result = Double . valueOf ( 0 ) ; } else if ( targetType == float . class ) { result = Float . valueOf ( 0 ) ; } else if ( targetType == byte . class ) { result = Byte . valueOf ( ( byte ) 0 ) ; } else if ( targetType == short . class ) { result = Short . valueOf ( ( short ) 0 ) ; } else if ( targetType == char . class ) { result = ' ' ; } } return ( T ) result ; } try { return convertValue ( value , targetType ) ; } catch ( WebApplicationException e ) { throw e ; } catch ( Exception e ) { throw new BadRequestException ( "Failed to convert '" + value + "' to type " + targetType ) ; } }
Gets the single parameter in a generic and flexible way .