List of usage examples for java.lang Class cast
@SuppressWarnings("unchecked") @HotSpotIntrinsicCandidate public T cast(Object obj)
From source file:com.liferay.ide.maven.core.NewMavenJSFModuleProjectProvider.java
@Override public <T> List<T> getData(String key, Class<T> type, Object... params) { if ("archetypeGAV".equals(key) && type.equals(String.class) && (params.length == 1)) { List<T> retval = new ArrayList<>(); String templateName = params[0].toString(); Version latestVersion = _extractLatestVersion( "com.liferay.faces.archetype." + templateName + ".portlet"); String gav = "com.liferay.faces.archetype:com.liferay.faces.archetype." + templateName + ".portlet:" + latestVersion.toString(); retval.add(type.cast(gav)); return retval; }//from w w w.java 2s . c om return super.getData(key, type, params); }
From source file:com.rodaxsoft.mailgun.converters.ListMemberConverter.java
@Override public <T> T convert(Class<T> type, Object value) { ListMember lm = null;// w w w . j av a2s . co m if (value instanceof JSONObject) { JSONObject jsonObj = (JSONObject) value; lm = new ListMember(); lm.setAddress(jsonObj.getString("address")); lm.setName(jsonObj.getString("name")); lm.setSubscribed(jsonObj.getBoolean("subscribed")); JSONObject varsObj = jsonObj.optJSONObject("vars"); if (varsObj != null) { //Use a converter if varClass defined if (varClass != null && ConvertUtils.lookup(JSONObject.class, varClass) != null) { lm.setVars(ConvertUtils.convert(varsObj, varClass)); } else { lm.setVars(varsObj); } } } return type.cast(lm); }
From source file:com.google.flatbuffers.Table.java
@SuppressWarnings("unchecked") @Override//from w ww . j ava 2s .c o m public <Any> Any clone(Map<String, Object> mutate) throws Exception { Object oData = null; Class<?> cls = this.getClass(); try { FlatBufferBuilder b = new FlatBufferBuilder(); int root_table = this.clone(b, mutate); b.finish(root_table); Method m = cls.getDeclaredMethod("getRootAs" + cls.getSimpleName(), new Class<?>[] { ByteBuffer.class }); oData = m.invoke(null, new Object[] { b.dataBuffer() }); } catch (Exception e) { e.printStackTrace(); throw e; } return (Any) cls.cast(oData); }
From source file:com.atlassian.jira.ComponentManager.java
/** * Returns all the components currently inside Pico which are instances of the given class, mapping them to their * component key./*from w w w. j ava2s .c o m*/ * * @param iface The class to search for * @return a map, mapping the component key, to the instances of the clas registered in JIRA's pico container. */ public static <T> Map<String, T> getComponentsOfTypeMap(final Class<T> iface) { final PicoContainer picoContainer = getInstance().getContainer(); final List<ComponentAdapter<T>> componentAdaptersOfType = picoContainer.getComponentAdapters(iface); final Map<String, T> implementations = new HashMap<String, T>(); for (final ComponentAdapter<T> componentAdapter : componentAdaptersOfType) { final T componentInstance = iface.cast(componentAdapter.getComponentInstance(picoContainer)); implementations.put(String.valueOf(componentAdapter.getComponentKey()), componentInstance); } return Collections.unmodifiableMap(implementations); }
From source file:org.openmeetings.app.data.basic.Configurationmanagement.java
/** * Return a object using a custom type and a default value if the key is not present * /* w ww.j a v a 2 s. c om*/ * Example: Integer my_key = getConfValue("my_key", Integer.class, "15"); * * @param CONF_KEY * @param typeObject * @param defaultValue * @return */ public <T> T getConfValue(String CONF_KEY, Class<T> typeObject, String defaultValue) { try { Configuration conf_reminder = getConfKey(3L, CONF_KEY); if (conf_reminder == null) { log.warn("Could not find key in configuration CONF_KEY: " + CONF_KEY); } else { // Use the custom value as default value defaultValue = conf_reminder.getConf_value(); } // Either this can be directly assigned or try to find a constructor // that handles it if (typeObject.isAssignableFrom(defaultValue.getClass())) { return typeObject.cast(defaultValue); } Constructor<T> c = typeObject.getConstructor(defaultValue.getClass()); return c.newInstance(defaultValue); } catch (Exception err) { log.error("cannot be cast to return type, you have misconfigured your configuration CONF_KEY: " + CONF_KEY, err); return null; } }
From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java
protected <T> T getXmlObject(JAXBContext context, String url, int expectedStatus, Class<T> expectedClass) throws Exception { MockHttpServletRequest request = createRequest(GET, url); MockHttpServletResponse response = createResponse(); dispatch(request, response);/*w w w.ja v a 2 s. c o m*/ assertEquals(expectedStatus, response.getStatus()); System.err.printf("xml: %s\n", response.getContentAsString()); InputStream in = new ByteArrayInputStream(response.getContentAsByteArray()); Unmarshaller unmarshaller = context.createUnmarshaller(); T result = expectedClass.cast(unmarshaller.unmarshal(in)); return result; }
From source file:biz.neustar.pc.ui.manager.impl.PersonalCloudManagerImpl.java
public <Entity> Entity handleException(ResponseData responsedata, Class<Entity> entityType) { int status = responsedata.getStatus(); Entity entity = null;/*ww w . j a v a2 s. c o m*/ if (!(status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT)) { PCloudError error; try { error = new ObjectMapper().readValue(responsedata.getBody(), PCloudError.class); throw new PCloudUIException(error.getErrorCode(), error.getErrorMessage(), responsedata.getStatus()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { try { entity = entityType.cast(new ObjectMapper().readValue(responsedata.getBody(), entityType)); } catch (Exception e) { LOGGER.info("inside exception"); e.printStackTrace(); } } return entity; }
From source file:net.camelpe.extension.camel.spi.CdiRegistry.java
/** * @see org.apache.camel.spi.Registry#lookupByType(java.lang.Class) *//* www . j av a 2 s .co m*/ @Override public <T> Map<String, T> lookupByType(final Class<T> type) { Validate.notNull(type, "type"); getLog().trace("Looking up all beans having expected type = [{}] in CDI registry ...", type.getName()); final Set<Bean<?>> beans = getDelegate().getBeans(type); if (beans.isEmpty()) { getLog().debug("Found no beans having expected type = [{}] in CDI registry.", type.getName()); return Collections.emptyMap(); } getLog().debug("Found [{}] beans having expected type = [{}] in CDI registry.", Integer.valueOf(beans.size()), type.getName()); final Map<String, T> beansByName = new HashMap<String, T>(beans.size()); final CreationalContext<?> creationalContext = getDelegate().createCreationalContext(null); for (final Bean<?> bean : beans) { beansByName.put(bean.getName(), type.cast(getDelegate().getReference(bean, type, creationalContext))); } return beansByName; }
From source file:org.cleverbus.core.AbstractOperationRouteTest.java
/** * Sends the test request to the Sync route (as if it was received via Spring WS). * * @param requestXML the request payload (XML) to send * @param responseClass {@link String}.class to get the response body as String, * or the class to unmarshal the response body to using JAXB * @return the result as the specified class * @throws Exception//from ww w . j a va 2 s .com */ protected <T> T sendSyncMessage(String requestXML, Class<T> responseClass) throws Exception { Exchange result = sendSyncMessage(requestXML); Exception exception = result.getException(); if (exception != null) { throw exception; } String responseXML = result.getOut().getMandatoryBody(String.class); if (responseClass.isAssignableFrom(String.class)) { return responseClass.cast(responseXML); } return unmarshalFragment(responseXML, responseClass); }