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 extends View> T inflateWithOutOfMemoryRetrial(Class<T> clazz, LayoutInflater inflater,
        int resourceId, ViewGroup root, boolean attachToRoot) {
    for (int i = 0; i < OUT_OF_MEMORY_RETRY_COUNT; ++i) {
        try {/*from   w  w w. j  a  v a 2s.c  o  m*/
            return clazz.cast(inflater.inflate(resourceId, root, attachToRoot));
        } catch (OutOfMemoryError e) {
            // Retry with GC.
            System.gc();
        }
    }

    return clazz.cast(inflater.inflate(resourceId, root, attachToRoot));
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> checkMap(Map<?, ?> map, Class<K> keyType, Class<V> valueType) {
    if (DEBUG) {/*w  w w.j  a  v  a 2  s .c om*/
        Map<K, V> copy = new HashMap<K, V>();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            copy.put(keyType.cast(entry.getKey()), valueType.cast(entry.getValue()));
        }
        return copy;
    }
    return (Map<K, V>) map;
}

From source file:com.qrmedia.commons.multispi.provider.ServiceClassnameAttributeProvider.java

public static <T> Function<Object, T> cast(final Class<T> targetClass) {
    return new Function<Object, T>() {
        public T apply(Object from) {
            return targetClass.cast(from);
        }//from   w w  w  .  jav a  2  s  .c o m
    };
}

From source file:Main.java

public static <T> Collection<T> filter(final Collection<?> objects, final Class<T> filterType) {
    final Collection<T> filtered = new ArrayList<T>();
    for (final Object object : objects) {
        if (filterType.isInstance(object)) {
            filtered.add(filterType.cast(object));
        }/* ww  w. ja  va 2 s . com*/
    }
    return filtered;
}

From source file:de.se_rwth.langeditor.util.Misc.java

public static <T> T callGetter(Object element, String getterName, Class<T> returnType) {
    try {/*from w ww  . j  a va2 s .com*/
        return returnType.cast(element.getClass().getMethod(getterName).invoke(element));
    } catch (ClassCastException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
            | NoSuchMethodException | SecurityException e) {
        throw new IllegalArgumentException("Object of type " + element.getClass().getName()
                + "didn't have correctly typed, no-argument method called " + getterName, e);
    }
}

From source file:com.mac.holdempoker.socket.JsonConverter.java

public static Object fromJsonString(String json, Class<? extends Object> type) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    //convert json string to object
    Object obj = objectMapper.readValue(json, type);
    return type.cast(obj);
}

From source file:Main.java

public static <T> T UnMarshall(Class<T> objectClass, String filePath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(objectClass);
    Unmarshaller u = jc.createUnmarshaller();
    File f = new File(filePath);
    return objectClass.cast(u.unmarshal(f));
}

From source file:de.ovgu.featureide.core.typecheck.helper.FujiWrapper.java

@SuppressWarnings("rawtypes")
public static <T> T getParentByType(ASTNode node, Class<T> type) {
    if (node == null) {
        return null;
    }//from w  w w.ja  v a 2 s  .  co m
    if (type.isInstance(node.getParent())) {
        return type.cast(node.getParent());
    } else {
        return getParentByType(node.getParent(), type);
    }
}

From source file:Main.java

public static <X extends View> X getChild(ViewGroup row, Class<X> klass) {
    assert klass != null;
    for (int i = 0; i < row.getChildCount(); i++) {
        View child = row.getChildAt(i);
        if (klass.isAssignableFrom(child.getClass())) {
            return klass.cast(child);
        }/*ww w .j  a v  a2 s  .  com*/

        if (child instanceof ViewGroup) {
            X kid = getChild((ViewGroup) child, klass);
            if (kid != null)
                return kid;
        }
    }
    // fail
    return null;
}

From source file:eu.freme.common.persistence.model.OwnedResource.java

public static <T extends OwnedResource> T fromJson(String json, Class<T> entityClass) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return entityClass.cast(mapper.readValue(json, entityClass));
}