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:com.athena.dolly.common.provider.AppContext.java

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

From source file:Main.java

public static <A> A unmarshal(Class<A> clazz, InputStream in) throws JAXBException {
    return clazz.cast(JAXBElement.class.cast(getUnmarshaller(clazz).unmarshal(in)).getValue());
}

From source file:Main.java

/**
 *  Verifies that the passed set contains only elements of the given
 *  type, and returns it as a parameterized type. Throws if any element
 *  is a different type.//from  w w w  . j  a  v  a  2s .co m
 */
@SuppressWarnings("unchecked")
public static <T> Set<T> cast(Set<?> set, Class<T> klass) {
    for (Object obj : set) {
        klass.cast(obj);
    }
    return (Set<T>) set;
}

From source file:Main.java

/**
 *  Verifies that the passed list contains only elements of the given
 *  type, and returns it as a parameterized type. Throws if any element
 *  is a different type./*from ww  w  .  jav  a2 s.c  o m*/
 */
@SuppressWarnings("unchecked")
public static <T> List<T> cast(List<?> list, Class<T> klass) {
    for (Object obj : list) {
        klass.cast(obj);
    }
    return (List<T>) list;
}

From source file:io.github.restdocsext.jersey.JerseyRestDocumentationConfigurerTest.java

private static <T> T getContextProperty(ClientRequestContext requestContext, String property, Class<T> cls) {
    return cls.cast(requestContext.getProperty(property));
}

From source file:Main.java

private static <T> boolean isMatch(Component component, Class<T> type, Predicate<T> predicate) {
    return type.isInstance(component) && predicate.test(type.cast(component));
}

From source file:Main.java

public static Object convertType(Class type, Object value) {
    if (type == null) {
        return null;
    }//from   w  w  w . j  a  v a 2 s.  c  o m
    return type.cast(value);
}

From source file:Main.java

@Nullable
public static <T> T firstChildOfType(@NonNull View root, @NonNull Class<T> type) {
    if (type.isInstance(root)) {
        return type.cast(root);
    }/*  w w  w . j a v a 2  s.  com*/
    if (root instanceof ViewGroup) {
        final ViewGroup rootGroup = (ViewGroup) root;
        for (int i = 0; i < rootGroup.getChildCount(); i++) {
            final View child = rootGroup.getChildAt(i);
            final T childResult = firstChildOfType(child, type);
            if (childResult != null) {
                return childResult;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get a class or interface from an object, if available.  This is useful
 * when an object may implement optional interfaces, and we want to
 * get those interfaces.//from  w w w. j  a  v a 2s .co  m
 * 
 * As an example, see {@link AudioPlayerWithEvents}.
 * 
 * @param object the object to inspect
 * @param desiredClass the class to get
 * @return
 */
public static <T> T getIfAvailable(Object object, Class<T> desiredClass) {
    return (desiredClass.isAssignableFrom(object.getClass())) ? desiredClass.cast(object) : null;
}

From source file:service.emailer.Emailer.java

private static <T> T lookup(Class<T> retvalClass, String jndi) {
    try {//from www . j  a  v a2s .com
        return retvalClass.cast(InitialContext.doLookup(jndi));
    } catch (NamingException ex) {
        throw new IllegalArgumentException("failed to lookup instance of " + retvalClass + " at " + jndi, ex);
    }
}