Skip to content

Commit 2f46829

Browse files
committed
Findbugs updates
Update findbugs to 3.0.1 Fix findbugs-exclude.xml Fix found bugs JAVA-1822 JAVA-1823
1 parent afd41e0 commit 2f46829

File tree

11 files changed

+75
-63
lines changed

11 files changed

+75
-63
lines changed

bson/src/main/org/bson/BsonDouble.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public BsonDouble(final double value) {
3939

4040
@Override
4141
public int compareTo(final BsonDouble o) {
42-
return Double.valueOf(value).compareTo(o.value);
42+
return Double.compare(value, o.value);
4343
}
4444

4545
@Override

bson/src/main/org/bson/json/JsonReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ private long visitDateTimeConstructor() {
823823
}
824824
if (pos == 1) {
825825
return values[0];
826-
} else if (pos < 3 && pos > 7) {
826+
} else if (pos < 3 || pos > 7) {
827827
throw new JsonParseException("JSON reader expected 1 or 3-7 integers but found %d.", pos);
828828
}
829829

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ configure(subprojects.findAll { it.name != 'util' && it.name != 'mongo-java-driv
195195
findbugs {
196196
excludeFilter = new File("$configDir/findbugs-exclude.xml")
197197
sourceSets = [sourceSets.main]
198-
toolVersion = '3.0.0'
198+
toolVersion = '3.0.1'
199199
}
200200

201201
codenarc {

config/findbugs-exclude.xml

+44-29
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,53 @@
77
<Bug code="EI,EI2"/>
88
</Match>
99

10-
<Match>
11-
<Package name="com.mongodb">
12-
<Bug pattern="EQ_DOESNT_OVERRIDE_EQUALS"/> <!-- Deliberately ignoring this, as many BSONObject subclasses don't do it -->
13-
</Package>
14-
</Match>
1510

1611
<!-- these specific issues are deliberate design decisions -->
12+
<!-- Deliberately ignoring this, as many BSONObject subclasses don't do it -->
1713
<Match>
18-
<Class name="com.mongodb.connection.BaseCluster">
19-
<Bug pattern="RV_RETURN_VALUE_IGNORED"/> <!-- Deliberately ignoring return value of CountDownLatch.await -->
20-
</Class>
14+
<Package name="com.mongodb"/>
15+
<Bug pattern="EQ_DOESNT_OVERRIDE_EQUALS"/>
2116
</Match>
2217

18+
<!-- Deliberately ignoring return value of CountDownLatch.await -->
2319
<Match>
24-
<Class name="com.mongodb.gridfs.GridFS" />
25-
<Method name="createFile" params="java.io.File" /> <!-- The underlying call to GridFSInputFile closes the file -->
26-
<Bug pattern="OBL_UNSATISFIED_OBLIGATION"/>
20+
<Class name="com.mongodb.connection.BaseCluster"/>
21+
<Bug pattern="RV_RETURN_VALUE_IGNORED"/>
2722
</Match>
23+
24+
<!-- The underlying call to GridFSInputFile closes the file -->
2825
<Match>
29-
<Class name="com.mongodb.DocumentCodec">
30-
<Bug pattern="NM_SAME_SIMPLE_NAME_AS_SUPERCLASS"/>
31-
</Class>
26+
<Class name="com.mongodb.gridfs.GridFS"/>
27+
<Method name="createFile" params="java.io.File"/>
28+
<Bug pattern="OBL_UNSATISFIED_OBLIGATION"/>
3229
</Match>
30+
3331
<Match>
34-
<Class name="org.mongodb.DatabaseTestCase">
35-
<Bug pattern="MS_PKGPROTECT"/>
36-
</Class>
32+
<Class name="com.mongodb.DocumentCodec"/>
33+
<Bug pattern="NM_SAME_SIMPLE_NAME_AS_SUPERCLASS"/>
3734
</Match>
35+
3836
<Match>
39-
<Class name="org.mongodb.FunctionalSpecification">
40-
<Bug pattern="MS_PKGPROTECT"/>
41-
</Class>
37+
<Class name="org.mongodb.DatabaseTestCase" />
38+
<Bug pattern="MS_PKGPROTECT"/>
4239
</Match>
40+
4341
<Match>
44-
<Class name="com.mongodb.DatabaseTestCase">
45-
<Bug pattern="MS_PKGPROTECT"/>
46-
</Class>
42+
<Class name="org.mongodb.FunctionalSpecification"/>
43+
<Bug pattern="MS_PKGPROTECT"/>
4744
</Match>
45+
4846
<Match>
49-
<Class name="org.bson.types.StringRangeSet$NumberStringComparator">
50-
<Bug pattern="SE_COMPARATOR_SHOULD_BE_SERIALIZABLE"/>
51-
</Class>
47+
<Class name="com.mongodb.DatabaseTestCase"/>
48+
<Bug pattern="MS_PKGPROTECT"/>
5249
</Match>
5350

54-
<!-- Spock tests seem to fail the serial version ID test when stubbing. Annoying. -->
5551
<Match>
56-
<Class name="~.*\.*Specification.*"/>
57-
<Bug pattern="SE_NO_SERIALVERSIONID,LI_LAZY_INIT_STATIC"/>
52+
<Class name="org.bson.types.StringRangeSet$NumberStringComparator"/>
53+
<Bug pattern="SE_COMPARATOR_SHOULD_BE_SERIALIZABLE"/>
5854
</Match>
5955

56+
<!-- Test exclusions -->
6057
<!-- All bugs in test classes, except for JUnit-specific bugs -->
6158
<Match>
6259
<Class name="~.*\.*Test"/>
@@ -65,4 +62,22 @@
6562
</Not>
6663
</Match>
6764

65+
<!-- Deliberate use of an unused field in the Person POJO class -->
66+
<Match>
67+
<Class name="com.mongodb.acceptancetest.crud.pojo.Person"/>
68+
<Bug pattern="SS_SHOULD_BE_STATIC"/>
69+
</Match>
70+
71+
<!-- Spock tests seem to fail the serial version ID test when stubbing. Annoying. -->
72+
<Match>
73+
<Source name="~.*\.groovy"/>
74+
<Bug pattern="SE_NO_SERIALVERSIONID,LI_LAZY_INIT_STATIC,EQ_UNUSUAL,IT_NO_SUCH_ELEMENT,RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT"/>
75+
</Match>
76+
77+
<!-- Strange findbugs issues -->
78+
<Match>
79+
<Source name="~.*ClusterSettingsSpecification\.groovy"/>
80+
<Bug pattern="RANGE_ARRAY_INDEX"/>
81+
</Match>
82+
6883
</FindBugsFilter>

driver-core/src/main/com/mongodb/WriteConcernResult.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean equals(final Object o) {
9393
if (this == o) {
9494
return true;
9595
}
96-
if (getClass() != o.getClass()) {
96+
if (o == null || getClass() != o.getClass()) {
9797
return false;
9898
}
9999

driver-core/src/test/functional/com/mongodb/ClusterFixture.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ public static AsyncReadWriteBinding getAsyncBinding(final Cluster cluster) {
162162
return new AsyncClusterBinding(cluster, ReadPreference.primary());
163163
}
164164

165-
public static Cluster getCluster() {
165+
public static synchronized Cluster getCluster() {
166166
if (cluster == null) {
167167
cluster = createCluster(new SocketStreamFactory(getSocketSettings(), getSslSettings()));
168168
}
169169
return cluster;
170170
}
171171

172-
public static Cluster getAsyncCluster() {
172+
public static synchronized Cluster getAsyncCluster() {
173173
if (asyncCluster == null) {
174174
asyncCluster = createCluster(getAsyncStreamFactory());
175175
}

driver-core/src/test/functional/com/mongodb/operation/ListCollectionsOperationSpecification.groovy

+3-6
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,10 @@ class ListCollectionsOperationSpecification extends OperationFunctionalSpecifica
369369

370370
def cursorToListWithNext(BatchCursor cursor) {
371371
def list = []
372-
try {
373-
while (true) {
374-
list += cursor.next()
375-
}
376-
} catch (NoSuchElementException e) {
377-
return list
372+
while (cursor.hasNext()) {
373+
list += cursor.next()
378374
}
375+
list
379376
}
380377

381378
def cursorToListWithTryNext(BatchCursor cursor) {

driver-core/src/test/unit/com/mongodb/JsonPoweredTestHelper.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323

2424
import java.io.BufferedReader;
2525
import java.io.File;
26-
import java.io.FileReader;
26+
import java.io.FileInputStream;
2727
import java.io.IOException;
28+
import java.io.InputStreamReader;
2829
import java.net.URISyntaxException;
30+
import java.nio.charset.Charset;
2931
import java.util.ArrayList;
3032
import java.util.List;
3133

@@ -42,26 +44,31 @@ public static List<File> getTestFiles(final String resourcePath) throws URISynta
4244
}
4345

4446
private static String getFileAsString(final File file) throws IOException {
45-
BufferedReader reader = new BufferedReader(new FileReader(file));
4647
String line;
4748
StringBuilder stringBuilder = new StringBuilder();
4849
String ls = System.getProperty("line.separator");
49-
50-
while ((line = reader.readLine()) != null) {
51-
stringBuilder.append(line);
52-
stringBuilder.append(ls);
50+
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")));
51+
try {
52+
while ((line = reader.readLine()) != null) {
53+
stringBuilder.append(line);
54+
stringBuilder.append(ls);
55+
}
56+
} finally {
57+
reader.close();
5358
}
54-
5559
return stringBuilder.toString();
5660
}
5761

5862
private static void addFilesFromDirectory(final File directory, final List<File> files) {
59-
for (String fileName : directory.list()) {
60-
File file = new File(directory, fileName);
61-
if (file.isDirectory()) {
62-
addFilesFromDirectory(file, files);
63-
} else if (file.getName().endsWith(".json")) {
64-
files.add(file);
63+
String[] fileNames = directory.list();
64+
if (fileNames != null) {
65+
for (String fileName : fileNames) {
66+
File file = new File(directory, fileName);
67+
if (file.isDirectory()) {
68+
addFilesFromDirectory(file, files);
69+
} else if (file.getName().endsWith(".json")) {
70+
files.add(file);
71+
}
6572
}
6673
}
6774
}

driver/src/main/com/mongodb/FindIterableImpl.java

-6
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,5 @@ public TResult first() {
197197
BatchCursor<TResult> batchCursor = executor.execute(findFirstOperation, readPreference);
198198
return batchCursor.hasNext() ? batchCursor.next().iterator().next() : null;
199199
}
200-
201-
@Override
202-
public FindOperationIterable batchSize(final int batchSize) {
203-
batchSize(batchSize);
204-
return this;
205-
}
206200
}
207201
}

driver/src/main/org/bson/LazyBSONObject.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ Object readValue(final BsonBinaryReader reader) {
170170
case BINARY:
171171
BsonBinary binary = reader.readBinaryData();
172172
byte binaryType = binary.getType();
173-
if (binaryType == BsonBinarySubType.BINARY.getValue()
174-
|| binaryType == BsonBinarySubType.BINARY.getValue()) {
173+
if (binaryType == BsonBinarySubType.BINARY.getValue() || binaryType == BsonBinarySubType.OLD_BINARY.getValue()) {
175174
return binary.getData();
176175
} else if (binaryType == BsonBinarySubType.UUID_LEGACY.getValue()) {
177176
return new UUID(readLong(binary.getData(), 0), readLong(binary.getData(), 8));

driver/src/test/functional/com/mongodb/client/NameCodec.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void encode(final BsonWriter writer, final Name n, final EncoderContext e
3737
public Name decode(final BsonReader reader, final DecoderContext decoderContext) {
3838
reader.readStartDocument();
3939
String name = reader.readString("_id");
40-
int count = new Double(reader.readDouble("value")).intValue();
40+
int count = (int) reader.readDouble("value");
4141

4242
reader.readEndDocument();
4343
return new Name(name, count);

0 commit comments

Comments
 (0)