@ SuppressWarnings ( "unchecked" ) private static < T > T cast ( Object obj , Class < T > clazz ) { if ( obj == null ) { return null ; } if ( clazz == null ) { throw new IllegalArgumentException ( "parameter 'Class<T> clazz' is not allowed to be null." ) ; } if ( clazz . isEnum ( ) && obj instanceof String ) { Class < Enum > enumClazz = ( Class < Enum > ) clazz ; return ( T ) Enum . valueOf ( enumClazz , obj . toString ( ) ) ; } if ( clazz . isPrimitive ( ) ) { if ( clazz == Integer . TYPE ) { clazz = ( Class < T > ) Integer . class ; } else if ( clazz == Long . TYPE ) { clazz = ( Class < T > ) Long . class ; } else if ( clazz == Short . TYPE ) { clazz = ( Class < T > ) Short . class ; } else if ( clazz == Double . TYPE ) { clazz = ( Class < T > ) Double . class ; } else if ( clazz == Float . TYPE ) { clazz = ( Class < T > ) Float . class ; } else if ( clazz == Character . TYPE ) { clazz = ( Class < T > ) Character . class ; } else if ( clazz == Byte . TYPE ) { clazz = ( Class < T > ) Byte . class ; } else if ( clazz == Boolean . TYPE ) { clazz = ( Class < T > ) Boolean . class ; } } return clazz . cast ( obj ) ; }
make a forced class casting .