private int getTypeIndex(List<TypeInformation<?>> parameterTypes, Class<?> type, @Nullable Class<?> componentType) { for (int i = 0; i < parameterTypes.size(); i++) { TypeInformation<?> candidate = parameterTypes.get(i); if (candidate.getType().equals(type)) { if (componentType == null) { return i; } else if (componentType.equals(candidate.getRequiredComponentType().getType())) { return i; } } } return -1; }
/** * Returns whether the given type is managed by one of the underlying {@link Metamodel} instances. * * @param type must not be {@literal null}. * @return */ public boolean isMetamodelManagedType(TypeInformation<?> type) { return isMetamodelManagedType(type.getType()); }
@Override public Class<?> getActualType() { return associationTargetType != null ? associationTargetType.getType() : super.getActualType(); }
private boolean isGeoNearQuery(Method method) { Class<?> returnType = method.getReturnType(); for (Class<?> type : GEO_NEAR_RESULTS) { if (type.isAssignableFrom(returnType)) { return true; } } if (Iterable.class.isAssignableFrom(returnType)) { TypeInformation<?> from = ClassTypeInformation.fromReturnTypeOf(method); return GeoResult.class.equals(from.getRequiredComponentType().getType()); } return false; }
private TypeInformation<?> getTypeHintForEntity(@Nullable Object source, MongoPersistentEntity<?> entity) { TypeInformation<?> info = entity.getTypeInformation(); Class<?> type = info.getActualType().getType(); if (source == null || type.isInterface() || java.lang.reflect.Modifier.isAbstract(type.getModifiers())) { return info; } if (source instanceof Collection) { return NESTED_DOCUMENT; } if (!type.equals(source.getClass())) { return info; } return NESTED_DOCUMENT; }
/** * Returns the {@link JpaMetamodel} for the given type. * * @param type must not be {@literal null}. * @return */ @Nullable public JpaMetamodel getMetamodel(TypeInformation<?> type) { Metamodel metamodel = getMetamodelFor(type.getType()); return metamodel == null ? null : JpaMetamodel.of(metamodel); }
private boolean isPathToJavaLangClassProperty(PropertyPath path) { if (path.getType().equals(Class.class) && path.getLeafProperty().getOwningType().getType().equals(Class.class)) { return true; } return false; }
private boolean isGeoNearQuery(Method method) { if (ReactiveWrappers.supports(method.getReturnType())) { TypeInformation<?> from = ClassTypeInformation.fromReturnTypeOf(method); return GeoResult.class.equals(from.getRequiredComponentType().getType()); } return false; }
/** * Adds custom type information to the given {@link Document} if necessary. That is if the value is not the same as * the one given. This is usually the case if you store a subtype of the actual declared type of the property. * * @param type * @param value must not be {@literal null}. * @param bson must not be {@literal null}. */ protected void addCustomTypeKeyIfNecessary(@Nullable TypeInformation<?> type, Object value, Bson bson) { Class<?> reference = type != null ? type.getActualType().getType() : Object.class; Class<?> valueType = ClassUtils.getUserClass(value.getClass()); boolean notTheSameClass = !valueType.equals(reference); if (notTheSameClass) { typeMapper.writeType(valueType, bson); } }
/** * @param keyspace * @param path * @param values * @param typeHint * @param sink */ private void writeCollection(String keyspace, String path, @Nullable Iterable<?> values, TypeInformation<?> typeHint, RedisData sink) { if (values == null) { return; } int i = 0; for (Object value : values) { if (value == null) { break; } String currentPath = path + ".[" + i + "]"; if (!ClassUtils.isAssignable(typeHint.getType(), value.getClass())) { throw new MappingException( String.format(INVALID_TYPE_ASSIGNMENT, value.getClass(), currentPath, typeHint.getType())); } if (customConversions.hasCustomWriteTarget(value.getClass())) { writeToBucket(currentPath, value, sink, typeHint.getType()); } else { writeInternal(keyspace, currentPath, value, typeHint, sink); } i++; } }
private Class<?> getTypeHint(String path, Bucket bucket, Class<?> fallback) { TypeInformation<?> typeInformation = typeMapper.readType(bucket.getPropertyPath(path), ClassTypeInformation.from(fallback)); return typeInformation.getType(); }
public ReactiveElasticsearchQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory, MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) { super(method, metadata, factory, mappingContext); this.method = method; if (hasParameterOfType(method, Pageable.class)) { TypeInformation<?> returnType = ClassTypeInformation.fromReturnTypeOf(method); boolean multiWrapper = ReactiveWrappers.isMultiValueType(returnType.getType()); boolean singleWrapperWithWrappedPageableResult = ReactiveWrappers.isSingleValueType(returnType.getType()) && (PAGE_TYPE.isAssignableFrom(returnType.getRequiredComponentType()) || SLICE_TYPE.isAssignableFrom(returnType.getRequiredComponentType())); if (singleWrapperWithWrappedPageableResult) { throw new InvalidDataAccessApiUsageException( String.format("'%s.%s' must not use sliced or paged execution. Please use Flux.buffer(size, skip).", ClassUtils.getShortName(method.getDeclaringClass()), method.getName())); } if (!multiWrapper) { throw new IllegalStateException(String.format( "Method has to use a either multi-item reactive wrapper return type or a wrapped Page/Slice type. Offending method: %s", method.toString())); } if (hasParameterOfType(method, Sort.class)) { throw new IllegalStateException(String.format("Method must not have Pageable *and* Sort parameter. " + "Use sorting capabilities on Pageble instead! Offending method: %s", method.toString())); } } }
@Nullable @SuppressWarnings("unchecked") <T> T readValue(Object value, TypeInformation<?> type, ObjectPath path) { Class<?> rawType = type.getType(); if (conversions.hasCustomReadTarget(value.getClass(), rawType)) { return (T) conversionService.convert(value, rawType); } else if (value instanceof DBRef) { return potentiallyReadOrResolveDbRef((DBRef) value, type, path, rawType); } else if (value instanceof List) { return (T) readCollectionOrArray(type, (List<Object>) value, path); } else if (value instanceof Document) { return (T) read(type, (Document) value, path); } else if (value instanceof DBObject) { return (T) read(type, (BasicDBObject) value, path); } else { return (T) getPotentiallyConvertedSimpleRead(value, rawType); } }
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); }
/** * Creates a new {@link BasicMongoPersistentEntity} with the given {@link TypeInformation}. Will default the * collection name to the entities simple type name. * * @param typeInformation must not be {@literal null}. */ public BasicMongoPersistentEntity(TypeInformation<T> typeInformation) { super(typeInformation, MongoPersistentPropertyComparator.INSTANCE); Class<?> rawType = typeInformation.getType(); String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType); if (this.isAnnotationPresent(Document.class)) { Document document = this.getRequiredAnnotation(Document.class); this.collection = StringUtils.hasText(document.collection()) ? document.collection() : fallback; this.language = StringUtils.hasText(document.language()) ? document.language() : ""; this.expression = detectExpression(document); } else { this.collection = fallback; this.language = ""; this.expression = null; } }
/** * @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; }