I don't think it's often used exactly as you have shown. Most common use I have seen is where folks using generics are trying to do the equivalent of this:
public static T castToNumber(Object o) {
return (T)o;
}
Which doesn't really do anything useful because of type erasure.
Whereas this works, and is type safe (modulo ClassCastExceptions
):
public static T castToNumber(Object o, Class clazz) {
return clazz.cast(o);
}
EDIT: Couple of examples of use from google guava:
- MutableClassToInstanceMap
- Cute use in Throwables#propagateIfInstanceOf, for type safe
generic throw spec