List of usage examples for java.lang Class cast
@SuppressWarnings("unchecked") @HotSpotIntrinsicCandidate public T cast(Object obj)
From source file:com.timtripcony.AbstractSmartDocumentModel.java
/** * Gets a field value, casting it to a specific Java class * /*from w ww . j av a2 s.c om*/ * @param name * String Item name to get value from * @param type * Class to cast the value to * @return Item value cast to relevant Java class */ public <T> T getTypedValue(final String name, final Class<T> type) { return type.cast(getValues().get(name)); }
From source file:ch.algotrader.cache.CacheManagerImpl.java
@Override public <T extends BaseEntityI> T get(Class<T> clazz, long id) { EntityCacheKey cacheKey = new EntityCacheKey(clazz, id); T entity = clazz.cast(this.entityCache.find(cacheKey, EntityCacheKey.ROOT)); // load the Entity if it is not available in the Cache if (entity == null) { // load the object from the database entity = clazz.cast(this.genericDao.get(clazz, id)); // put into the cache if (entity != null) { put(entity);/*from w w w . ja v a 2 s . co m*/ } } else { // make sure the entity is initialized if (entity instanceof Security) { ((Security) entity).accept(InitializationVisitor.INSTANCE, this); } } return entity; }
From source file:org.nuxeo.ecm.automation.client.android.AndroidAutomationClient.java
@Override public <T> T getAdapter(Object objToAdapt, Class<T> adapterType) { if (adapterType.getName().equals(NuxeoLayoutService.class.getName())) { return adapterType.cast(layoutService); } else if (adapterType.getName().equals(FileDownloader.class.getName())) { return adapterType.cast(fileDownloader); } else if (adapterType.getName().equals(FileUploader.class.getName())) { return adapterType.cast(fileUploader); } else if (adapterType.getName().equals(TransientStateManager.class.getName())) { return adapterType.cast(transientStateManager); } else if (adapterType.getName().equals(DeferredUpdateManager.class.getName())) { return adapterType.cast(deferredUpdatetManager); } else if (adapterType.getName().equals(BlobStoreManager.class.getName())) { return adapterType.cast(blobStoreManager); } else if (adapterType.getName().equals(ResponseCacheManager.class.getName())) { return adapterType.cast(responseCacheManager); } else if (adapterType.getName().equals(DocumentMessageService.class.getName())) { return adapterType.cast(messageHelper); } else if (adapterType.getName().equals(DocumentProvider.class.getName())) { return adapterType.cast(documentProvider); } else if (adapterType.getName().equals(DocumentManager.class.getName())) { return adapterType.cast(new DocumentManager((Session) objToAdapt)); }/*from www . jav a2s. c o m*/ return super.getAdapter(objToAdapt, adapterType); }
From source file:JSONWrapper.java
public <E> List<E> asList(List<E> defaultValue, Class<E> type) { if (this.value instanceof List) { List<E> values = new ArrayList<>(); for (Object obj : (List) this.value) { if (type.isInstance(obj)) { values.add(type.cast(obj)); }//from w ww . j a v a 2 s.co m } return values; } else if (this.value instanceof JSONArray) { return this.asArray().asList(defaultValue, type); } return defaultValue; }
From source file:com.spoledge.audao.db.dao.RootDaoImpl.java
protected final <T> T deserialize(byte[] bytes, Class<T> clazz) { if (bytes == null) return null; try {/* ww w . j ava 2s .com*/ ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); T ret = clazz.cast(ois.readObject()); ois.close(); return ret; } catch (Exception e) { throw new DBException(e); } }
From source file:org.neo4j.ogm.session.response.SessionResponseHandler.java
private <T> T lookup(Class<T> type, Long id) { Object ref;/*from www . ja v a 2 s.com*/ ClassInfo typeInfo = metaData.classInfo(type.getName()); if (typeInfo.annotationsInfo().get(RelationshipEntity.CLASS) == null) { ref = mappingContext.get(id); } else { ref = mappingContext.getRelationshipEntity(id); } return type.cast(ref); }
From source file:com.google.api.ads.adwords.keywordoptimizer.KeywordOptimizer.java
/** * Helper method to create an object based on a class name specified in the properties file. Note * that all classes instantiated here must provide an empty constructor. * * @param clazz the super class of the object to be created * @param property the property that has the class name as a value * @param context holding shared objects during the optimization process * @return the instantiated object/*from w w w. j ava2s . c o m*/ * @throws KeywordOptimizerException in case of a problem lading the specified object */ @SuppressWarnings(value = "unchecked") private static <Type> Type createObjectBasedOnProperty(Class<Type> clazz, KeywordOptimizerProperty property, OptimizationContext context) throws KeywordOptimizerException { String className = context.getConfiguration().getString(property.getName()); if (className == null || className.isEmpty()) { throw new KeywordOptimizerException("Mandatory property '" + property.getName() + "' is missing"); } try { Class<Type> dynamicClazz = (Class<Type>) Class.forName(className); // First try if there is a constructor expecting the optimization context. try { Constructor<Type> constructor = dynamicClazz.getConstructor(OptimizationContext.class); Type obj = constructor.newInstance(context); return clazz.cast(obj); } catch (NoSuchMethodException e) { // Ignore. } // Else use default constructor. Constructor<Type> constructor = dynamicClazz.getConstructor(); Type obj = constructor.newInstance(); return clazz.cast(obj); } catch (ReflectiveOperationException e) { throw new KeywordOptimizerException("Error constructing '" + className + "'"); } }
From source file:org.jboss.spring.facade.ControllerBeanFactory.java
/** * Get exact bean./*from w w w . ja v a2s . c om*/ * * @param name the bean name * @param clazz the expected class * @param <T> the exact type * @return exact bean * @throws BeansException for any error */ protected <T> T getBeanWithType(String name, Class<T> clazz) throws BeansException { Object result = getBean(name); if (!clazz.isInstance(result)) { throw new BeanNotOfRequiredTypeException(name, clazz, result.getClass()); } return clazz.cast(result); }
From source file:eu.udig.catalog.jgrass.core.JGTtmsGeoResource.java
public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor) throws IOException { if (adaptee == null) return null; if (adaptee.isAssignableFrom(IService.class)) { return adaptee.cast(parentService); }/*from w w w .j a v a2s .com*/ if (adaptee.isAssignableFrom(IGeoResourceInfo.class)) { return adaptee.cast(getInfo(monitor)); } if (adaptee.isAssignableFrom(File.class)) { return adaptee.cast(tmsPropertiesFile); } if (adaptee.isAssignableFrom(JGTtmsGeoResource.class)) { return adaptee.cast(this); } if (adaptee.isAssignableFrom(IGeoResource.class)) { return adaptee.cast(this); } if (adaptee.isAssignableFrom(CoordinateReferenceSystem.class)) { return adaptee.cast(tmsCrs); } // bad call to resolve return super.resolve(adaptee, monitor); }
From source file:com.cedarsoft.configuration.xml.ConfigurationAccess.java
/** * <p>Constructor for ConfigurationAccess.</p> * * @param configuration a Configuration object. * @param type a Class object.//from ww w . ja v a 2 s .c o m * @param key a String object. * @param defaultValue a T object. */ public ConfigurationAccess(@Nonnull Configuration configuration, @Nonnull Class<? extends T> type, @Nonnull String key, @Nonnull final T defaultValue) { this(configuration, type, key, new DefaultValueProvider() { @Override @Nonnull public <T> T getDefaultValue(@Nonnull String key, @Nonnull Class<T> type) { return type.cast(defaultValue); } }); }