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:jp.go.nict.langrid.servicecontainer.executor.jsonrpc.DynamicJsonRpcServiceExecutor.java

public static <T> T create(String invocationName, long invocationId, Endpoint endpoint,
        Class<T> interfaceClass) {
    return interfaceClass.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
            new Class[] { interfaceClass, StreamingNotifier.class },
            new DynamicJsonRpcServiceExecutor(invocationName, invocationId, endpoint)));
}

From source file:com.examples.with.different.packagename.ClassPublicInterface.java

public static <L> void bindEventsToMethod(final Object target, final String methodName,
        final Object eventSource, final Class<L> listenerType, final String... eventTypes) {
    final L listener = listenerType.cast(Proxy.newProxyInstance(target.getClass().getClassLoader(),
            new Class[] { listenerType }, new EventBindingInvocationHandler(target, methodName, eventTypes)));
    addEventListener(eventSource, listenerType, listener);
}

From source file:ExceptionUtils.java

/**
 * Locates a particular type of exception, working its way via the cause property of each exception in the exception
 * stack.// w w  w  . ja v  a2s .com
 *
 * @param t    the outermost exception
 * @param type the type of exception to search for
 * @return the first exception of the given type, if found, or null
 */
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
    Throwable current = t;

    while (current != null) {
        if (type.isInstance(current))
            return type.cast(current);

        // Not a match, work down.

        current = current.getCause();
    }

    return null;
}

From source file:Main.java

/**
 * Convert the collection to a typed list of given type. 
 * @param <T>/* w w w.ja  v  a  2 s . c  o  m*/
 * @param collection
 * @param type
 * @return
 */
public static <T> List<T> asList(Collection<?> collection, Class<T> type) {
    List<T> list = new ArrayList<T>();
    for (Object o : collection) {
        if (type.isInstance(o)) {
            list.add(type.cast(o));
        }
    }
    return list;
}

From source file:org.ow2.proactive.procci.service.transformer.TransformerProvider.java

protected static <T> T castInstanceModel(Class<T> classe, InstanceModel instanceModel) {

    if (classe.isInstance(instanceModel)) {
        return classe.cast(instanceModel);
    } else {/*from   ww  w .ja  v a 2s .c om*/
        logger.error("Error in castInstanceModel : the instance of " + instanceModel.getClass().getName()
                + " is not an instance of " + classe.getName());
        throw new ServerException();
    }

}

From source file:Main.java

public static <T> T getArgument(int index, Class<T> clazz, Object... objects) {
    T ret = null;//from   w ww . jav a 2  s. co m

    if (objects != null && index < objects.length && clazz != null && clazz.isInstance(objects[index])) {
        ret = clazz.cast(objects[index]);
    }
    return ret;
}

From source file:Main.java

/**
 * Ensures that all elements of the given set can be cast to a desired type.
 * // w ww.  j a v a  2  s. co  m
 * @param list the set to check
 * @param type the desired type
 * @return a set of the desired type
 * @throws ClassCastException if an element cannot be cast to the desired type
 */
@SuppressWarnings("unchecked")
public static <E> Set<E> checkSet(Set<?> set, Class<E> type) {
    if (DEBUG) {
        Set<E> copy = new HashSet<E>();
        for (Object element : set) {
            copy.add(type.cast(element));
        }
        return copy;
    }
    return (Set<E>) set;
}

From source file:de.thingweb.util.encoding.ContentHelper.java

public static <T> T ensureClass(Object o, Class<T> clazz) {
    try {// w  w w  .j  a va2s.co m
        return clazz.cast(o);
    } catch (ClassCastException e) {
        final String msg = String.format("expected value to be of type %s, not %s in %s", clazz, o.getClass(),
                o.toString());
        if (o instanceof String) {
            try {
                return clazz.cast(NumberFormat.getInstance().parse((String) o));
            } catch (ParseException e1) {
                log.warn(msg);
                throw new IllegalArgumentException(msg);
            }
        } else
            throw new IllegalArgumentException(msg);
    }
}

From source file:io.fabric8.jolokia.assertions.Assertions.java

public static <T> T asInstanceOf(Object value, Class<T> clazz) {
    assertThat(value).isInstanceOf(clazz);
    return clazz.cast(value);
}

From source file:Main.java

public static <T> T UnMarshall(Class<T> objectClass, Node node) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(objectClass);
    Unmarshaller u = jc.createUnmarshaller();
    return objectClass.cast(u.unmarshal(node));
}