Example usage for java.lang Class cast

List of usage examples for java.lang Class cast

Introduction

In this page you can find the example usage for java.lang Class cast.

Prototype

@SuppressWarnings("unchecked")
@HotSpotIntrinsicCandidate
public T cast(Object obj) 

Source Link

Document

Casts an object to the class or interface represented by this Class object.

Usage

From source file:Main.java

public static <T> T castAs(Object o, Class<T> c) {
    return c.cast(o);
}

From source file:Main.java

public static <T> T as(Class<T> t, Object o) {
    return t.isInstance(o) ? t.cast(o) : null;
}

From source file:Main.java

public static <T> T castObjectOrThrow(Object o, Class<T> clazz) {
    try {/*from ww w . j  a v  a2  s. c o  m*/
        return clazz.cast(o);
    } catch (ClassCastException e) {
        throw new ClassCastException(o.toString() + " must implement " + clazz.getSimpleName());
    }
}

From source file:Main.java

public static <T> T as(Object obj, Class<T> tClass) {
    return tClass.isInstance(obj) ? tClass.cast(obj) : null;
}

From source file:Main.java

public static <T> T[] newArray(final Class<T[]> pClass, final int pLength) {
    return pClass.cast(Array.newInstance(pClass.getComponentType(), pLength));
}

From source file:Main.java

public static <T> T get(Class<T> cls) {
    try {/*from  w ww  .  j  av a2  s.  c o m*/
        T t = cls.cast(threadLocal.get());
        return t;
    } catch (Exception ex) {
        return null;
    } finally {
        remove();
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <C> C getComponent(Class<C> componentType, Object o) {
    return componentType.cast(o);
}

From source file:net.eusashead.hateoas.header.impl.PropertyUtil.java

public static <V> V getValue(Object target, String name, Class<V> type) {
    return type.cast(ConvertUtils.convert(getValue(target, name), type));
}

From source file:Main.java

public static <T extends View> T lfind(Activity aty, int id, Class<T> clazz) {
    View view = aty.findViewById(id);
    return clazz.cast(view);
}

From source file:com.athena.dolly.common.provider.AppContext.java

public static <T> T getBean(Class<T> clazz) {
    return clazz.cast(ctx.getBean(clazz));
}