Here you can find the source of castSafe(Class
Parameter | Description |
---|---|
inValue | a parameter |
@SuppressWarnings("unchecked") public static <T> T castSafe(Class<T> inClass, Object inValue, T inDefault)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w ww. ja v a 2 s.co m * @param inValue * @return casted value or given default if {@link ClassCastException} would * occur */ @SuppressWarnings("unchecked") public static <T> T castSafe(Class<T> inClass, Object inValue, T inDefault) { if (inClass == null) { throw new IllegalArgumentException("no target class specified"); } else if (inValue != null && !inClass.isAssignableFrom(inValue.getClass())) { return inDefault; } try { return (T) inValue; } catch (Exception e) { return inDefault; } } /** * @param inValue * @return casted value or <code>null</code> if {@link ClassCastException} * would occur */ public static <T> T castSafe(Class<? extends T> inClass, Object inValue) { return castSafe(inClass, inValue, null); } }