List of usage examples for java.lang Class cast
@SuppressWarnings("unchecked") @HotSpotIntrinsicCandidate public T cast(Object obj)
From source file:org.solmix.runtime.support.spring.SpringBeanProvider.java
/** * {@inheritDoc}//from w w w. j av a2 s . co m * * @see org.solmix.api.bean.ConfiguredBeanProvider#loadBeansOfType(java.lang.Class, * org.solmix.api.bean.ConfiguredBeanProvider.BeanLoaderListener) */ @Override public <T> boolean loadBeansOfType(Class<T> type, BeanLoaderListener<T> listener) { List<String> list = new ArrayList<String>(Arrays.asList(context.getBeanNamesForType(type, false, false))); list.removeAll(passThroughs); Collections.reverse(list); boolean loaded = false; for (String s : list) { Class<?> beanType = context.getType(s); Class<? extends T> t = beanType.asSubclass(type); if (listener.loadBean(s, t)) { Object o = context.getBean(s); if (listener.beanLoaded(s, type.cast(o))) { return true; } loaded = true; } } return loaded || original.loadBeansOfType(type, listener); }
From source file:com.orchestra.portale.controller.EditEventController.java
@RequestMapping(value = "/editevent", params = "id") public ModelAndView editEventbyID(@RequestParam(value = "id") String id) { ModelAndView model = new ModelAndView("editform"); try {// w w w. j ava 2s .c om 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("start", poi.getStart_date()); model.addObject("end", poi.getEnd_date()); 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:com.orchestra.portale.controller.EditEventController.java
@RequestMapping(value = "/editevent", params = "name") public ModelAndView editPoi(@RequestParam(value = "name") String name) { ModelAndView model = new ModelAndView("editeventform"); try {//from w ww .j a v a 2 s .c om 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("start", poi.getStart_date()); model.addObject("end", poi.getEnd_date()); 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:de.zib.gndms.infra.system.GNDMSystemDirectory.java
/** * Returns the instance, which has been registered on {@link #instances} with the name <tt>name</tt>. * The instace will be casted to the parameter <tt>T</tt> of <tt>clazz</tt>. * * @param clazz the class the instance belongs to * @param name the name of the instance as denoted on the map {@link #instances} * @param <T> the class the instance will be casted to * @return an instance from the <tt>instances</tt> map *//* w w w .j a va2 s. co m*/ public synchronized @NotNull <T> T getInstance(@NotNull Class<? extends T> clazz, @NotNull String name) { final Object obj = instances.get(name); if (obj == null) throw new IllegalStateException("Null instance retrieved or invalid or unregistered name"); return clazz.cast(obj); }
From source file:com.github.yongchristophertang.engine.web.response.JsonResultTransformer.java
/** * Transform the json result to an instance of T. * * @param clazz the type class of transformed object *//*ww w . jav a 2 s . c om*/ public <T> ResultTransform<T> object(Class<T> clazz) { return result -> { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); try { return expression == null ? mapper.readValue(result.getResponseStringContent(), clazz) : mapper.readValue( JsonPath.compile(expression).read(result.getResponseStringContent()).toString(), clazz); } catch (JsonParseException e) { Objects.nonNull(expression); return clazz.cast(JsonPath.compile(expression).read(result.getResponseStringContent())); } }; }
From source file:org.apache.taverna.scufl2.validation.correctness.CorrectnessVisitor.java
public <T> T findAncestral(Child<?> bean, Class<T> class1) { T result = null;//from w ww . jav a2 s . co m for (WorkflowBean parent = bean.getParent(); parent != null; parent = ((Child<?>) parent).getParent()) { if (class1.isInstance(parent)) return class1.cast(parent); if (!(parent instanceof Child)) return null; } return result; }
From source file:it.attocchi.jsf2.PageBase.java
protected <T> T getParamObject(String paramName, Class<T> clazz) { T res = null;//from w w w . j av a 2 s .c o m try { if (hasParam(paramName)) { Object o = getExternalContext().getRequestParameterMap().get(paramName); if (o != null) { res = clazz.cast(o); } } } catch (Exception ex) { logger.error("getParamObject", ex); } return res; }
From source file:integration.DiskUsageTest.java
/** * Retrieve a model object from the database. Assumed to exist. * @param targetClass the object's class * @param targetId the object's ID/*from w w w . j a v a 2s.c om*/ * @return the object * @throws ServerError if the retrieval failed */ private <X extends IObject> X queryForObject(Class<X> targetClass, long targetId) throws ServerError { final String query = "FROM " + targetClass.getSimpleName() + " WHERE id = " + targetId; final List<List<RType>> results = iQuery.projection(query, null); return targetClass.cast(((RObject) results.get(0).get(0)).getValue()); }
From source file:edu.gmu.isa681.ctn.EncodedChannel.java
/** * Decodes an encoded packet if and only if: 1) it is marked by Encodeable, and 2) has the same type as * the expected type./*from www . j a v a 2 s .c o m*/ * * @param encoded * @param expectedType * @return * @throws UnexpectedTypeException if either of the two conditions is violated. */ private Encodeable decode(final String encoded, Class<? extends Encodeable> expectedType) throws UnexpectedTypeException { try { JsonAdaptor adaptor = gson.fromJson(encoded, JsonAdaptor.class); // do NOT change the "false" to true. We don't want any code to run until the type is validated Class<?> actual = Class.forName(adaptor.getType(), false, getClass().getClassLoader()); // check if type is marked by Encodeable if (expectedType.isAssignableFrom(actual)) { Object body = gson.fromJson(adaptor.getBody(), actual); return expectedType.cast(body); } else { throw new UnexpectedTypeException(adaptor.getType()); } } catch (JsonSyntaxException | ClassNotFoundException ex) { throw new UnexpectedTypeException(ex); } }
From source file:org.jboss.spring.support.SpringInjectionSupport.java
/** * @param jndiName the JNDI location of the Spring context relative to * 'java:' on JBoss AS5 and JBoss AS6 and 'java:jboss' on JBoss AS7 * @param expectedClass the expected type of the retrieved object * @return/*w w w . j a v a2s.c o m*/ */ private <T> T lookup(String jndiName, Class<T> expectedClass) { Object instance; try { InitialContext initialContext = new InitialContext(); instance = initialContext.lookup(jndiName); if (!(expectedClass.isAssignableFrom(instance.getClass()))) { throw new IllegalArgumentException("Cannot retrieve an " + expectedClass.getName() + " from " + jndiName + " - a " + instance.getClass().getName() + " found instead"); } } catch (NamingException e) { throw new IllegalStateException(e); } return expectedClass.cast(instance); }