@ SuppressWarnings ( "unchecked" ) public static < T > T convertToType ( final Class < T > clazz , final Object value , final String errorMessage ) { if ( ! clazz . isAssignableFrom ( value . getClass ( ) ) ) { throw new IllegalArgumentException ( errorMessage ) ; } return ( T ) value ; }
Cast an object to the given class and return it or throw IllegalArgumentException if it s not assignable to that class .