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