/** * Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s * are created for properties and types with {@link Indexed}, {@link CompoundIndexes} or {@link GeoSpatialIndexed}. * * @param entityType * @return Empty {@link Iterable} in case no {@link IndexDefinition} could be resolved for type. * @see 2.2 */ default Iterable<? extends IndexDefinition> resolveIndexFor(Class<?> entityType) { return resolveIndexFor(ClassTypeInformation.from(entityType)); }
public <S extends Object> S read(Class<S> clazz, final Bson bson) { return read(ClassTypeInformation.from(clazz), bson); }
/** * @param keyspace * @param path * @param mapValueType * @param source * @param sink */ private void writeMap(String keyspace, String path, Class<?> mapValueType, Map<?, ?> source, RedisData sink) { if (CollectionUtils.isEmpty(source)) { return; } for (Map.Entry<?, ?> entry : source.entrySet()) { if (entry.getValue() == null || entry.getKey() == null) { continue; } String currentPath = path + ".[" + mapMapKey(entry.getKey()) + "]"; if (!ClassUtils.isAssignable(mapValueType, entry.getValue().getClass())) { throw new MappingException( String.format(INVALID_TYPE_ASSIGNMENT, entry.getValue().getClass(), currentPath, mapValueType)); } if (customConversions.hasCustomWriteTarget(entry.getValue().getClass())) { writeToBucket(currentPath, entry.getValue(), sink, mapValueType); } else { writeInternal(keyspace, currentPath, entry.getValue(), ClassTypeInformation.from(mapValueType), sink); } } }
/** * Inspects the association annotations on the property and returns the target entity type if specified. * * @return */ @Nullable private TypeInformation<?> detectAssociationTargetType() { if (!isAssociation()) { return null; } for (Class<? extends Annotation> annotationType : ASSOCIATION_ANNOTATIONS) { Annotation annotation = findAnnotation(annotationType); if (annotation == null) { continue; } Object entityValue = AnnotationUtils.getValue(annotation, "targetEntity"); if (entityValue == null || entityValue.equals(void.class)) { continue; } return ClassTypeInformation.from((Class<?>) entityValue); } return null; }
private Class<?> getTypeHint(String path, Bucket bucket, Class<?> fallback) { TypeInformation<?> typeInformation = typeMapper.readType(bucket.getPropertyPath(path), ClassTypeInformation.from(fallback)); return typeInformation.getType(); }
@Override public void writeTypeRestrictions(Document result, @Nullable Set<Class<?>> restrictedTypes) { if (ObjectUtils.isEmpty(restrictedTypes)) { return; } BasicDBList restrictedMappedTypes = new BasicDBList(); for (Class<?> restrictedType : restrictedTypes) { Alias typeAlias = getAliasFor(ClassTypeInformation.from(restrictedType)); if (!ObjectUtils.nullSafeEquals(Alias.NONE, typeAlias) && typeAlias.isPresent()) { restrictedMappedTypes.add(typeAlias.getValue()); } } accessor.writeTypeTo(result, new Document("$in", restrictedMappedTypes)); }
/** * Creates a new {@link MongoParameters} instance from the given {@link Method} and {@link MongoQueryMethod}. * * @param method must not be {@literal null}. * @param queryMethod must not be {@literal null}. */ public MongoParameters(Method method, boolean isGeoNearMethod) { super(method); List<Class<?>> parameterTypes = Arrays.asList(method.getParameterTypes()); this.fullTextIndex = parameterTypes.indexOf(TextCriteria.class); ClassTypeInformation<?> declaringClassInfo = ClassTypeInformation.from(method.getDeclaringClass()); List<TypeInformation<?>> parameterTypeInfo = declaringClassInfo.getParameterTypes(method); this.rangeIndex = getTypeIndex(parameterTypeInfo, Range.class, Distance.class); this.maxDistanceIndex = this.rangeIndex == -1 ? getTypeIndex(parameterTypeInfo, Distance.class, null) : -1; int index = findNearIndexInParameters(method); if (index == -1 && isGeoNearMethod) { index = getNearIndex(parameterTypes); } this.nearIndex = index; }
private Object readCollectionOrArray(String path, Class<?> collectionType, Class<?> valueType, Bucket bucket) { List<String> keys = new ArrayList<>(bucket.extractAllKeysFor(path)); keys.sort(listKeyComparator); boolean isArray = collectionType.isArray(); Class<?> collectionTypeToUse = isArray ? ArrayList.class : collectionType; Collection<Object> target = CollectionFactory.createCollection(collectionTypeToUse, valueType, keys.size()); for (String key : keys) { if (typeMapper.isTypeKey(key)) { continue; } Bucket elementData = bucket.extract(key); TypeInformation<?> typeInformation = typeMapper.readType(elementData.getPropertyPath(key), ClassTypeInformation.from(valueType)); Class<?> typeToUse = typeInformation.getType(); if (conversionService.canConvert(byte[].class, typeToUse)) { target.add(fromBytes(elementData.get(key), typeToUse)); } else { target.add(readInternal(key, typeToUse, new RedisData(elementData))); } } return isArray ? toArray(target, collectionType, valueType) : (target.isEmpty() ? null : target); }
/** * Root entry method into write conversion. Adds a type discriminator to the {@link Document}. Shouldn't be called for * nested conversions. * * @see org.springframework.data.mongodb.core.convert.MongoWriter#write(java.lang.Object, com.mongodb.Document) */ public void write(Object obj, Bson bson) { if (null == obj) { return; } Class<?> entityType = ClassUtils.getUserClass(obj.getClass()); TypeInformation<? extends Object> type = ClassTypeInformation.from(entityType); Object target = obj instanceof LazyLoadingProxy ? ((LazyLoadingProxy) obj).getTarget() : obj; writeInternal(target, bson, type); if (asMap(bson).containsKey("_id") && asMap(bson).get("_id") == null) { removeFromMap(bson, "_id"); } if (requiresTypeHint(entityType)) { typeMapper.writeType(type, bson); } }
/** * @param path * @param mapType * @param keyType * @param valueType * @param source * @return */ @Nullable private Map<?, ?> readMapOfComplexTypes(String path, Class<?> mapType, Class<?> keyType, Class<?> valueType, RedisData source) { Set<String> keys = source.getBucket().extractAllKeysFor(path); Map<Object, Object> target = CollectionFactory.createMap(mapType, keys.size()); for (String key : keys) { Bucket partial = source.getBucket().extract(key); Object mapKey = extractMapKeyForPath(path, key, keyType); TypeInformation<?> typeInformation = typeMapper.readType(source.getBucket().getPropertyPath(key), ClassTypeInformation.from(valueType)); Object o = readInternal(key, typeInformation.getType(), new RedisData(partial)); target.put(mapKey, o); } return target.isEmpty() ? null : target; }
TypeInformation<?> readType = typeMapper.readType(source.getBucket().getPath(), ClassTypeInformation.from(type));
/** * Configures the {@link JsonSchemaProperty} to reflect the given type. * * @param type must not be {@literal null}. * @return */ public JsonSchemaProperty withType(Class<?> type) { Assert.notNull(type, "Type must not be null!"); return with(ClassTypeInformation.from(type)); }
public <S extends Object> S read(Class<S> clazz, final Bson bson) { return read(ClassTypeInformation.from(clazz), bson); }
private static ElasticsearchEntityInformation<Book, String> createElasticsearchEntityInformation() { TypeInformation<Book> typeInformation = ClassTypeInformation.from(Book.class); ElasticsearchPersistentEntity<Book> entity = new SimpleElasticsearchPersistentEntity(typeInformation); ElasticsearchEntityInformation<Book, String> information = new MappingElasticsearchEntityInformation(entity); return information; }
@SuppressWarnings("rawtypes") public TypeInformation<?> getPropertyType() { return property.map(it -> (TypeInformation) it.getTypeInformation()) .orElseGet(() -> ClassTypeInformation.from(definition.getPrimaryMember().getRawType())); }
@Autowired public PersistentEntityResourceProcessor(Repositories repositories, List<ResourceProcessor<Resource<?>>> resourceProcessors) { if (null != resourceProcessors) { for (ResourceProcessor<Resource<?>> rp : resourceProcessors) { TypeInformation<?> typeInfo = ClassTypeInformation.from(rp.getClass()); TypeInformation<?> domainType = typeInfo.getTypeArguments().get(0); if (null != repositories.getPersistentEntity(domainType.getType())) { this.resourceProcessors.add(new DomainTypeResourceProcessor(domainType.getType(), rp)); } } } }
private Optional<Pair<QuerydslPredicateExecutor<?>, Predicate>> getRepositoryAndPredicate( QuerydslPredicateExecutor<?> repository, Class<?> domainType, Map<String, String[]> parameters) { ClassTypeInformation<?> type = ClassTypeInformation.from(domainType); QuerydslBindings bindings = factory.createBindingsFor(type); Predicate predicate = predicateBuilder.getPredicate(type, toMultiValueMap(parameters), bindings); return Optional.ofNullable(predicate).map(it -> Pair.of(repository, it)); }
@Override public void writeTypeRestrictions(Document result, @Nullable Set<Class<?>> restrictedTypes) { if (ObjectUtils.isEmpty(restrictedTypes)) { return; } BasicDBList restrictedMappedTypes = new BasicDBList(); for (Class<?> restrictedType : restrictedTypes) { Alias typeAlias = getAliasFor(ClassTypeInformation.from(restrictedType)); if (!ObjectUtils.nullSafeEquals(Alias.NONE, typeAlias) && typeAlias.isPresent()) { restrictedMappedTypes.add(typeAlias.getValue()); } } accessor.writeTypeTo(result, new Document("$in", restrictedMappedTypes)); }
/** * Creates a new {@link MongoParameters} instance from the given {@link Method} and {@link MongoQueryMethod}. * * @param method must not be {@literal null}. * @param queryMethod must not be {@literal null}. */ public MongoParameters(Method method, boolean isGeoNearMethod) { super(method); List<Class<?>> parameterTypes = Arrays.asList(method.getParameterTypes()); this.fullTextIndex = parameterTypes.indexOf(TextCriteria.class); ClassTypeInformation<?> declaringClassInfo = ClassTypeInformation.from(method.getDeclaringClass()); List<TypeInformation<?>> parameterTypeInfo = declaringClassInfo.getParameterTypes(method); this.rangeIndex = getTypeIndex(parameterTypeInfo, Range.class, Distance.class); this.maxDistanceIndex = this.rangeIndex == -1 ? getTypeIndex(parameterTypeInfo, Distance.class, null) : -1; int index = findNearIndexInParameters(method); if (index == -1 && isGeoNearMethod) { index = getNearIndex(parameterTypes); } this.nearIndex = index; }