Here you can find the source of castToTypeOrNull(Object object, Class
static <T> T castToTypeOrNull(Object object, Class<T> clazz)
//package com.java2s; //License from project: Open Source License public class Main { static <T> T castToTypeOrNull(Object object, Class<T> clazz) { return clazz.isInstance(object) ? clazz.cast(object) : null; }/*ww w . ja va 2 s .co m*/ }