/** * Checks whether we have a custom conversion for the given simple object. Converts the given value if so, applies * {@link Enum} handling or returns the value as is. * * @param value * @param target must not be {@literal null}. * @return */ @Nullable @SuppressWarnings({ "rawtypes", "unchecked" }) private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullable Class<?> target) { if (value == null || target == null || ClassUtils.isAssignableValue(target, value)) { return value; } if (conversions.hasCustomReadTarget(value.getClass(), target)) { return conversionService.convert(value, target); } if (Enum.class.isAssignableFrom(target)) { return Enum.valueOf((Class<Enum>) target, value.toString()); } return conversionService.convert(value, target); }
@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); } }
Class<? extends S> rawType = typeToUse.getType(); if (conversions.hasCustomReadTarget(bson.getClass(), rawType)) { return conversionService.convert(bson, rawType);
if (customConversions.hasCustomReadTarget(Map.class, readType.getType())) {
/** * Checks whether we have a custom conversion for the given simple object. Converts the given value if so, applies * {@link Enum} handling or returns the value as is. * * @param value * @param target must not be {@literal null}. * @return */ @Nullable @SuppressWarnings({ "rawtypes", "unchecked" }) private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullable Class<?> target) { if (value == null || target == null || ClassUtils.isAssignableValue(target, value)) { return value; } if (conversions.hasCustomReadTarget(value.getClass(), target)) { return conversionService.convert(value, target); } if (Enum.class.isAssignableFrom(target)) { return Enum.valueOf((Class<Enum>) target, value.toString()); } return conversionService.convert(value, target); }
@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); } }
Class<? extends S> rawType = typeToUse.getType(); if (conversions.hasCustomReadTarget(bson.getClass(), rawType)) { return conversionService.convert(bson, rawType);
/** * @param sourceType * @param targetType * @return true if custom read target defined in {@link #customConversions} */ protected boolean hasCustomReadTarget(Class<?> sourceType, Class<?> targetType) { return this.customConversions.hasCustomReadTarget(sourceType, targetType); }
/** * Potentially convert simple values like ENUMs. * * @param value the value to convert. * @param target the target object. * @return the potentially converted object. */ @SuppressWarnings("unchecked") private Object getPotentiallyConvertedSimpleRead(final Object value, final Class<?> target) { if (value == null || target == null) { return value; } if (conversions.hasCustomReadTarget(value.getClass(), target)) { return conversionService.convert(value, target); } if (Enum.class.isAssignableFrom(target)) { return Enum.valueOf((Class<Enum>) target, value.toString()); } if (Class.class.isAssignableFrom(target)) { try { return Class.forName(value.toString()); } catch (ClassNotFoundException e) { throw new MappingException("Unable to create class from " + value.toString()); } } return target.isAssignableFrom(value.getClass()) ? value : conversionService.convert(value, target); }
/** * Potentially convert simple values like ENUMs. * * @param value the value to convert. * @param target the target object. * @return the potentially converted object. */ @SuppressWarnings("unchecked") private Object getPotentiallyConvertedSimpleRead(final Object value, final Class<?> target) { if (value == null || target == null) { return value; } if (conversions.hasCustomReadTarget(value.getClass(), target)) { return conversionService.convert(value, target); } if (Enum.class.isAssignableFrom(target)) { return Enum.valueOf((Class<Enum>) target, value.toString()); } if (Class.class.isAssignableFrom(target)) { try { return Class.forName(value.toString()); } catch (ClassNotFoundException e) { throw new MappingException("Unable to create class from " + value.toString()); } } return target.isAssignableFrom(value.getClass()) ? value : conversionService.convert(value, target); }
/** * Checks whether we have a custom conversion for the given simple object. Converts the given value if so, applies * {@link Enum} handling or returns the value as is. * * @param value simple value to convert into a value of type {@code target}. * @param target must not be {@literal null}. * @return the converted value. */ @Nullable @SuppressWarnings({ "rawtypes", "unchecked" }) private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullable Class<?> target) { if (value == null || target == null || target.isAssignableFrom(value.getClass())) { return value; } if (getCustomConversions().hasCustomReadTarget(value.getClass(), target)) { return getConversionService().convert(value, target); } if (Enum.class.isAssignableFrom(target)) { return Enum.valueOf((Class<Enum>) target, value.toString()); } return getConversionService().convert(value, target); }
@Override @Nullable public Object readValue(@Nullable Object value, TypeInformation<?> type) { if (null == value) { return null; } if (conversions.hasCustomReadTarget(value.getClass(), type.getType())) { return conversionService.convert(value, type.getType()); } return getPotentiallyConvertedSimpleRead(value, type.getType()); }
@Override @Nullable public Object readValue(@Nullable Object value, TypeInformation<?> type) { if (null == value) { return null; } if (conversions.hasCustomReadTarget(value.getClass(), type.getType())) { return conversionService.convert(value, type.getType()); } return getPotentiallyConvertedSimpleRead(value, type.getType()); }
@Override @Nullable public Object readValue(@Nullable Object value, TypeInformation<?> type) { if (null == value) { return null; } if (conversions.hasCustomReadTarget(value.getClass(), type.getType())) { return conversionService.convert(value, type.getType()); } return getPotentiallyConvertedSimpleRead(value, type.getType()); }
@Nullable @SuppressWarnings("unchecked") private <T> T readValue(Object value, TypeInformation<?> type) { Class<?> rawType = type.getType(); if (conversions.hasCustomReadTarget(value.getClass(), rawType)) { return (T) conversionService.convert(value, rawType); } else if (value instanceof List) { return (T) readCollectionOrArray(type, (List) value); } else if (value instanceof Map) { return (T) read(type, (Map) value); } else { return (T) getPotentiallyConvertedSimpleRead(value, rawType); } }
@Override @Nullable public Object readValue(@Nullable Object value, TypeInformation<?> type) { if (null == value) { return null; } if (getConversions().hasCustomReadTarget(value.getClass(), type.getType())) { return getConversionService().convert(value, type.getType()); } if (AggregateReference.class.isAssignableFrom(type.getType())) { TypeInformation<?> idType = type.getSuperTypeInformation(AggregateReference.class).getTypeArguments().get(1); return AggregateReference.to(readValue(value, idType)); } return super.readValue(value, type); }
/** * Helper method to read the value based on the value type. * * @param value the value to convert. * @param type the type information. * @param parent the optional parent. * @param <R> the target type. * @return the converted object. */ @SuppressWarnings("unchecked") private <R> R readValue(Object value, TypeInformation<?> type, Object parent) { Class<?> rawType = type.getType(); if (conversions.hasCustomReadTarget(value.getClass(), rawType)) { return (R) conversionService.convert(value, rawType); } else if (value instanceof CouchbaseDocument) { return (R) read(type, (CouchbaseDocument) value, parent); } else if (value instanceof CouchbaseList) { return (R) readCollection(type, (CouchbaseList) value, parent); } else { return (R) getPotentiallyConvertedSimpleRead(value, rawType); } }
/** * Helper method to read the value based on the value type. * * @param value the value to convert. * @param type the type information. * @param parent the optional parent. * @param <R> the target type. * @return the converted object. */ @SuppressWarnings("unchecked") private <R> R readValue(Object value, TypeInformation<?> type, Object parent) { Class<?> rawType = type.getType(); if (conversions.hasCustomReadTarget(value.getClass(), rawType)) { return (R) conversionService.convert(value, rawType); } else if (value instanceof CouchbaseDocument) { return (R) read(type, (CouchbaseDocument) value, parent); } else if (value instanceof CouchbaseList) { return (R) readCollection(type, (CouchbaseList) value, parent); } else { return (R) getPotentiallyConvertedSimpleRead(value, rawType); } }
/** * Read an incoming {@link CouchbaseDocument} into the target entity. * * @param type the type information of the target entity. * @param source the document to convert. * @param parent an optional parent object. * @param <R> the entity type. * @return the converted entity. */ @SuppressWarnings("unchecked") protected <R> R read(final TypeInformation<R> type, final CouchbaseDocument source, final Object parent) { if (source == null) { return null; } TypeInformation<? extends R> typeToUse = typeMapper.readType(source, type); Class<? extends R> rawType = typeToUse.getType(); if (conversions.hasCustomReadTarget(source.getClass(), rawType)) { return conversionService.convert(source, rawType); } if (typeToUse.isMap()) { return (R) readMap(typeToUse, source, parent); } CouchbasePersistentEntity<R> entity = (CouchbasePersistentEntity<R>) mappingContext.getRequiredPersistentEntity(typeToUse); return read(entity, source, parent); }
/** * Read an incoming {@link CouchbaseDocument} into the target entity. * * @param type the type information of the target entity. * @param source the document to convert. * @param parent an optional parent object. * @param <R> the entity type. * @return the converted entity. */ @SuppressWarnings("unchecked") protected <R> R read(final TypeInformation<R> type, final CouchbaseDocument source, final Object parent) { if (source == null) { return null; } TypeInformation<? extends R> typeToUse = typeMapper.readType(source, type); Class<? extends R> rawType = typeToUse.getType(); if (conversions.hasCustomReadTarget(source.getClass(), rawType)) { return conversionService.convert(source, rawType); } if (typeToUse.isMap()) { return (R) readMap(typeToUse, source, parent); } CouchbasePersistentEntity<R> entity = (CouchbasePersistentEntity<R>) mappingContext.getRequiredPersistentEntity(typeToUse); return read(entity, source, parent); }