List of usage examples for org.springframework.data.util TypeInformation getComponentType
@Nullable TypeInformation<?> getComponentType();
From source file:com.github.vanroy.springdata.jest.MappingBuilder.java
protected static Class<?> getFieldType(java.lang.reflect.Field field) { Class<?> clazz = field.getType(); TypeInformation typeInformation = ClassTypeInformation.from(clazz); if (typeInformation.isCollectionLike()) { clazz = GenericCollectionTypeResolver.getCollectionFieldType(field) != null ? GenericCollectionTypeResolver.getCollectionFieldType(field) : typeInformation.getComponentType().getType(); }/*from w ww .j ava2 s .c o m*/ return clazz; }
From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java
@SuppressWarnings("unchecked") protected <S extends Object> S read(TypeInformation<S> type, DBObject dbo) { if (null == dbo) { return null; }//from ww w .java 2 s.c o m TypeInformation<? extends S> typeToUse = getMoreConcreteTargetType(dbo, type); Class<? extends S> rawType = typeToUse.getType(); Class<?> customTarget = getCustomTarget(rawType, DBObject.class); if (customTarget != null) { return conversionService.convert(dbo, rawType); } if (typeToUse.isCollectionLike() && dbo instanceof BasicDBList) { List<Object> l = new ArrayList<Object>(); BasicDBList dbList = (BasicDBList) dbo; for (Object o : dbList) { if (o instanceof DBObject) { Object newObj = read(typeToUse.getComponentType(), (DBObject) o); Class<?> rawComponentType = typeToUse.getComponentType().getType(); if (newObj.getClass().isAssignableFrom(rawComponentType)) { l.add(newObj); } else { l.add(conversionService.convert(newObj, rawComponentType)); } } else { l.add(o); } } return conversionService.convert(l, rawType); } // Retrieve persistent entity info MongoPersistentEntity<S> persistentEntity = (MongoPersistentEntity<S>) mappingContext .getPersistentEntity(typeToUse); if (persistentEntity == null) { throw new MappingException("No mapping metadata found for " + rawType.getName()); } return read(persistentEntity, dbo); }
From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java
@SuppressWarnings({ "unchecked" }) protected void writePropertyInternal(MongoPersistentProperty prop, Object obj, DBObject dbo) { org.springframework.data.document.mongodb.mapping.DBRef dbref = prop.getField() .getAnnotation(org.springframework.data.document.mongodb.mapping.DBRef.class); String name = prop.getName(); Class<?> type = prop.getType(); if (prop.isCollection()) { BasicDBList dbList = new BasicDBList(); Collection<?> coll; if (type.isArray()) { coll = new ArrayList<Object>(); for (Object o : (Object[]) obj) { ((List<Object>) coll).add(o); }/* w ww. j ava2 s . c om*/ } else { coll = (Collection<?>) obj; } for (Object propObjItem : coll) { if (null != dbref) { DBRef dbRef = createDBRef(propObjItem, dbref); dbList.add(dbRef); } else if (type.isArray() && isSimpleType(prop.getComponentType())) { dbList.add(propObjItem); } else if (propObjItem instanceof List) { List<?> propObjColl = (List<?>) propObjItem; TypeInformation<?> typeInfo = ClassTypeInformation.from(propObjItem.getClass()); while (typeInfo.isCollectionLike()) { typeInfo = typeInfo.getComponentType(); } if (isSimpleType(typeInfo.getType())) { dbList.add(propObjColl); } else { BasicDBList propNestedDbList = new BasicDBList(); for (Object propNestedObjItem : propObjColl) { BasicDBObject propDbObj = new BasicDBObject(); writeInternal(propNestedObjItem, propDbObj); propNestedDbList.add(propDbObj); } dbList.add(propNestedDbList); } } else if (isSimpleType(propObjItem.getClass())) { dbList.add(propObjItem); } else { BasicDBObject propDbObj = new BasicDBObject(); writeInternal(propObjItem, propDbObj, mappingContext.getPersistentEntity(prop.getComponentType())); dbList.add(propDbObj); } } dbo.put(name, dbList); return; } if (null != obj && obj instanceof Map) { BasicDBObject mapDbObj = new BasicDBObject(); writeMapInternal((Map<Object, Object>) obj, mapDbObj); dbo.put(name, mapDbObj); return; } if (null != dbref) { DBRef dbRefObj = createDBRef(obj, dbref); if (null != dbRefObj) { dbo.put(name, dbRefObj); return; } } // Lookup potential custom target type Class<?> basicTargetType = getCustomTarget(obj.getClass(), null); if (basicTargetType != null) { dbo.put(name, conversionService.convert(obj, basicTargetType)); return; } BasicDBObject propDbObj = new BasicDBObject(); writeInternal(obj, propDbObj, mappingContext.getPersistentEntity(prop.getTypeInformation())); dbo.put(name, propDbObj); }
From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java
protected <T> List<?> unwrapList(BasicDBList dbList, TypeInformation<T> targetType) { List<Object> rootList = new LinkedList<Object>(); for (int i = 0; i < dbList.size(); i++) { Object obj = dbList.get(i); if (obj instanceof BasicDBList) { rootList.add(unwrapList((BasicDBList) obj, targetType.getComponentType())); } else if (obj instanceof DBObject) { rootList.add(read(targetType, (DBObject) obj)); } else {//from w w w.j av a 2s .co m rootList.add(obj); } } return rootList; }
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
/** * Populates the given {@link BasicDBList} with values from the given {@link Collection}. * // w w w.j av a 2s.c o m * @param source the collection to create a {@link BasicDBList} for, must not be {@literal null}. * @param type the {@link TypeInformation} to consider or {@literal null} if unknown. * @param sink the {@link BasicDBList} to write to. * @return */ private BasicDBList writeCollectionInternal(Collection<?> source, TypeInformation<?> type, BasicDBList sink) { TypeInformation<?> componentType = type == null ? null : type.getComponentType(); for (Object element : source) { Class<?> elementType = element == null ? null : element.getClass(); if (elementType == null || conversions.isSimpleType(elementType)) { sink.add(getPotentiallyConvertedSimpleWrite(element)); } else if (element instanceof Collection || elementType.isArray()) { sink.add(writeCollectionInternal(asCollection(element), componentType, new BasicDBList())); } else { BasicDBObject propDbObj = new BasicDBObject(); writeInternal(element, propDbObj, componentType); sink.add(propDbObj); } } return sink; }
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.jav a2 s . c om * @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.data.mongodb.core.convert.MappingMongoConverter.java
/** * Reads the given {@link DBObject} into a {@link Map}. will recursively resolve nested {@link Map}s as well. * //from www . ja v a 2 s . co m * @param type the {@link Map} {@link TypeInformation} to be used to unmarshall this {@link DBObject}. * @param dbObject * @return */ @SuppressWarnings("unchecked") protected Map<Object, Object> readMap(TypeInformation<?> type, DBObject dbObject) { Assert.notNull(dbObject); Class<?> mapType = typeMapper.readType(dbObject, type).getType(); Map<Object, Object> map = CollectionFactory.createMap(mapType, dbObject.keySet().size()); Map<String, Object> sourceMap = dbObject.toMap(); for (Entry<String, Object> entry : sourceMap.entrySet()) { if (typeMapper.isTypeKey(entry.getKey())) { continue; } Object key = potentiallyUnescapeMapKey(entry.getKey()); TypeInformation<?> keyTypeInformation = type.getComponentType(); if (keyTypeInformation != null) { Class<?> keyType = keyTypeInformation.getType(); key = conversionService.convert(key, keyType); } Object value = entry.getValue(); TypeInformation<?> valueType = type.getMapValueType(); if (value instanceof DBObject) { map.put(key, read(valueType, (DBObject) value)); } else { Class<?> valueClass = valueType == null ? null : valueType.getType(); map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass)); } } return map; }
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
protected <T> List<?> unwrapList(BasicDBList dbList, TypeInformation<T> targetType) { List<Object> rootList = new ArrayList<Object>(); for (int i = 0; i < dbList.size(); i++) { Object obj = dbList.get(i); if (obj instanceof BasicDBList) { rootList.add(unwrapList((BasicDBList) obj, targetType.getComponentType())); } else if (obj instanceof DBObject) { rootList.add(read(targetType, (DBObject) obj)); } else {/* www. j a va 2 s. co m*/ rootList.add(obj); } } return rootList; }