Java tutorial
//package com.java2s; import java.util.Collections; import java.util.List; public class Main { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> void checkedAtRuntime(List<T> list, Class<T> clazz, Object data) { List tryToAddNotTStuff = Collections.checkedList(list, clazz); try { tryToAddNotTStuff.add(data); } catch (ClassCastException e) { System.out.println("can't add data different that expected"); } System.out.println(tryToAddNotTStuff); } }