Here you can find the source of cast(Class
@SuppressWarnings("unchecked") public static <T> T cast(Class<T> clazz, Object object)
//package com.java2s; public class Main { @SuppressWarnings("unchecked") public static <T> T cast(Class<T> clazz, Object object) { if (clazz.isAssignableFrom(object.getClass())) { return clazz.cast(object); }//from w w w . j a va2 s. c o m throw new ClassCastException("Cannot cast class " + object.getClass().getName() + ", whose classloader is " + object.getClass().getClassLoader() + ", to class " + clazz + ", whose classloader is " + clazz.getClassLoader()); } }