Java tutorial
//package com.java2s; public class Main { public static <T> T castObjectOrThrow(Object o, Class<T> clazz) { try { return clazz.cast(o); } catch (ClassCastException e) { throw new ClassCastException(o.toString() + " must implement " + clazz.getSimpleName()); } } }