Here you can find the source of castOrNull(Object o, Class
Parameter | Description |
---|---|
o | The object to cast |
cls | The class to cast o to |
public static <T> T castOrNull(Object o, Class<T> cls)
//package com.java2s; //License from project: LGPL public class Main { /**/* www . j a v a2 s.c om*/ * Cast object to class or return null. * @param o The object to cast * @param cls The class to cast o to * @return o cast as cls. null if o is null or not a instance of cls. */ public static <T> T castOrNull(Object o, Class<T> cls) { if (o == null) return null; if (cls.isInstance(o)) return cls.cast(o); return null; } }