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.flexive.faces.FxJsfUtils.java

/**
 * Recursively search for the first child of the given class type for the component.
 *
 * @param component the component to be searched
 * @param childType the required child type
 * @return the first child of the given type, or null if no child was found.
 *///ww  w.jav  a  2 s .c o  m
public static <T extends UIComponent> T findChild(UIComponent component, Class<T> childType) {
    for (Object item : component.getChildren()) {
        final UIComponent child = (UIComponent) item;
        if (childType.isAssignableFrom(child.getClass())) {
            return childType.cast(child);
        }
        // search in this child's children
        final T nestedChild = findChild(child, childType);
        if (nestedChild != null) {
            return nestedChild;
        }
    }
    return null;
}

From source file:com.github.jknack.css.expression.AbstractExpression.java

@Override
public <E extends Expression> E adapt(final Class<E> expressionType) {
    notNull(expressionType, "The expressionType is required.");
    if (expressionType.isInstance(this)) {
        return expressionType.cast(this);
    }/*from  ww  w .  j a  va  2  s  .c  om*/
    return null;
}

From source file:com.orchestra.portale.controller.GetDeepeningPageController.java

@RequestMapping(value = "/getDP", params = "id")
public ModelAndView getDp(@RequestParam(value = "id") String id) {

    //Creo la view che sar mostrata all'utente
    ModelAndView model = new ModelAndView("infopoi");
    ModelAndView error = new ModelAndView("errorViewPoi");
    DeepeningPage dp = pm.findDeepeningPage(id);
    //aggiungo il poi al model
    model.addObject("poi", dp);

    //ciclo sulle componenti del poi
    for (AbstractPoiComponent comp : dp.getComponents()) {

        //associazione delle componenti al model tramite lo slug
        String slug = comp.slug();
        int index = slug.lastIndexOf(".");
        String cname = slug.substring(index + 1).replace("Component", "").toLowerCase();
        Class c;
        try {/*  w  w w.j av  a2  s .  com*/
            c = Class.forName(slug);
            model.addObject(cname, c.cast(comp));
        } catch (ClassNotFoundException ex) {

        }

    }

    model.addObject("vartype",
            dp.getClass().toString().substring(dp.getClass().toString().lastIndexOf(".") + 1));
    return model;

}

From source file:edu.umd.cs.psl.util.graph.memory.MemoryNode.java

@Override
public <O> O getAttribute(String type, Class<O> c) {
    return c.cast(getAttribute(type));
}

From source file:com.vwf5.base.utils.DataUtil.java

/**
 * Collect distinct values of a property from an object list instead of
 * doing a for:each then call a getter// www .j  av  a 2s .  c  om
 *
 * @param source object list
 * @param propertyName name of property
 * @param returnClass class of property
 * @return value list of property
 */
public static <T> Set<T> collectUniqueProperty(Collection<?> source, String propertyName,
        Class<T> returnClass) {
    Set<T> propertyValues = Sets.newHashSet();
    try {
        String getMethodName = "get" + upperFirstChar(propertyName);
        for (Object x : source) {
            Class<?> clazz = x.getClass();
            Method getMethod = clazz.getMethod(getMethodName);
            Object propertyValue = getMethod.invoke(x);
            if (returnClass.isAssignableFrom(propertyValue.getClass())) {
                propertyValues.add(returnClass.cast(propertyValue));
            }
        }
        return propertyValues;
    } catch (Exception e) {
        return Sets.newHashSet();
    }
}

From source file:dk.dma.navnet.messages.c2c.broadcast.BroadcastSend.java

public <T extends BroadcastMessage> T tryRead(Class<T> type) throws Exception {
    return type.cast(tryRead());
}

From source file:com.vwf5.base.utils.DataUtil.java

/**
 * Collect values of a property from an object list instead of doing a
 * for:each then call a getter/*from  ww w  . j  a va2  s.c om*/
 *
 * @param source object list
 * @param propertyName name of property
 * @param returnClass class of property
 * @return value list of property
 */
public static <T> List<T> collectProperty(Collection<?> source, String propertyName, Class<T> returnClass) {
    List<T> propertyValues = Lists.newArrayList();
    try {
        String getMethodName = "get" + upperFirstChar(propertyName);
        for (Object x : source) {
            Class<?> clazz = x.getClass();
            Method getMethod = clazz.getMethod(getMethodName);
            Object propertyValue = getMethod.invoke(x);
            if (returnClass.isAssignableFrom(propertyValue.getClass())) {
                propertyValues.add(returnClass.cast(propertyValue));
            }
        }
        return propertyValues;
    } catch (Exception e) {
        return Lists.newArrayList();
    }
}

From source file:au.id.hazelwood.xmltvguidebuilder.config.ConfigFactory.java

private <T> List<T> getTypeList(List entries, Class<T> clazz) {
    List<T> result = new ArrayList<T>();
    for (Object entry : entries) {
        result.add(clazz.cast(entry));
    }/* ww w.  ja va 2  s. com*/
    return result;
}

From source file:com.github.rinde.rinsim.core.model.road.PlaneRoadModel.java

@Override
@Nonnull
public <U> U get(Class<U> type) {
    return type.cast(this);
}

From source file:org.mascherl.session.MascherlSession.java

public <T> T get(String key, Class<T> expectedType) {
    if (dataStorage.containsKey(key)) {
        return expectedType.cast(dataStorage.get(key));
    }//from w w  w . j a  v a  2s.  c  om
    if (jsonRootNode.has(key)) {
        try {
            return objectMapper.reader(expectedType).readValue(jsonRootNode.path(key));
        } catch (IOException | RuntimeException e) {
            return null;
        }
    }
    return null;
}