Java tutorial
//package com.java2s; public class Main { public static <T> T getView(Object object, Class<T> t) { try { return (T) object; } catch (ClassCastException e) { throw new IllegalStateException(object.getClass().getSimpleName() + t.getSimpleName() + " does not implement contract interface", e); } } }