Here you can find the source of cast(Object o, Class
Parameter | Description |
---|---|
T | the target type |
o | the object to cast |
klass | the target class (same as T) |
o
is null or the type o
is not a subclass of klass
. The casted value otherwise.
@SuppressWarnings("unchecked") public static <T> T cast(Object o, Class<T> klass)
//package com.java2s; // License: GPL. For details, see LICENSE file. public class Main { /**//from w w w . j a va 2 s . c om * Cast an object savely. * @param <T> the target type * @param o the object to cast * @param klass the target class (same as T) * @return null if <code>o</code> is null or the type <code>o</code> is not * a subclass of <code>klass</code>. The casted value otherwise. */ @SuppressWarnings("unchecked") public static <T> T cast(Object o, Class<T> klass) { if (klass.isInstance(o)) { return (T) o; } return null; } }