Java Object Type Case castAs(Class clazz, Object obj)

Here you can find the source of castAs(Class clazz, Object obj)

Description

cast As

License

Open Source License

Declaration

public static <T> T castAs(Class<T> clazz, Object obj) 

Method Source Code

//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;
    }
}

Related

  1. cast(Object x)
  2. cast(Object[] parameters, Class[] types)
  3. cast(String type)
  4. castAndThrow(Throwable e)
  5. castArray(Object[] array, T[] targetArray)
  6. castAsDoubleArray(int[] input)
  7. castClass(Class aClass)
  8. castClone(U obj, T superClone)
  9. castEnum(Enum enum_param)