Here you can find the source of cast(Class
@SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T cast(Class<T> type, Object key, Object value) throws Exception
//package com.java2s; //License from project: Apache License public class Main { /**/*from w w w . j a va 2 s .c o m*/ * @since 1.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T cast(Class<T> type, Object key, Object value) throws Exception { if (value == null) { return null; } if (!type.isAssignableFrom(value.getClass())) { throw new Exception("Value has invalid type" + (key == null ? "" : " for key: " + key) + " -- expected '" + type.getName() + "', got: " + value.getClass().getName()); } return (T) value; } }