Here you can find the source of castAs(Class
public static <T> T castAs(Class<T> clazz, Object obj)
//package com.java2s; //License from project: Open Source License public class Main { public static <T> T castAs(Class<T> clazz, Object obj) { if (obj == null) { return null; }/*from w ww. jav a2 s . c o m*/ try { // Try cast. if (clazz.isInstance(obj)) { return clazz.cast(obj); } } catch (ClassCastException ex) { // No op. } return null; } }