Skip to content

Commit bdd5c9d

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1762 - Add @nullable and @NonNullApi annotations.
Marked all packages with Spring Frameworks @NonNullApi. Added Spring's @nullable to methods, parameters and fields that take or produce null values. Adapted using code to make sure the IDE can evaluate the null flow properly. Fixed Javadoc in places where an invalid null handling policy was advertised. Strengthened null requirements for types that expose null-instances.
1 parent e28bede commit bdd5c9d

File tree

113 files changed

+1126
-745
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1126
-745
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.regex.Matcher;
2626
import java.util.regex.Pattern;
2727

28+
import org.springframework.lang.Nullable;
2829
import org.springframework.util.StringUtils;
2930

3031
import com.mongodb.MongoCredential;
@@ -51,7 +52,7 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
5152
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
5253
*/
5354
@Override
54-
public void setAsText(String text) throws IllegalArgumentException {
55+
public void setAsText(@Nullable String text) throws IllegalArgumentException {
5556

5657
if (!StringUtils.hasText(text)) {
5758
return;

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717

1818
import java.beans.PropertyEditorSupport;
1919

20+
import org.springframework.lang.Nullable;
21+
2022
import com.mongodb.ReadPreference;
2123

2224
/**
@@ -32,7 +34,7 @@ public class ReadPreferencePropertyEditor extends PropertyEditorSupport {
3234
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
3335
*/
3436
@Override
35-
public void setAsText(String readPreferenceString) throws IllegalArgumentException {
37+
public void setAsText(@Nullable String readPreferenceString) throws IllegalArgumentException {
3638

3739
if (readPreferenceString == null) {
3840
return;
@@ -59,8 +61,8 @@ public void setAsText(String readPreferenceString) throws IllegalArgumentExcepti
5961
} else if ("NEAREST".equalsIgnoreCase(readPreferenceString)) {
6062
setValue(ReadPreference.nearest());
6163
} else {
62-
throw new IllegalArgumentException(String.format("Cannot find matching ReadPreference for %s",
63-
readPreferenceString));
64+
throw new IllegalArgumentException(
65+
String.format("Cannot find matching ReadPreference for %s", readPreferenceString));
6466
}
6567
}
6668
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
26+
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
2728
import org.springframework.util.StringUtils;
2829

@@ -34,6 +35,7 @@
3435
* @author Mark Pollack
3536
* @author Oliver Gierke
3637
* @author Thomas Darimont
38+
* @author Christoph Strobl
3739
*/
3840
public class ServerAddressPropertyEditor extends PropertyEditorSupport {
3941

@@ -49,7 +51,7 @@ public class ServerAddressPropertyEditor extends PropertyEditorSupport {
4951
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
5052
*/
5153
@Override
52-
public void setAsText(String replicaSetString) {
54+
public void setAsText(@Nullable String replicaSetString) {
5355

5456
if (!StringUtils.hasText(replicaSetString)) {
5557
setValue(null);

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,9 @@
1717

1818
import java.beans.PropertyEditorSupport;
1919

20+
import org.springframework.lang.Nullable;
21+
import org.springframework.util.StringUtils;
22+
2023
import com.mongodb.WriteConcern;
2124

2225
/**
@@ -26,14 +29,19 @@
2629
* string value.
2730
*
2831
* @author Mark Pollack
32+
* @author Christoph Strobl
2933
*/
3034
public class WriteConcernPropertyEditor extends PropertyEditorSupport {
3135

3236
/**
3337
* Parse a string to a List<ServerAddress>
3438
*/
3539
@Override
36-
public void setAsText(String writeConcernString) {
40+
public void setAsText(@Nullable String writeConcernString) {
41+
42+
if (!StringUtils.hasText(writeConcernString)) {
43+
return;
44+
}
3745

3846
WriteConcern writeConcern = WriteConcern.valueOf(writeConcernString);
3947
if (writeConcern != null) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Spring XML namespace configuration for MongoDB specific repositories.
33
*/
4+
@org.springframework.lang.NonNullApi
45
package org.springframework.data.mongodb.config;
56

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2016 the original author or authors.
2+
* Copyright 2010-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,12 +17,13 @@
1717

1818
import org.bson.Document;
1919
import org.springframework.dao.DataAccessException;
20+
import org.springframework.lang.Nullable;
2021

2122
import com.mongodb.MongoException;
2223
import com.mongodb.client.MongoCollection;
2324

2425
/**
25-
* Callback interface for executing actions against a {@link MongoCollection}
26+
* Callback interface for executing actions against a {@link MongoCollection}.
2627
*
2728
* @author Mark Pollak
2829
* @author Grame Rocher
@@ -35,10 +36,11 @@ public interface CollectionCallback<T> {
3536

3637
/**
3738
* @param collection never {@literal null}.
38-
* @return
39+
* @return can be {@literal null}.
3940
* @throws MongoException
4041
* @throws DataAccessException
4142
*/
43+
@Nullable
4244
T doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException;
4345

4446
}

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919

2020
import org.springframework.data.mongodb.core.query.Collation;
21+
import org.springframework.lang.Nullable;
2122
import org.springframework.util.Assert;
2223

2324
/**
@@ -29,26 +30,27 @@
2930
*/
3031
public class CollectionOptions {
3132

32-
private Long maxDocuments;
33-
private Long size;
34-
private Boolean capped;
35-
private Collation collation;
33+
private @Nullable Long maxDocuments;
34+
private @Nullable Long size;
35+
private @Nullable Boolean capped;
36+
private @Nullable Collation collation;
3637

3738
/**
3839
* Constructs a new <code>CollectionOptions</code> instance.
3940
*
40-
* @param size the collection size in bytes, this data space is preallocated.
41-
* @param maxDocuments the maximum number of documents in the collection.
41+
* @param size the collection size in bytes, this data space is preallocated. Can be {@literal null}.
42+
* @param maxDocuments the maximum number of documents in the collection. Can be {@literal null}.
4243
* @param capped true to created a "capped" collection (fixed size with auto-FIFO behavior based on insertion order),
43-
* false otherwise.
44+
* false otherwise. Can be {@literal null}.
4445
* @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point.
4546
*/
4647
@Deprecated
47-
public CollectionOptions(Long size, Long maxDocuments, Boolean capped) {
48+
public CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped) {
4849
this(size, maxDocuments, capped, null);
4950
}
5051

51-
private CollectionOptions(Long size, Long maxDocuments, Boolean capped, Collation collation) {
52+
private CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped,
53+
@Nullable Collation collation) {
5254

5355
this.maxDocuments = maxDocuments;
5456
this.size = size;
@@ -120,7 +122,7 @@ public CollectionOptions size(long size) {
120122
* @return new {@link CollectionOptions}.
121123
* @since 2.0
122124
*/
123-
public CollectionOptions collation(Collation collation) {
125+
public CollectionOptions collation(@Nullable Collation collation) {
124126
return new CollectionOptions(size, maxDocuments, capped, collation);
125127
}
126128

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2016 the original author or authors.
2+
* Copyright 2010-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,29 @@
1616
package org.springframework.data.mongodb.core;
1717

1818
import org.springframework.dao.DataAccessException;
19+
import org.springframework.lang.Nullable;
1920

2021
import com.mongodb.MongoException;
2122
import com.mongodb.client.MongoDatabase;
2223

2324
/**
25+
* Callback interface for executing actions against a {@link MongoDatabase}.
2426
*
25-
* @param <T>
27+
* @author Mark Pollak
28+
* @author Graeme Rocher
29+
* @author Thomas Risberg
30+
* @author Oliver Gierke
31+
* @author John Brisbin
32+
* @author Christoph Strobl
2633
*/
2734
public interface DbCallback<T> {
2835

36+
/**
37+
* @param db must not be {@literal null}.
38+
* @return can be {@literal null}.
39+
* @throws MongoException
40+
* @throws DataAccessException
41+
*/
42+
@Nullable
2943
T doInDB(MongoDatabase db) throws MongoException, DataAccessException;
3044
}

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

-66
This file was deleted.

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.mongodb.core.query.Query;
3636
import org.springframework.data.mongodb.core.query.Update;
3737
import org.springframework.data.util.Pair;
38+
import org.springframework.lang.Nullable;
3839
import org.springframework.util.Assert;
3940

4041
import com.mongodb.BulkWriteException;
@@ -67,7 +68,7 @@ class DefaultBulkOperations implements BulkOperations {
6768
private final List<WriteModel<Document>> models = new ArrayList<>();
6869

6970
private PersistenceExceptionTranslator exceptionTranslator;
70-
private WriteConcern defaultWriteConcern;
71+
private @Nullable WriteConcern defaultWriteConcern;
7172

7273
private BulkWriteOptions bulkOptions;
7374

@@ -99,7 +100,7 @@ class DefaultBulkOperations implements BulkOperations {
99100
*
100101
* @param exceptionTranslator can be {@literal null}.
101102
*/
102-
public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) {
103+
public void setExceptionTranslator(@Nullable PersistenceExceptionTranslator exceptionTranslator) {
103104
this.exceptionTranslator = exceptionTranslator == null ? new MongoExceptionTranslator() : exceptionTranslator;
104105
}
105106

@@ -108,7 +109,7 @@ public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTrans
108109
*
109110
* @param defaultWriteConcern can be {@literal null}.
110111
*/
111-
void setDefaultWriteConcern(WriteConcern defaultWriteConcern) {
112+
void setDefaultWriteConcern(@Nullable WriteConcern defaultWriteConcern) {
112113
this.defaultWriteConcern = defaultWriteConcern;
113114
}
114115

0 commit comments

Comments
 (0)