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.client.soap.SoapClientFactory.java

private <T> T create(Class<T> interfaceClass, AxisStublessInvocationHandler h) {
    return interfaceClass.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
            new Class<?>[] { interfaceClass, SoapRequestAttributes.class, ResponseAttributes.class }, h));
}

From source file:org.opendaylight.ovsdb.lib.schema.DatabaseSchema.java

public <E extends TableSchema<E>> E table(String tableName, Class<E> clazz) {
    TableSchema<E> table = tables.get(tableName);

    if (clazz.isInstance(table)) {
        return clazz.cast(table);
    }//from w  ww . j a  va  2s  . co  m

    return createTableSchema(clazz, table);
}

From source file:com.creactiviti.piper.core.task.SpelTaskEvaluator.java

private <T> MethodExecutor cast(Class<T> type) {
    return (ctx, target, args) -> {
        T value = type.cast(ConvertUtils.convert(args[0], type));
        return new TypedValue(value);
    };//from w w w .j  a  va 2  s  . c  om
}

From source file:org.vader.common.spring.cache.SerializableCacheProxy.java

@Override
public <T> T get(Object key, Class<T> type) {
    final byte[] bytes = cache.get(key, byte[].class);
    try {/*from  w ww. j av a2s. c  o  m*/
        return bytes == null ? null : type.cast(deserialize(bytes));
    } catch (IOException e) {
        LOG.warn("Unable to deserialize cached value", e);
        return null;
    }
}

From source file:gumga.framework.application.GumgaCrudAndQueryNotOnlyTypedRepositoryImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//from ww  w  .  ja  v a 2 s  .  co m
public <E> E getUniqueResult(String hql, Class<E> type, Map<String, Object> param) {
    javax.persistence.Query query = entityManager.createQuery(hql);
    query.setMaxResults(1);
    addParam(query, param);
    return type.cast(query.getSingleResult());
}

From source file:jp.primecloud.auto.ui.mock.MockBeanContext.java

protected <T> Map<String, T> getBeansOfType(Class<T> clazz) {
    Map<String, T> beans = new HashMap<String, T>();
    String id = Introspector.decapitalize(clazz.getSimpleName());
    beans.put(id, clazz.cast(getBean(id)));
    return beans;
}

From source file:org.jenkinsci.plugins.uithemes.rest.model.StatusResponse.java

public <T> T dataTo(Class<T> to) throws IOException {
    if (data == null) {
        return null;
    }/*from w  w  w. j  av  a2  s  . c  om*/
    if (to.isInstance(data)) {
        return to.cast(data);
    }
    if (data instanceof String) {
        return JSONReadWrite.fromString((String) data, to);
    } else {
        String asString = JSONReadWrite.toString(data);
        return JSONReadWrite.fromString(asString, to);
    }
}

From source file:net.camelpe.extension.camel.spi.CdiInjector.java

private <T> T createBeanInstanceViaBeanManager(final Class<T> type, final Bean<T> bean) {
    final T beanInstance = type
            .cast(getBeanManager().getReference(bean, type, getBeanManager().createCreationalContext(bean)));
    getLog().trace("Obtained bean instance [{}] from BeanManager.", beanInstance, type.getName());

    return beanInstance;
}

From source file:guru.qas.martini.jmeter.sampler.SamplerContext.java

protected <T> T get(String key, Class<? extends T> implementation) {
    Map<String, Object> samplerContext = context.getSamplerContext();
    Object o = samplerContext.get(key);
    return implementation.isInstance(o) ? implementation.cast(o) : null;
}

From source file:org.shredzone.cilla.core.event.EventInvoker.java

/**
 * Evaluates a single parameter of the handler method's parameter list.
 *
 * @param type//from w w w . j a v a  2  s. co  m
 *            Expected parameter type
 * @param event
 *            Event with further data
 * @return Parameter value
 */
private Object evaluateParameter(Class<?> type, Event<?> event) {
    Object source = event.getSource();
    if (source != null && type.isAssignableFrom(source.getClass())) {
        return type.cast(source);
    }

    if (type.isAssignableFrom(EventType.class)) {
        return event.getType();
    }

    if (type.isAssignableFrom(Event.class)) {
        return event;
    }

    // Alas, we cannot find anything to satisfy this parameter
    throw new IllegalStateException("Unknown parameter type " + type.getName());
}