List of usage examples for org.springframework.core CollectionFactory createCollection
public static <E> Collection<E> createCollection(Class<?> collectionType, int capacity)
From source file:org.anon.smart.d2cache.store.repository.hbase.SilentObjectCreator.java
public static <T> T create(Class<T> clazz, Class<? super T> parent) { try {/* www. j av a 2 s . c o m*/ if (Collection.class.isAssignableFrom(clazz)) { return (T) CollectionFactory.createCollection(clazz, 10); } ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); Constructor objDef = parent.getDeclaredConstructor(); Constructor intConstr = rf.newConstructorForSerialization(clazz, objDef); return clazz.cast(intConstr.newInstance()); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalStateException("Cannot create object", e); } }
From source file:org.kuali.rice.krad.data.provider.impl.DataObjectWrapperBase.java
/** * Populates the property on the other side of the relationship. * * @param relationship the relationship on the wrapped data object for which to populate the inverse relationship. * @param propertyValue the value of the property. */// ww w.j av a 2 s . c o m protected void populateInverseRelationship(MetadataChild relationship, Object propertyValue) { if (propertyValue != null) { MetadataChild inverseRelationship = relationship.getInverseRelationship(); if (inverseRelationship != null) { DataObjectWrapper<?> wrappedRelationship = dataObjectService.wrap(propertyValue); if (inverseRelationship instanceof DataObjectCollection) { DataObjectCollection collectionRelationship = (DataObjectCollection) inverseRelationship; String colRelName = inverseRelationship.getName(); Collection<Object> collection = (Collection<Object>) wrappedRelationship .getPropertyValue(colRelName); if (collection == null) { // if the collection is null, let's instantiate an empty one collection = CollectionFactory .createCollection(wrappedRelationship.getPropertyType(colRelName), 1); wrappedRelationship.setPropertyValue(colRelName, collection); } collection.add(getWrappedInstance()); } } } }
From source file:org.springframework.beans.factory.support.ConstructorResolver.java
/** * Template method for resolving the specified argument which is supposed to be autowired. *//*from w w w.ja v a 2 s .c o m*/ @Nullable protected Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, TypeConverter typeConverter, boolean fallback) { Class<?> paramType = param.getParameterType(); if (InjectionPoint.class.isAssignableFrom(paramType)) { InjectionPoint injectionPoint = currentInjectionPoint.get(); if (injectionPoint == null) { throw new IllegalStateException("No current InjectionPoint available for " + param); } return injectionPoint; } try { return this.beanFactory.resolveDependency(new DependencyDescriptor(param, true), beanName, autowiredBeanNames, typeConverter); } catch (NoUniqueBeanDefinitionException ex) { throw ex; } catch (NoSuchBeanDefinitionException ex) { if (fallback) { // Single constructor or factory method -> let's return an empty array/collection // for e.g. a vararg or a non-null List/Set/Map parameter. if (paramType.isArray()) { return Array.newInstance(paramType.getComponentType(), 0); } else if (CollectionFactory.isApproximableCollectionType(paramType)) { return CollectionFactory.createCollection(paramType, 0); } else if (CollectionFactory.isApproximableMapType(paramType)) { return CollectionFactory.createMap(paramType, 0); } } throw ex; } }
From source file:org.springframework.data.document.mongodb.convert.SimpleMongoConverter.java
/** * Reads the given collection values (that are {@link DBObject}s potentially) into a {@link Collection} of domain * objects.//www. j av a 2 s .c om * * @param descriptor * @param values * @return */ private Collection<Object> readCollection(MongoPropertyDescriptor descriptor, Collection<?> values) { Class<?> targetCollectionType = descriptor.getPropertyType(); boolean targetIsArray = targetCollectionType.isArray(); @SuppressWarnings("unchecked") Collection<Object> result = targetIsArray ? new ArrayList<Object>(values.size()) : CollectionFactory.createCollection(targetCollectionType, values.size()); for (Object o : values) { if (o instanceof DBObject) { Class<?> type; if (targetIsArray) { type = targetCollectionType.getComponentType(); } else { type = getGenericParameters(descriptor.getTypeToSet()).get(0); } result.add(read(type, (DBObject) o)); } else { result.add(o); } } return result; }
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
/** * Reads the given {@link BasicDBList} into a collection of the given {@link TypeInformation}. * //from ww w . j a va 2 s . com * @param targetType must not be {@literal null}. * @param sourceValue must not be {@literal null}. * @return the converted {@link Collections}, will never be {@literal null}. */ @SuppressWarnings("unchecked") private Object readCollectionOrArray(TypeInformation<?> targetType, BasicDBList sourceValue) { Assert.notNull(targetType); Class<?> collectionType = targetType.getType(); collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class; Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>() : CollectionFactory.createCollection(collectionType, sourceValue.size()); for (int i = 0; i < sourceValue.size(); i++) { Object dbObjItem = sourceValue.get(i); if (dbObjItem instanceof DBRef) { items.add(read(targetType.getComponentType(), ((DBRef) dbObjItem).fetch())); } else if (dbObjItem instanceof DBObject) { items.add(read(targetType.getComponentType(), (DBObject) dbObjItem)); } else { TypeInformation<?> componentType = targetType.getComponentType(); items.add(getPotentiallyConvertedSimpleRead(dbObjItem, componentType == null ? null : componentType.getType())); } } return getPotentiallyConvertedSimpleRead(items, targetType.getType()); }
From source file:org.springframework.jmx.access.MBeanClientInterceptor.java
private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType, Class<?> elementType) throws NoSuchMethodException { Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType()); Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array)); for (int i = 0; i < array.length; i++) { resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i])); }/*from w w w . jav a2 s .c o m*/ return resultColl; }