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:hudson.model.Items.java

private static <T extends Item> void getAllItems(final ItemGroup root, Class<T> type, List<T> r) {
    List<Item> items = new ArrayList<Item>(((ItemGroup<?>) root).getItems());
    Collections.sort(items, new Comparator<Item>() {
        @Override// w w  w  .j a v  a 2  s .  c  om
        public int compare(Item i1, Item i2) {
            return name(i1).compareToIgnoreCase(name(i2));
        }

        String name(Item i) {
            String n = i.getName();
            if (i instanceof ItemGroup) {
                n += '/';
            }
            return n;
        }
    });
    for (Item i : items) {
        if (type.isInstance(i)) {
            if (i.hasPermission(Item.READ)) {
                r.add(type.cast(i));
            }
        }
        if (i instanceof ItemGroup) {
            getAllItems((ItemGroup) i, type, r);
        }
    }
}

From source file:gemlite.core.support.CompoundObjectImpl.java

@Override
public <T> T valueOf(String name, Class<T> T) {
    Object result = map.get(name);
    if (result != null)
        return T.cast(result);

    return null;//  w ww. j a v  a 2  s.c  om
}

From source file:com.jwt.common.aspects.LoggerAspect.java

@AfterThrowing(pointcut = "setPointcut()")
public void afterThrowing(JoinPoint joinPoint) {
    System.out.println("Accessed the save pointcut");

    Object[] signatureArgs = joinPoint.getArgs();

    for (Object signatureArg : signatureArgs) {
        Class<?> classCast = signatureArg.getClass();
        String classInfo = classCast.cast(signatureArg).toString();
        System.out.println("Returned value " + classInfo);
    }/*from w  w w. j av a  2s  . c om*/
}

From source file:org.shredzone.cilla.core.repository.impl.SectionDaoHibImpl.java

@Override
public <T extends Section> T fetchAs(long id, Class<T> clazz) {
    Section result = fetch(id);/* w  ww.  j av  a 2 s  .co  m*/
    try {
        return clazz.cast(result);
    } catch (ClassCastException ex) {
        return null;
    }
}

From source file:com.fuzhepan.arpc.client.ProxyHandler.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    log.debug("invoke was called!");

    if (method.getName().equals("toString")) {
        return "toString method was called";
    }//  w w w.j a  va2  s.  c  o m

    RpcContext rpcContext = new RpcContext(interfaceName, method.getName(), method.getParameterTypes(), args);

    //get service info and load balance
    List<HostPortPair> serviceList = RpcConfig.getServiceList(serviceName);
    if (serviceList == null || serviceList.size() == 0)
        throw new ClassNotFoundException("not find service : " + serviceName);
    int index = requestCount.get() % serviceList.size();
    if (requestCount.get() > 100)
        requestCount.set(0);
    else
        requestCount.getAndIncrement();
    HostPortPair hostPort = serviceList.get(index);

    Socket socket = new Socket(hostPort.host, hostPort.port);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
    objectOutputStream.writeObject(rpcContext);
    objectOutputStream.flush();

    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
    Object response = objectInputStream.readObject();

    objectInputStream.close();
    objectOutputStream.close();
    socket.close();

    Class methodReturnType = method.getReturnType();
    return methodReturnType.cast(response);
}

From source file:com.shigengyu.hyperion.cache.WorkflowStateCache.java

public <T extends WorkflowState> T get(final Class<T> workflowStateClass) {
    try {// w ww  .j  av a 2 s .  c o  m
        return workflowStateClass.cast(cache.get(workflowStateClass));
    } catch (final ExecutionException e) {
        throw new WorkflowStateException("Failed to get workflow state by type [{}]", workflowStateClass, e);
    }
}

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

@RequestMapping(value = "/infopoi", params = "id")
public ModelAndView InfoPoi(@RequestParam(value = "id") String id) {
    ModelAndView model = new ModelAndView("infopoi");
    CompletePOI poi = mongoRepo.findOne(id);

    model.addObject("poi", poi);
    for (AbstractPoiComponent comp : poi.getComponents()) {
        try {/*from ww  w.j  a v  a  2s. com*/
            String slug = comp.slug();
            int index = slug.lastIndexOf(".");
            String cname = slug.substring(index + 1).replace("Component", "").toLowerCase();

            Class c = Class.forName(slug);
            model.addObject(cname, c.cast(comp));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return model;

}

From source file:com.cloudera.api.model.ApiModelTest.java

static <T> T xmlToObject(String text, Class<T> type)
        throws JAXBException, UnsupportedEncodingException, IllegalAccessException, InstantiationException {
    JAXBContext jc = JAXBContext.newInstance(type);
    Unmarshaller um = jc.createUnmarshaller();
    ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes(TEXT_ENCODING));
    Object res = um.unmarshal(bais);
    return type.cast(res);
}

From source file:com.github.nethad.clustermeister.provisioning.ec2.AmazonNode.java

@Override
public <T extends Node> T as(Class<T> clazz) {
    return clazz.cast(this);
}

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

@RequestMapping(value = "/infopoi", params = "nome")
public ModelAndView InfoPoiNome(@RequestParam(value = "nome") String nome) {
    ModelAndView model = new ModelAndView("infopoi");
    Iterator poi_iterator = mongoRepo.findAll().iterator();
    boolean ok = false;
    while (poi_iterator.hasNext() && !ok) {
        CompletePOI poi = (CompletePOI) poi_iterator.next();
        if (poi.getName().equals(nome)) {
            ok = true;/*  w ww. ja  v  a 2  s .  c  o  m*/
            model.addObject("poi", poi);
            for (AbstractPoiComponent comp : poi.getComponents()) {
                try {
                    String slug = comp.slug();
                    int index = slug.lastIndexOf(".");
                    String cname = slug.substring(index + 1).replace("Component", "").toLowerCase();

                    Class c = Class.forName(slug);
                    model.addObject(cname, c.cast(comp));

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    return model;

}