Java tutorial
//package com.java2s; public class Main { @SuppressWarnings("unchecked") static public <T> T dynamicCast(Object object, Class<T> clazz) { if (object != null && clazz != null && object.getClass().equals(clazz)) { return (T) object; } return null; } @SuppressWarnings("unchecked") static public <T> T dynamicCast(Object object) { if (object != null) { try { return (T) object; } catch (Exception e) { } } return null; } }