Skip to content

Commit 448f44b

Browse files
start fixing it
1 parent 74e8097 commit 448f44b

16 files changed

+86
-55
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoTransactionOptions.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ default MongoTransactionOptions mergeWith(@Nullable MongoTransactionOptions fall
9292
: fallbackOptions.getReadConcern();
9393
}
9494

95-
@Nullable
9695
@Override
97-
public ReadPreference getReadPreference() {
96+
public @Nullable ReadPreference getReadPreference() {
9897
return MongoTransactionOptions.this.hasReadPreference() ? MongoTransactionOptions.this.getReadPreference()
9998
: fallbackOptions.getReadPreference();
10099
}
@@ -121,8 +120,8 @@ default <T> T map(Function<MongoTransactionOptions, T> mappingFunction) {
121120
* @return MongoDB driver native {@link TransactionOptions}.
122121
* @see MongoTransactionOptions#map(Function)
123122
*/
124-
@Nullable
125-
default TransactionOptions toDriverOptions() {
123+
@SuppressWarnings("NullAway")
124+
default @Nullable TransactionOptions toDriverOptions() {
126125

127126
return map(it -> {
128127

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/SessionAwareMethodInterceptor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public <T> SessionAwareMethodInterceptor(ClientSession session, T target, Class<
101101
if (requiresDecoration(methodInvocation.getMethod())) {
102102

103103
Object target = methodInvocation.proceed();
104+
Assert.notNull(target, "invocation target was null");
104105
if (target instanceof Proxy) {
105106
return target;
106107
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/SimpleMongoTransactionOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ class SimpleMongoTransactionOptions implements MongoTransactionOptions {
4141
static final Set<String> KNOWN_KEYS = Arrays.stream(OptionKey.values()).map(OptionKey::getKey)
4242
.collect(Collectors.toSet());
4343

44-
private final Duration maxCommitTime;
45-
private final ReadConcern readConcern;
46-
private final ReadPreference readPreference;
47-
private final WriteConcern writeConcern;
44+
private final @Nullable Duration maxCommitTime;
45+
private final @Nullable ReadConcern readConcern;
46+
private final @Nullable ReadPreference readPreference;
47+
private final @Nullable WriteConcern writeConcern;
4848

4949
static SimpleMongoTransactionOptions of(Map<String, String> options) {
5050
return new SimpleMongoTransactionOptions(options);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoConfigurationSupport.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter;
3636
import org.springframework.data.mongodb.core.mapping.Document;
3737
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
38+
import org.springframework.util.Assert;
3839
import org.springframework.util.ClassUtils;
3940
import org.springframework.util.StringUtils;
4041

@@ -172,8 +173,11 @@ protected Set<Class<?>> scanForEntities(String basePackage) throws ClassNotFound
172173

173174
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
174175

176+
String beanClassName = candidate.getBeanClassName();
177+
Assert.notNull(beanClassName, "BeanClassName cannot be null");
178+
175179
initialEntitySet
176-
.add(ClassUtils.forName(candidate.getBeanClassName(), MongoConfigurationSupport.class.getClassLoader()));
180+
.add(ClassUtils.forName(beanClassName, MongoConfigurationSupport.class.getClassLoader()));
177181
}
178182
}
179183

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ default SessionScoped withSession(Supplier<ClientSession> sessionProvider) {
194194
private @Nullable ClientSession session;
195195

196196
@Override
197-
public <T> T execute(SessionCallback<T> action, Consumer<ClientSession> onComplete) {
197+
@SuppressWarnings("NullAway")
198+
public <T> @Nullable T execute(SessionCallback<T> action, Consumer<ClientSession> onComplete) {
198199

199200
lock.executeWithoutResult(() -> {
200201

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,9 +3309,9 @@ private class ProjectingReadCallback<S, T> implements DocumentCallback<T> {
33093309
@SuppressWarnings("unchecked")
33103310
public T doWith(Document document) {
33113311

3312-
if (document == null) {
3313-
return null;
3314-
}
3312+
// if (document == null) {
3313+
// return null;
3314+
// }
33153315

33163316
maybeEmitEvent(new AfterLoadEvent<>(document, projection.getMappedType().getType(), collectionName));
33173317

@@ -3554,8 +3554,6 @@ public void close() {
35543554
throw potentiallyConvertRuntimeException(ex, exceptionTranslator);
35553555
} finally {
35563556
cursor = null;
3557-
exceptionTranslator = null;
3558-
objectReadCallback = null;
35593557
}
35603558
}
35613559
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/LazyLoadingProxyFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public static class LazyLoadingInterceptor
191191
* @return a {@link LazyLoadingInterceptor} that just continues with the invocation.
192192
* @since 4.0
193193
*/
194+
@SuppressWarnings("NullAway")
194195
public static LazyLoadingInterceptor none() {
195196

196197
return new LazyLoadingInterceptor(null, null, null, null) {
@@ -223,7 +224,7 @@ public LazyLoadingInterceptor(MongoPersistentProperty property, DbRefResolverCal
223224
}
224225

225226
@Override
226-
public @Nullable Object intercept(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable {
227+
public @Nullable Object intercept(Object o, Method method, Object @Nullable[] args, @Nullable MethodProxy proxy) throws Throwable {
227228

228229
if (INITIALIZE_METHOD.equals(method)) {
229230
return ensureResolved();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.core.env.StandardEnvironment;
5454
import org.springframework.data.annotation.Reference;
5555
import org.springframework.data.convert.CustomConversions;
56+
import org.springframework.data.convert.PropertyValueConversions;
5657
import org.springframework.data.convert.PropertyValueConverter;
5758
import org.springframework.data.convert.TypeMapper;
5859
import org.springframework.data.convert.ValueConversionContext;
@@ -156,7 +157,7 @@ public class MappingMongoConverter extends AbstractMongoConverter
156157

157158
protected @Nullable ApplicationContext applicationContext;
158159
protected @Nullable Environment environment;
159-
protected MongoTypeMapper typeMapper;
160+
protected @Nullable MongoTypeMapper typeMapper;
160161
protected @Nullable String mapKeyDotReplacement = null;
161162
protected @Nullable CodecRegistryProvider codecRegistryProvider;
162163

@@ -306,7 +307,9 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
306307
this.environment = applicationContext.getEnvironment();
307308
this.spELContext = new SpELContext(this.spELContext, applicationContext);
308309
this.projectionFactory.setBeanFactory(applicationContext);
309-
this.projectionFactory.setBeanClassLoader(applicationContext.getClassLoader());
310+
if(applicationContext.getClassLoader() != null) {
311+
this.projectionFactory.setBeanClassLoader(applicationContext.getClassLoader());
312+
}
310313

311314
if (entityCallbacks == null) {
312315
setEntityCallbacks(EntityCallbacks.create(applicationContext));
@@ -432,12 +435,12 @@ static class MapPersistentPropertyAccessor implements PersistentPropertyAccessor
432435
Map<String, Object> map = new LinkedHashMap<>();
433436

434437
@Override
435-
public void setProperty(PersistentProperty<?> persistentProperty, Object o) {
438+
public void setProperty(PersistentProperty<?> persistentProperty, @Nullable Object o) {
436439
map.put(persistentProperty.getName(), o);
437440
}
438441

439442
@Override
440-
public Object getProperty(PersistentProperty<?> persistentProperty) {
443+
public @Nullable Object getProperty(PersistentProperty<?> persistentProperty) {
441444
return map.get(persistentProperty.getName());
442445
}
443446

@@ -544,7 +547,7 @@ public EvaluatingDocumentAccessor(Bson document) {
544547
}
545548

546549
@Override
547-
public <T> T evaluate(String expression) {
550+
public <T> @Nullable T evaluate(String expression) {
548551
return expressionEvaluatorFactory.create(getDocument()).evaluate(expression);
549552
}
550553
}
@@ -742,8 +745,7 @@ && peek(collection) instanceof Document) {
742745
}
743746
}
744747

745-
@Nullable
746-
private Object readUnwrapped(ConversionContext context, DocumentAccessor documentAccessor,
748+
private @Nullable Object readUnwrapped(ConversionContext context, DocumentAccessor documentAccessor,
747749
MongoPersistentProperty prop, MongoPersistentEntity<?> unwrappedEntity) {
748750

749751
if (prop.findAnnotation(Unwrapped.class).onEmpty().equals(OnEmpty.USE_EMPTY)) {
@@ -765,7 +767,7 @@ public DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referringP
765767

766768
if (referringProperty != null) {
767769
annotation = referringProperty.getDBRef();
768-
Assert.isTrue(annotation != null, "The referenced property has to be mapped with @DBRef");
770+
Assert.notNull(annotation, "The referenced property has to be mapped with @DBRef");
769771
}
770772

771773
// DATAMONGO-913
@@ -1339,8 +1341,13 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersist
13391341
return (T) persistentPropertyAccessor.getProperty(property);
13401342
}
13411343
}, property, this, spELContext);
1342-
PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter = conversions
1343-
.getPropertyValueConversions().getValueConverter(property);
1344+
1345+
PropertyValueConversions propertyValueConversions = conversions.getPropertyValueConversions();
1346+
if(propertyValueConversions == null) {
1347+
return value;
1348+
}
1349+
1350+
PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter = propertyValueConversions.getValueConverter(property);
13441351
return value != null ? valueConverter.write(value, context) : valueConverter.writeNull(context);
13451352
}
13461353

@@ -2341,15 +2348,15 @@ protected static class DefaultConversionContext implements ConversionContext {
23412348
final ObjectPath path;
23422349
final ContainerValueConverter<Bson> documentConverter;
23432350
final ContainerValueConverter<Collection<?>> collectionConverter;
2344-
final ContainerValueConverter<Bson> mapConverter;
2345-
final ContainerValueConverter<DBRef> dbRefConverter;
2346-
final ValueConverter<Object> elementConverter;
2351+
final ContainerValueConverter<@Nullable Bson> mapConverter;
2352+
final ContainerValueConverter<@Nullable DBRef> dbRefConverter;
2353+
final ValueConverter<@Nullable Object> elementConverter;
23472354

23482355
DefaultConversionContext(MongoConverter sourceConverter,
23492356
org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
2350-
ContainerValueConverter<Bson> documentConverter, ContainerValueConverter<Collection<?>> collectionConverter,
2351-
ContainerValueConverter<Bson> mapConverter, ContainerValueConverter<DBRef> dbRefConverter,
2352-
ValueConverter<Object> elementConverter) {
2357+
ContainerValueConverter<@Nullable Bson> documentConverter, ContainerValueConverter<Collection<?>> collectionConverter,
2358+
ContainerValueConverter<@Nullable Bson> mapConverter, ContainerValueConverter<@Nullable DBRef> dbRefConverter,
2359+
ValueConverter<@Nullable Object> elementConverter) {
23532360

23542361
this.sourceConverter = sourceConverter;
23552362
this.conversions = customConversions;
@@ -2453,9 +2460,9 @@ interface ValueConverter<T> {
24532460
*
24542461
* @param <T>
24552462
*/
2456-
interface ContainerValueConverter<T> {
2463+
interface ContainerValueConverter<@Nullable T> {
24572464

2458-
Object convert(ConversionContext context, T source, TypeInformation<?> typeHint);
2465+
@Nullable Object convert(ConversionContext context, @Nullable T source, TypeInformation<?> typeHint);
24592466

24602467
}
24612468

@@ -2470,7 +2477,7 @@ class ProjectingConversionContext extends DefaultConversionContext {
24702477

24712478
ProjectingConversionContext(MongoConverter sourceConverter, CustomConversions customConversions, ObjectPath path,
24722479
ContainerValueConverter<Collection<?>> collectionConverter, ContainerValueConverter<Bson> mapConverter,
2473-
ContainerValueConverter<DBRef> dbRefConverter, ValueConverter<Object> elementConverter,
2480+
ContainerValueConverter<@Nullable DBRef> dbRefConverter, ValueConverter<Object> elementConverter,
24742481
EntityProjection<?, ?> projection) {
24752482
super(sourceConverter, customConversions, path,
24762483
(context, source, typeHint) -> doReadOrProject(context, source, typeHint, projection),

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public Set<ConvertiblePair> getConvertibleTypes() {
133133
return new HashSet<>(Arrays.asList(localeToString, booleanToString));
134134
}
135135

136-
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
136+
public @Nullable Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
137137
return source != null ? source.toString() : null;
138138
}
139139
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.springframework.data.mongodb.util.BsonUtils;
6767
import org.springframework.data.mongodb.util.DotPath;
6868
import org.springframework.data.util.TypeInformation;
69+
import org.springframework.lang.Contract;
6970
import org.springframework.util.Assert;
7071
import org.springframework.util.ObjectUtils;
7172
import org.springframework.util.StringUtils;
@@ -274,6 +275,7 @@ public Document addMetaAttributes(Document source, @Nullable MongoPersistentEnti
274275
return mapMetaAttributes(source, entity, MetaMapping.FORCE);
275276
}
276277

278+
@SuppressWarnings("NullAway")
277279
private Document mapMetaAttributes(Document source, @Nullable MongoPersistentEntity<?> entity,
278280
MetaMapping metaMapping) {
279281

@@ -346,7 +348,7 @@ private Document getMappedTextScoreField(MongoPersistentProperty property) {
346348
* @param rawValue
347349
* @return
348350
*/
349-
protected Entry<String, Object> getMappedObjectForField(Field field, Object rawValue) {
351+
protected Entry<String, @Nullable Object> getMappedObjectForField(Field field, @Nullable Object rawValue) {
350352

351353
String key = field.getMappedKey();
352354
Object value;
@@ -410,7 +412,9 @@ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEn
410412
}
411413

412414
if (keyword.isSample()) {
413-
return exampleMapper.getMappedExample(keyword.getValue(), entity);
415+
416+
Example<?> example = keyword.getValue();
417+
return exampleMapper.getMappedExample(example, entity != null ? entity : mappingContext.getRequiredPersistentEntity(example.getProbeType()));
414418
}
415419

416420
if (keyword.isJsonSchema()) {
@@ -451,8 +455,8 @@ protected Document getMappedKeyword(Field property, Keyword keyword) {
451455
* @param sourceValue the source object to be mapped
452456
* @return
453457
*/
454-
@SuppressWarnings("unchecked")
455-
protected @Nullable Object getMappedValue(Field documentField, Object sourceValue) {
458+
@SuppressWarnings("NullAway")
459+
protected @Nullable Object getMappedValue(Field documentField, @Nullable Object sourceValue) {
456460

457461
Object value = applyFieldTargetTypeHintToValue(documentField, sourceValue);
458462

@@ -489,6 +493,7 @@ private boolean isIdField(Field documentField) {
489493
&& documentField.getProperty().getOwner().isIdProperty(documentField.getProperty());
490494
}
491495

496+
@SuppressWarnings("NullAway")
492497
private Class<?> getIdTypeForField(Field documentField) {
493498
return isIdField(documentField) ? documentField.getProperty().getFieldType() : ObjectId.class;
494499
}
@@ -527,7 +532,7 @@ protected boolean isAssociationConversionNecessary(Field documentField, @Nullabl
527532
}
528533

529534
MongoPersistentEntity<?> entity = documentField.getPropertyEntity();
530-
return entity.hasIdProperty()
535+
return entity != null && entity.hasIdProperty()
531536
&& (type.equals(DBRef.class) || entity.getRequiredIdProperty().getActualType().isAssignableFrom(type));
532537
}
533538

@@ -660,7 +665,7 @@ protected Object convertAssociation(@Nullable Object source, @Nullable MongoPers
660665
return createReferenceFor(source, property);
661666
}
662667

663-
private @Nullable Object convertValue(Field documentField, Object sourceValue, Object value,
668+
private @Nullable Object convertValue(Field documentField, @Nullable Object sourceValue, @Nullable Object value,
664669
PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter) {
665670

666671
MongoPersistentProperty property = documentField.getProperty();
@@ -828,6 +833,7 @@ private Object createReferenceFor(Object source, MongoPersistentProperty propert
828833
* @param candidate
829834
* @return
830835
*/
836+
@Contract("null -> false")
831837
protected boolean isNestedKeyword(@Nullable Object candidate) {
832838

833839
if (!(candidate instanceof Document)) {

0 commit comments

Comments
 (0)