diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 544c2f234..e3d187097 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -250,7 +250,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Bson source, DefaultSpELExpressionEvaluator evaluator, ObjectPath path) { MongoDbPropertyValueProvider provider = new MongoDbPropertyValueProvider(source, evaluator, path); - PersistentEntityParameterValueProvider parameterProvider = new PersistentEntityParameterValueProvider( + PersistentEntityParameterValueProvider parameterProvider = new PersistentEntityParameterValueProvider<>( entity, provider, path.getCurrentObject()); return new ConverterAwareSpELExpressionParameterValueProvider(evaluator, conversionService, parameterProvider, @@ -273,7 +273,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App DocumentAccessor documentAccessor = new DocumentAccessor(bson); // make sure id property is set before all other properties - Optional idValue = idProperty.filter(it -> documentAccessor.hasValue(it)).map(it -> { + Optional idValue = idProperty.filter(documentAccessor::hasValue).map(it -> { Optional value = getValueInternal(it, bson, evaluator, path); accessor.setProperty(it, value); @@ -285,42 +285,38 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App idValue.isPresent() ? idProperty.map(it -> bson.get(it.getFieldName())).orElse(null) : null); // Set properties not already set in the constructor - entity.doWithProperties(new PropertyHandler() { - public void doWithPersistentProperty(MongoPersistentProperty prop) { + entity.doWithProperties((PropertyHandler) prop -> { - // we skip the id property since it was already set - if (idProperty != null && idProperty.equals(prop)) { - return; - } - - if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) { - return; - } - - accessor.setProperty(prop, getValueInternal(prop, bson, evaluator, currentPath)); + // we skip the id property since it was already set + if (idProperty != null && idProperty.equals(prop)) { + return; } + + if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) { + return; + } + + accessor.setProperty(prop, getValueInternal(prop, bson, evaluator, currentPath)); }); // Handle associations - entity.doWithAssociations(new AssociationHandler() { - public void doWithAssociation(Association association) { + entity.doWithAssociations((AssociationHandler) association -> { - final MongoPersistentProperty property = association.getInverse(); - Object value = documentAccessor.get(property); + final MongoPersistentProperty property = association.getInverse(); + Object value = documentAccessor.get(property); - if (value == null || entity.isConstructorArgument(property)) { - return; - } - - DBRef dbref = value instanceof DBRef ? (DBRef) value : null; - - DbRefProxyHandler handler = new DefaultDbRefProxyHandler(spELContext, mappingContext, - MappingMongoConverter.this); - DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, - MappingMongoConverter.this); - - accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler)); + if (value == null || entity.isConstructorArgument(property)) { + return; } + + DBRef dbref = value instanceof DBRef ? (DBRef) value : null; + + DbRefProxyHandler handler = new DefaultDbRefProxyHandler(spELContext, mappingContext, + MappingMongoConverter.this); + DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, + MappingMongoConverter.this); + + accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler)); }); return result; @@ -430,31 +426,26 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App prop -> dbObjectAccessor.computeIfAbsent(prop, () -> idMapper.convertId(accessor.getProperty(prop)))); // Write the properties - entity.doWithProperties(new PropertyHandler() { - public void doWithPersistentProperty(MongoPersistentProperty prop) { + entity.doWithProperties((PropertyHandler) prop -> { - if (idProperty.map(it -> it.equals(prop)).orElse(false) || !prop.isWritable()) { - return; - } - - accessor.getProperty(prop).ifPresent(it -> { - if (!conversions.isSimpleType(it.getClass())) { - - writePropertyInternal(it, bson, prop); - } else { - writeSimpleInternal(it, bson, prop); - } - }); + if (idProperty.map(it -> it.equals(prop)).orElse(false) || !prop.isWritable()) { + return; } + + accessor.getProperty(prop).ifPresent(it -> { + if (!conversions.isSimpleType(it.getClass())) { + + writePropertyInternal(it, bson, prop); + } else { + writeSimpleInternal(it, bson, prop); + } + }); }); - entity.doWithAssociations(new AssociationHandler() { + entity.doWithAssociations((AssociationHandler) association -> { - public void doWithAssociation(Association association) { - - MongoPersistentProperty inverseProp = association.getInverse(); - accessor.getProperty(inverseProp).ifPresent(it -> writePropertyInternal(it, bson, inverseProp)); - } + MongoPersistentProperty inverseProp = association.getInverse(); + accessor.getProperty(inverseProp).ifPresent(it -> writePropertyInternal(it, bson, inverseProp)); }); } @@ -562,7 +553,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App return writeCollectionInternal(collection, Optional.of(property.getTypeInformation()), new BasicDBList()); } - List dbList = new ArrayList(); + List dbList = new ArrayList<>(collection.size()); for (Object element : collection) { @@ -624,7 +615,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App private BasicDBList writeCollectionInternal(Collection source, Optional> type, BasicDBList sink) { - Optional> componentType = type.flatMap(it -> it.getComponentType()); + Optional> componentType = type.flatMap(TypeInformation::getComponentType); for (Object element : source) { @@ -758,7 +749,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App */ protected void addCustomTypeKeyIfNecessary(Optional> type, Object value, Bson bson) { - Optional> actualType = type.map(it -> it.getActualType()).map(it -> it.getType()); + Optional> actualType = type.map(TypeInformation::getActualType).map(TypeInformation::getType); Class reference = actualType.orElse(Object.class); Class valueType = ClassUtils.getUserClass(value.getClass()); @@ -904,7 +895,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Class rawComponentType = componentType.getType(); collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class; - Collection items = targetType.getType().isArray() ? new ArrayList() + Collection items = targetType.getType().isArray() ? new ArrayList<>(sourceValue.size()) : CollectionFactory.createCollection(collectionType, rawComponentType, sourceValue.size()); if (sourceValue.isEmpty()) { @@ -964,8 +955,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Class mapType = typeMapper.readType(bson, type).getType(); Optional> valueType = type.getMapValueType(); - Class rawKeyType = type.getComponentType().map(it -> it.getType()).orElse(null); - Class rawValueType = type.getMapValueType().map(it -> it.getType()).orElse(null); + Class rawKeyType = type.getComponentType().map(TypeInformation::getType).orElse(null); + Class rawValueType = type.getMapValueType().map(TypeInformation::getType).orElse(null); Map sourceMap = asMap(bson); Map map = CollectionFactory.createMap(mapType, rawKeyType, sourceMap.keySet().size()); @@ -1008,7 +999,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App } @SuppressWarnings("unchecked") - private Map asMap(Bson bson) { + private static Map asMap(Bson bson) { if (bson instanceof Document) { return (Document) bson; @@ -1022,7 +1013,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App String.format("Cannot read %s. as map. Given Bson must be a Document or DBObject!", bson.getClass())); } - private void addToMap(Bson bson, String key, Object value) { + private static void addToMap(Bson bson, String key, Object value) { if (bson instanceof Document) { ((Document) bson).put(key, value); @@ -1037,7 +1028,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App } @SuppressWarnings("unchecked") - private void addAllToMap(Bson bson, Map value) { + private static void addAllToMap(Bson bson, Map value) { if (bson instanceof Document) { ((Document) bson).putAll(value); @@ -1053,7 +1044,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App String.format("Cannot add all to %s. Given Bson must be a Document or DBObject.", bson.getClass())); } - private void removeFromMap(Bson bson, String key) { + private static void removeFromMap(Bson bson, String key) { if (bson instanceof Document) { ((Document) bson).remove(key); @@ -1120,7 +1111,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App if (obj instanceof Map) { - Map converted = new LinkedHashMap(); + Map converted = new LinkedHashMap<>(((Map)obj).size(), 1); Document result = new Document(); for (Map.Entry entry : ((Map) obj).entrySet()) { @@ -1254,7 +1245,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App return Optional .ofNullable(property.getSpelExpression()// - .map(it -> evaluator.evaluate(it))// + .map(evaluator::evaluate)// .orElseGet(() -> source.get(property)))// .map(it -> readValue(it, property.getTypeInformation(), path)); } @@ -1337,7 +1328,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App private void bulkReadAndConvertDBRefMapIntoTarget(TypeInformation valueType, Class rawValueType, Map sourceMap, Map targetMap) { - LinkedHashMap referenceMap = new LinkedHashMap(sourceMap); + LinkedHashMap referenceMap = new LinkedHashMap<>(sourceMap); List convertedObjects = bulkReadAndConvertDBRefs((List) new ArrayList(referenceMap.values()), valueType, ObjectPath.ROOT, rawValueType); int index = 0; @@ -1360,19 +1351,19 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App ? Collections.singletonList(readRef(dbrefs.iterator().next())) : bulkReadRefs(dbrefs); String collectionName = dbrefs.iterator().next().getCollectionName(); - List targeList = new ArrayList(dbrefs.size()); + List targeList = new ArrayList<>(dbrefs.size()); for (Document document : referencedRawDocuments) { if (document != null) { - maybeEmitEvent(new AfterLoadEvent(document, (Class) rawType, collectionName)); + maybeEmitEvent(new AfterLoadEvent<>(document, (Class) rawType, collectionName)); } final T target = (T) read(type, document, path); targeList.add(target); if (target != null) { - maybeEmitEvent(new AfterConvertEvent(document, target, collectionName)); + maybeEmitEvent(new AfterConvertEvent<>(document, target, collectionName)); } } @@ -1421,7 +1412,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Assert.notNull(source, "Iterable of DBRefs must not be null!"); - Set collectionsFound = new HashSet(); + Set collectionsFound = new HashSet<>(); for (Object dbObjItem : source) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java index 1ec87d6ee..fd0a36113 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java @@ -24,6 +24,7 @@ import java.io.Serializable; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -40,7 +41,6 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; - import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; @@ -95,7 +95,7 @@ public class DbRefMappingMongoConverterUnitTests { person.id = "foo"; DBRef dbRef = converter.toDBRef(person, null); - assertThat(dbRef.getId(), is((Object) "foo")); + assertThat(dbRef.getId(), is("foo")); assertThat(dbRef.getCollectionName(), is("person")); } @@ -127,7 +127,7 @@ public class DbRefMappingMongoConverterUnitTests { MapDBRefVal val = new MapDBRefVal(); val.id = BigInteger.ONE; - Map mapVal = new HashMap(); + Map mapVal = new HashMap<>(); mapVal.put("test", val); mapDBRef.map = mapVal; @@ -156,7 +156,7 @@ public class DbRefMappingMongoConverterUnitTests { person.id = "foo"; DBRef dbRef = converter.toDBRef(person, property); - assertThat(dbRef.getId(), is((Object) "foo")); + assertThat(dbRef.getId(), is("foo")); assertThat(dbRef.getCollectionName(), is("person")); } @@ -166,11 +166,11 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToInterface = new LinkedList(Arrays.asList(new LazyDbRefTarget("1"))); + lazyDbRefs.dbRefToInterface = new LinkedList<>(Collections.singletonList(new LazyDbRefTarget("1"))); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -187,12 +187,11 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteCollection = new ArrayList( - Arrays.asList(new LazyDbRefTarget(id, value))); + lazyDbRefs.dbRefToConcreteCollection = new ArrayList<>(Collections.singletonList(new LazyDbRefTarget(id, value))); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -209,7 +208,7 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); @@ -230,12 +229,11 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructor = new LazyDbRefTargetWithPeristenceConstructor((Object) id, - (Object) value); + lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructor = new LazyDbRefTargetWithPeristenceConstructor(id, value); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -252,12 +250,12 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor = new LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor( - (Object) id, (Object) value); + id, value); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -274,7 +272,7 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); SerializableClassWithLazyDbRefs lazyDbRefs = new SerializableClassWithLazyDbRefs(); @@ -296,7 +294,7 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); WithObjectMethodOverrideLazyDbRefs lazyDbRefs = new WithObjectMethodOverrideLazyDbRefs(); @@ -317,7 +315,7 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); WithObjectMethodOverrideLazyDbRefs lazyDbRefs = new WithObjectMethodOverrideLazyDbRefs(); @@ -393,7 +391,7 @@ public class DbRefMappingMongoConverterUnitTests { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); WithObjectMethodOverrideLazyDbRefs lazyDbRefs = new WithObjectMethodOverrideLazyDbRefs(); @@ -537,11 +535,11 @@ public class DbRefMappingMongoConverterUnitTests { MappingMongoConverter converterSpy = spy(converter); doReturn( Arrays.asList(new Document("_id", id1).append("value", value), new Document("_id", id2).append("value", value))) - .when(converterSpy).bulkReadRefs(anyListOf(DBRef.class)); + .when(converterSpy).bulkReadRefs(anyList()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteCollection = new ArrayList( + lazyDbRefs.dbRefToConcreteCollection = new ArrayList<>( Arrays.asList(new LazyDbRefTarget(id1, value), new LazyDbRefTarget(id2, value))); converterSpy.write(lazyDbRefs, document); @@ -565,7 +563,7 @@ public class DbRefMappingMongoConverterUnitTests { MappingMongoConverter converterSpy = spy(converter); doReturn( Arrays.asList(new Document("_id", id1).append("value", value), new Document("_id", id2).append("value", value))) - .when(converterSpy).bulkReadRefs(any()); + .when(converterSpy).bulkReadRefs(anyList()); Document document = new Document("dbRefToInterface", Arrays.asList(new DBRef("lazyDbRefTarget", "1"), new DBRef("lazyDbRefTarget", "2"))); @@ -590,7 +588,7 @@ public class DbRefMappingMongoConverterUnitTests { Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteCollection = new ArrayList( + lazyDbRefs.dbRefToConcreteCollection = new ArrayList<>( Arrays.asList(new LazyDbRefTarget(id1, value), new SerializableLazyDbRefTarget(id2, value))); converterSpy.write(lazyDbRefs, document); @@ -602,7 +600,7 @@ public class DbRefMappingMongoConverterUnitTests { assertThat(result.dbRefToConcreteCollection.get(1).getId(), is(id2)); verify(converterSpy, times(2)).readRef(Mockito.any(DBRef.class)); - verify(converterSpy, never()).bulkReadRefs(anyListOf(DBRef.class)); + verify(converterSpy, never()).bulkReadRefs(anyList()); } @Test // DATAMONGO-1194 @@ -616,11 +614,11 @@ public class DbRefMappingMongoConverterUnitTests { MappingMongoConverter converterSpy = spy(converter); doReturn(Arrays.asList(new Document("_id", val1.id), new Document("_id", val2.id))).when(converterSpy) - .bulkReadRefs(anyListOf(DBRef.class)); + .bulkReadRefs(anyList()); Document document = new Document(); MapDBRef mapDBRef = new MapDBRef(); - mapDBRef.map = new LinkedHashMap(); + mapDBRef.map = new LinkedHashMap<>(); mapDBRef.map.put("one", val1); mapDBRef.map.put("two", val2); @@ -633,7 +631,7 @@ public class DbRefMappingMongoConverterUnitTests { // assertProxyIsResolved(result.map, true); assertThat(result.map.get("two").id, is(val2.id)); - verify(converterSpy, times(1)).bulkReadRefs(anyListOf(DBRef.class)); + verify(converterSpy, times(1)).bulkReadRefs(anyList()); verify(converterSpy, never()).readRef(Mockito.any(DBRef.class)); } @@ -648,11 +646,11 @@ public class DbRefMappingMongoConverterUnitTests { MappingMongoConverter converterSpy = spy(converter); doReturn(Arrays.asList(new Document("_id", val1.id), new Document("_id", val2.id))).when(converterSpy) - .bulkReadRefs(anyListOf(DBRef.class)); + .bulkReadRefs(anyList()); Document document = new Document(); MapDBRef mapDBRef = new MapDBRef(); - mapDBRef.lazyMap = new LinkedHashMap(); + mapDBRef.lazyMap = new LinkedHashMap<>(); mapDBRef.lazyMap.put("one", val1); mapDBRef.lazyMap.put("two", val2); @@ -665,8 +663,8 @@ public class DbRefMappingMongoConverterUnitTests { assertProxyIsResolved(result.lazyMap, true); assertThat(result.lazyMap.get("two").id, is(val2.id)); - verify(converterSpy, times(1)).bulkReadRefs(anyListOf(DBRef.class)); - verify(converterSpy, never()).readRef(Mockito.any(DBRef.class)); + verify(converterSpy, times(1)).bulkReadRefs(anyList()); + verify(converterSpy, never()).readRef(any()); } private Object transport(Object result) {