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.github.rinde.logistics.pdptw.mas.comm.AuctionCommModel.java

@Override
public <U> U get(Class<U> clazz) {
    if (clazz.isAssignableFrom(AuctionCommModel.class)) {
        return clazz.cast(this);
    }//w w  w  . j a  v  a2 s . c om
    throw new IllegalArgumentException(AuctionCommModel.class.getSimpleName() + " does not support " + clazz);
}

From source file:com.heliosapm.tsdblite.handlers.websock.WebSocketRequest.java

/**
 * Returns the arg value for the specified index
 * @param index The arg index/*from   ww w . j  a  va2 s  .co  m*/
 * @param type The expected type of the value
 * @return The value
 */
public <T> T getArg(final int index, final Class<T> type) {
    if (index < 0 || index > args.length - 1)
        throw new IllegalArgumentException(
                "Invalid index [" + index + "] for arg array of length [" + args.length + "]");
    if (type == null)
        throw new IllegalArgumentException("The passed type was null");
    final Object o = args[index];
    return o == null ? null : type.cast(o);
}

From source file:com.flowpowered.commons.datatable.SerializableHashMap.java

@Override
public <T> T get(String key, Class<T> clazz) {
    Serializable s = get(key);//from   www  . j  a v a2 s .  co  m
    if (s != null) {
        try {
            return clazz.cast(s);
        } catch (ClassCastException ignore) {
        }
    }
    return null;
}

From source file:org.ocelotds.spring.SpringResolver.java

@Override
public <T> T resolveDataService(Class<T> clazz) throws DataServiceException {
    Map<String, ?> beansOfType = getApplicationContext().getBeansOfType(clazz);
    if (beansOfType == null || beansOfType.isEmpty()) {
        throw new DataServiceException("Unable to find any Spring bean of type : " + clazz.getName());
    }//w  w  w  .jav a  2  s.c  o m
    if (beansOfType.size() > 1) {
        throw new DataServiceException("Multiple (" + beansOfType.size() + ") Spring beans of type : '"
                + clazz.getName() + "' founded. Unable to choose one.");
    }
    return clazz.cast(beansOfType.values().iterator().next());
}

From source file:de.hska.ld.core.client.ClientRequest.java

public <T> T getParsedBody(Class<T> type) {
    if (unparsedBody != null) {
        try {/* w  ww. j a v a  2  s. c o  m*/
            this.parsedBody = mapper.readValue(this.unparsedBody, type);
            this.unparsedBody = null;
        } catch (IOException e) {
            this.exceptionLogger.log(this.getLoggingPrefix() + this.action, e,
                    "Parsing client request failed! (2)");
        }
    }
    if (this.parsedBody != null) {
        return type.cast(this.parsedBody);
    } else {
        return null;
    }
}

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

@RequestMapping(value = "/editpoi", params = "id")
public ModelAndView editPoibyID(@RequestParam(value = "id") String id) {
    ModelAndView model = new ModelAndView("editform");
    try {//from  w  w w  . j  a v  a 2  s.co  m
        CompletePOI poi = pm.getCompletePoiById(id);
        model.addObject("nome", poi.getName());
        model.addObject("loc", poi.getLocation());
        model.addObject("cat", poi.getCategories());
        model.addObject("id", poi.getId());
        model.addObject("shortD", poi.getShortDescription());
        model.addObject("addr", poi.getAddress());
        model.addObject("visibility", poi.getVisibility());

        for (AbstractPoiComponent comp : poi.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();

            try {
                Class c = Class.forName(slug);
                model.addObject(cname, c.cast(comp));
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PoiViewController.class.getName()).log(Level.SEVERE, null, ex);

            }
        }
        return model;
    } catch (RuntimeException e) {
        ModelAndView model2 = new ModelAndView("errorViewPoi");
        model2.addObject("err", "Errore impossibile trovare il poi con id: " + id);
        return model2;
    }
}

From source file:de.zib.gndms.infra.system.GNDMSystemDirectory.java

/**
 * Retrieves the configlet stored with the key <tt>name</tt> from {@link #configlets} map, casts it to a <tt>T</tt> class
 * and returns it.//from w  w  w.  j  av a  2 s .  c om
 *
 * @param clazz the class the Configlet belongs to
 * @param name the name of the Configlet
 * @param <T> the class the instance will be casted to 
 * @return a casted configlet from the <tt>configlets</tt> map
 */
public <T extends Configlet> T getConfiglet(final @NotNull Class<T> clazz, final @NotNull String name) {
    return clazz.cast(configlets.get(name));
}

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

@RequestMapping(value = "/editpoi", params = "name")
public ModelAndView editPoi(@RequestParam(value = "name") String name) {
    ModelAndView model = new ModelAndView("editform");
    try {// w  w w.java2s .c  o  m
        CompletePOI poi = pm.findOneCompletePoiByName(name);
        model.addObject("nome", poi.getName());
        model.addObject("loc", poi.getLocation());
        model.addObject("cat", poi.getCategories());
        model.addObject("id", poi.getId());
        model.addObject("shortD", poi.getShortDescription());
        model.addObject("addr", poi.getAddress());
        model.addObject("visibility", poi.getVisibility());

        for (AbstractPoiComponent comp : poi.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();

            try {
                Class c = Class.forName(slug);
                model.addObject(cname, c.cast(comp));
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PoiViewController.class.getName()).log(Level.SEVERE, null, ex);

            }

        }

        return model;
    } catch (RuntimeException e) {
        ModelAndView model2 = new ModelAndView("errorViewPoi");
        model2.addObject("err", "Errore impossibile trovare il poi: " + e.toString());
        return model2;
    }
}

From source file:com.vk.sdk.payments.VKIInAppBillingService.java

/**
 * @param iInAppBillingService implementation of com.android.vending.billing.IInAppBillingService
 *///from  w  w w. j  ava  2s.  c o  m
public VKIInAppBillingService(@NonNull Object iInAppBillingService) {
    this.mIInAppBillingService = iInAppBillingService;

    Class<?> mIInAppBillingServiceClass;
    try {
        mIInAppBillingServiceClass = Class.forName("com.android.vending.billing.IInAppBillingService");
        mIInAppBillingServiceClass.cast(mIInAppBillingService);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(PARAMS_ARE_NOT_VALID_ERROR);
    }
}

From source file:com.slytechs.capture.FileFactory.java

public <T extends FileCapture<? extends FilePacket>> T newFile(final Class<T> c, final File file)
        throws IOException, FileFormatException {

    if (c == PcapFile.class) {
        final FileCapture pcap = PcapFileCapture.createFile(file, FileMode.ReadWrite, ByteOrder.BIG_ENDIAN,
                null);// ww  w  . j  a v a2 s . c o  m
        return c.cast(pcap);
    }
    if (c == SnoopFile.class) {
        final FileCapture snoop = SnoopFileCapture.createFile(file, FileMode.ReadWrite, null);
        return c.cast(snoop);
    } else {
        throw new FileFormatException("Unknown file format " + c.getName());
    }
}