Skip to content

Commit 567051e

Browse files
authored
Merge pull request #150 from microsoftgraph/feature/spotbugs
feature/spotbugs
2 parents 7303bae + ab2ff30 commit 567051e

File tree

72 files changed

+565
-365
lines changed

Some content is hidden

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

72 files changed

+565
-365
lines changed

build.gradle

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ plugins {
1414
id 'maven-publish'
1515
id 'signing'
1616
id 'jacoco'
17+
id 'com.github.spotbugs' version '4.6.0'
1718
}
1819

1920
java {
@@ -32,6 +33,28 @@ jacoco {
3233
toolVersion = "0.8.7-SNAPSHOT" //https://github.com/gradle/gradle/issues/15038
3334
}
3435

36+
spotbugsMain {
37+
excludeFilter = file("spotBugsExcludeFilter.xml")
38+
reports {
39+
html {
40+
enabled = true
41+
destination = file("$buildDir/reports/spotbugs/main/spotbugs.html")
42+
stylesheet = 'fancy-hist.xsl'
43+
}
44+
}
45+
}
46+
47+
spotbugsTest {
48+
excludeFilter = file("spotBugsExcludeFilter.xml")
49+
reports {
50+
html {
51+
enabled = true
52+
destination = file("$buildDir/reports/spotbugs/test/spotbugs.html")
53+
stylesheet = 'fancy-hist.xsl'
54+
}
55+
}
56+
}
57+
3558
jacocoTestReport {
3659
reports {
3760
xml.enabled true

spotBugsExcludeFilter.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<FindBugsFilter
3+
xmlns="https://github.com/spotbugs/filter/3.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
6+
<Match>
7+
<Class name="com.microsoft.graph.authentication.BaseAuthenticationProviderTest" />
8+
<Method name="providerDoesNotAddTokenOnNullUrls" />
9+
<Bug code="NP" />
10+
</Match>
11+
<Match>
12+
<Class name="com.microsoft.graph.concurrency.ChunkedUploadRequest" />
13+
<Bug code="Dm" />
14+
</Match>
15+
<Match>
16+
<Class name="com.microsoft.graph.concurrency.ChunkedUploadResponseHandler" />
17+
<Method name="generateResult" />
18+
<Bug code="RCN,NP" />
19+
</Match>
20+
</FindBugsFilter>

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadProvider.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.InputStream;
3131
import java.security.InvalidParameterException;
3232
import java.util.List;
33+
import java.util.Objects;
3334
import java.util.concurrent.CompletableFuture;
3435
import java.util.concurrent.ExecutionException;
3536

@@ -103,25 +104,15 @@ public ChunkedUploadProvider(@Nonnull final IUploadSession uploadSession,
103104
@Nonnull final InputStream inputStream,
104105
final long streamSize,
105106
@Nonnull final Class<UploadType> uploadTypeClass) {
106-
if (uploadSession == null) {
107-
throw new InvalidParameterException("Upload session is null.");
108-
}
109-
110-
if (client == null) {
111-
throw new InvalidParameterException("OneDrive client is null.");
112-
}
113-
114-
if (inputStream == null) {
115-
throw new InvalidParameterException("Input stream is null.");
116-
}
107+
Objects.requireNonNull(uploadSession, "Upload session is null.");
117108

118109
if (streamSize <= 0) {
119110
throw new InvalidParameterException("Stream size should larger than 0.");
120111
}
121112

122-
this.client = client;
113+
this.client = Objects.requireNonNull(client, "Graph client is null.");
123114
this.readSoFar = 0;
124-
this.inputStream = inputStream;
115+
this.inputStream = Objects.requireNonNull(inputStream, "Input stream is null.");
125116
this.streamSize = streamSize;
126117
this.uploadUrl = uploadSession.getUploadUrl();
127118
this.responseHandler = new ChunkedUploadResponseHandler<UploadType>(uploadTypeClass, uploadSession.getClass());

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadRequest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.List;
88
import java.util.Locale;
9+
import java.util.Objects;
910

1011
import javax.annotation.Nullable;
1112
import javax.annotation.Nonnull;
@@ -61,9 +62,12 @@ protected ChunkedUploadRequest(@Nonnull final String requestUrl,
6162
final int chunkSize,
6263
final long beginIndex,
6364
final long totalLength) {
65+
Objects.requireNonNull(requestUrl, "parameter requestUrl cannot be null");
66+
Objects.requireNonNull(client, "parameter client cannot be null");
67+
Objects.requireNonNull(chunk, "parameter chunk cannot be null");
6468
this.data = new byte[chunkSize];
6569
System.arraycopy(chunk, 0, this.data, 0, chunkSize);
66-
this.baseRequest = new BaseRequest<ChunkedUploadResult<UploadType>>(requestUrl, client, options, (Class<? extends ChunkedUploadResult<UploadType>>)(new ChunkedUploadResult<UploadType>((UploadType)null)).getClass()) {
70+
this.baseRequest = new BaseRequest<ChunkedUploadResult<UploadType>>(requestUrl, client, options, (Class<? extends ChunkedUploadResult<UploadType>>)(new ChunkedUploadResult<>((UploadType)null)).getClass()) {
6771
};
6872
this.baseRequest.setHttpMethod(HttpMethod.PUT);
6973
this.baseRequest.addHeader(CONTENT_RANGE_HEADER_NAME,
@@ -84,6 +88,7 @@ protected ChunkedUploadRequest(@Nonnull final String requestUrl,
8488
@Nonnull
8589
public ChunkedUploadResult<UploadType> upload(
8690
@Nonnull final ChunkedUploadResponseHandler<UploadType> responseHandler) {
91+
Objects.requireNonNull(responseHandler, "parameter responseHandler cannot be null");
8792
ChunkedUploadResult<UploadType> result = null;
8893

8994
try {

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadResponseHandler.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import java.io.ByteArrayInputStream;
3636
import java.io.IOException;
3737
import java.io.InputStream;
38+
import java.util.Objects;
3839

3940
import javax.annotation.Nonnull;
4041
import javax.annotation.Nullable;
4142

43+
import okhttp3.MediaType;
4244
import okhttp3.Response;
4345
import okhttp3.ResponseBody;
4446

@@ -64,8 +66,8 @@ public class ChunkedUploadResponseHandler<UploadType>
6466
* @param uploadSessionType the type of the upload session
6567
*/
6668
protected ChunkedUploadResponseHandler(@Nonnull final Class<UploadType> uploadType, @Nonnull final Class<? extends IUploadSession> uploadSessionType) {
67-
this.deserializeTypeClass = uploadType;
68-
this.uploadSessionClass = uploadSessionType;
69+
this.deserializeTypeClass = Objects.requireNonNull(uploadType, "parameter uploadType cannot be null");
70+
this.uploadSessionClass = Objects.requireNonNull(uploadSessionType, "parameter uploadSessionType cannot be null");
6971
}
7072

7173
/**
@@ -95,6 +97,10 @@ public ChunkedUploadResult<UploadType> generateResult(
9597
@Nonnull final Response response,
9698
@Nonnull final ISerializer serializer,
9799
@Nonnull final ILogger logger) throws Exception {
100+
Objects.requireNonNull(request, "parameter request cannot be null");
101+
Objects.requireNonNull(response, "parameter response cannot be null");
102+
Objects.requireNonNull(serializer, "parameter serializer cannot be null");
103+
Objects.requireNonNull(logger, "parameter logger cannot be null");
98104
if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) {
99105
logger.logDebug("Receiving error during upload, see detail on result error");
100106

@@ -105,7 +111,9 @@ public ChunkedUploadResult<UploadType> generateResult(
105111
&& response.code() < HttpResponseCode.HTTP_MULTIPLE_CHOICES) {
106112
try(final ResponseBody body = response.body()) {
107113
final String location = response.headers().get("Location");
108-
if (body.contentType() != null && body.contentType().subtype().contains("json")) {
114+
final MediaType contentType = body.contentType();
115+
final String subType = contentType == null ? null : contentType.subtype();
116+
if (subType != null && subType.contains("json")) {
109117
return parseJsonUploadResult(body, serializer, logger);
110118
} else if (location != null) {
111119
logger.logDebug("Upload session is completed (Outlook), uploaded item returned.");

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadResult.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import com.microsoft.graph.http.GraphServiceException;
99

1010
import javax.annotation.Nullable;
11+
12+
import java.util.Objects;
13+
1114
import javax.annotation.Nonnull;
1215

1316
/**
@@ -68,7 +71,10 @@ protected ChunkedUploadResult(@Nullable final ClientException error) {
6871
* @param exception The exception received from server.
6972
*/
7073
protected ChunkedUploadResult(@Nonnull final GraphServiceException exception) {
71-
this(new ClientException(exception.getMessage(/* verbose */ true), exception));
74+
this(new ClientException(Objects
75+
.requireNonNull(exception, "parameter exception cannot be null")
76+
.getMessage(/* verbose */ true),
77+
exception));
7278
}
7379

7480
/**

src/main/java/com/microsoft/graph/content/BatchRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class BatchRequest extends BaseRequest<BatchResponseContent> {
4343
* @param client the client to use to execute the request
4444
* @param options the options to apply to the request
4545
*/
46-
public BatchRequest(@Nonnull final String requestUrl, @Nonnull final IBaseClient client, @Nonnull final List<? extends Option> options) {
46+
public BatchRequest(@Nonnull final String requestUrl, @Nonnull final IBaseClient<?> client, @Nonnull final List<? extends Option> options) {
4747
super(requestUrl, client, options, BatchResponseContent.class);
4848
}
4949

@@ -55,7 +55,7 @@ public BatchRequest(@Nonnull final String requestUrl, @Nonnull final IBaseClient
5555
* @throws ClientException an exception occurs if there was an error while the request was sent
5656
*/
5757
@Nullable
58-
public BatchResponseContent post(@Nonnull final BatchRequestContent content) throws ClientException {
58+
public BatchResponseContent post(@Nullable final BatchRequestContent content) throws ClientException {
5959
this.setHttpMethod(HttpMethod.POST);
6060
final BatchResponseContent response = this.getClient().getHttpProvider().send(this, BatchResponseContent.class, content);
6161
setSerializerOnSteps(response);
@@ -69,7 +69,7 @@ public BatchResponseContent post(@Nonnull final BatchRequestContent content) thr
6969
* @throws ClientException an exception occurs if there was an error while the request was sent
7070
*/
7171
@Nullable
72-
public java.util.concurrent.CompletableFuture<BatchResponseContent> postAsync(@Nonnull final BatchRequestContent content) throws ClientException {
72+
public java.util.concurrent.CompletableFuture<BatchResponseContent> postAsync(@Nullable final BatchRequestContent content) throws ClientException {
7373
this.setHttpMethod(HttpMethod.POST);
7474
return this.getClient().getHttpProvider().sendAsync(this, BatchResponseContent.class, content).thenApply(response -> {
7575
setSerializerOnSteps(response);

src/main/java/com/microsoft/graph/content/BatchRequestBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class BatchRequestBuilder extends BaseRequestBuilder<BatchResponseContent
3939
* @param client the client to use to execute the request
4040
* @param options the request options
4141
*/
42-
public BatchRequestBuilder(@Nonnull final String requestUrl, @Nonnull final IBaseClient client, @Nonnull final List<? extends Option> options) {
42+
public BatchRequestBuilder(@Nonnull final String requestUrl, @Nonnull final IBaseClient<?> client, @Nonnull final List<? extends Option> options) {
4343
super(requestUrl, client, options);
4444
}
4545
/**

src/main/java/com/microsoft/graph/content/BatchRequestContent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public void removeBatchRequestStepWithId(@Nonnull final String ...stepIds) {
138138

139139
for(final String stepId : stepIds) {
140140
Objects.requireNonNull(stepId, "parameter stepIds cannot contain null values");
141-
requests.removeIf(x -> x.id == stepId);
141+
requests.removeIf(x -> stepId.equals(x.id));
142142
for(final BatchRequestStep<?> step : requests) {
143143
if(step.dependsOn != null) {
144-
step.dependsOn.removeIf(x -> x == stepId);
144+
step.dependsOn.removeIf(x -> stepId.equals(x));
145145
if(step.dependsOn.isEmpty())
146146
step.dependsOn = null; // so we don't send dependsOn: [] over the wire
147147
}

src/main/java/com/microsoft/graph/core/BaseClient.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import javax.annotation.Nullable;
3737

38+
import java.util.Collections;
3839
import java.util.Objects;
3940

4041
import javax.annotation.Nonnull;
@@ -74,7 +75,7 @@ public String getServiceRoot() {
7475

7576
@Override
7677
public void setServiceRoot(@Nonnull final String value) {
77-
endpoint = value;
78+
endpoint = Objects.requireNonNull(value, "value parameter cannot be null");
7879
}
7980

8081
/**
@@ -88,7 +89,9 @@ public void setServiceRoot(@Nonnull final String value) {
8889
*/
8990
@Nonnull
9091
public <T> CustomRequestBuilder<T> customRequest(@Nonnull final String url, @Nonnull final Class<T> responseType) {
91-
return new CustomRequestBuilder<>(getServiceRoot() + url, this, null, responseType);
92+
Objects.requireNonNull(url, "url parameter cannot be null");
93+
Objects.requireNonNull(responseType, "responseType parameter cannot be null");
94+
return new CustomRequestBuilder<>(getServiceRoot() + url, this, null, responseType);
9295
}
9396

9497
/**
@@ -100,8 +103,7 @@ public <T> CustomRequestBuilder<T> customRequest(@Nonnull final String url, @Non
100103
*/
101104
@Nonnull
102105
public CustomRequestBuilder<JsonElement> customRequest(@Nonnull final String url) {
103-
return new CustomRequestBuilder<>(getServiceRoot() + url, this, null,
104-
JsonElement.class);
106+
return this.customRequest(url, JsonElement.class);
105107
}
106108

107109
/**
@@ -110,7 +112,7 @@ public CustomRequestBuilder<JsonElement> customRequest(@Nonnull final String url
110112
*/
111113
@Nonnull
112114
public BatchRequestBuilder batch() {
113-
return new BatchRequestBuilder(getServiceRoot() + "/$batch", this, null);
115+
return new BatchRequestBuilder(getServiceRoot() + "/$batch", this, Collections.emptyList());
114116
}
115117

116118
/**
@@ -196,7 +198,7 @@ private IHttpProvider<nativeRequestType> getHttpProvider() {
196198
*/
197199
@Nonnull
198200
public Builder<httpClientType, nativeRequestType> serializer(@Nonnull final ISerializer serializer) {
199-
checkNotNull(serializer, "serializer");
201+
Objects.requireNonNull(serializer, "parameter serializer cannot be null");
200202
this.serializer = serializer;
201203
return this;
202204
}
@@ -210,7 +212,7 @@ public Builder<httpClientType, nativeRequestType> serializer(@Nonnull final ISer
210212
*/
211213
@Nonnull
212214
public Builder<httpClientType, nativeRequestType> httpProvider(@Nonnull final IHttpProvider<nativeRequestType> httpProvider) {
213-
checkNotNull(httpProvider, "httpProvider");
215+
Objects.requireNonNull(httpProvider, "parameter httpProvider cannot be null");
214216
this.httpProvider = httpProvider;
215217
return this;
216218
}
@@ -224,7 +226,7 @@ public Builder<httpClientType, nativeRequestType> httpProvider(@Nonnull final IH
224226
*/
225227
@Nonnull
226228
public Builder<httpClientType, nativeRequestType> logger(@Nonnull final ILogger logger) {
227-
checkNotNull(logger, "logger");
229+
Objects.requireNonNull(logger, "parameter logger cannot be null");
228230
this.logger = logger;
229231
return this;
230232
}
@@ -238,7 +240,7 @@ public Builder<httpClientType, nativeRequestType> logger(@Nonnull final ILogger
238240
*/
239241
@Nonnull
240242
public Builder<httpClientType, nativeRequestType> httpClient(@Nonnull final httpClientType client) {
241-
checkNotNull(client, "client");
243+
Objects.requireNonNull(client, "parameter client cannot be null");
242244
this.httpClient = client;
243245
return this;
244246
}
@@ -251,7 +253,7 @@ public Builder<httpClientType, nativeRequestType> httpClient(@Nonnull final http
251253
*/
252254
@Nonnull
253255
public Builder<httpClientType, nativeRequestType> authenticationProvider(@Nonnull final IAuthenticationProvider auth) {
254-
checkNotNull(auth, "auth");
256+
Objects.requireNonNull(auth, "parameter auth cannot be null");
255257
this.auth = auth;
256258
return this;
257259
}
@@ -287,18 +289,6 @@ public IBaseClient<nativeRequestType> buildClient() throws ClientException {
287289
}
288290
}
289291

290-
/**
291-
* Checks whether the provided object is null or not and throws an exception if it is
292-
*
293-
* @param o object to check
294-
* @param name name to use in the exception message
295-
*/
296-
protected static void checkNotNull(@Nullable final Object o, @Nonnull final String name) {
297-
if (o==null) {
298-
throw new NullPointerException(name + " cannot be null");
299-
}
300-
}
301-
302292
/**
303293
* The HTTP provider instance
304294
*/
@@ -352,7 +342,7 @@ public ISerializer getSerializer() {
352342
* @param logger The logger
353343
*/
354344
protected void setLogger(@Nonnull final ILogger logger) {
355-
checkNotNull(logger, "logger");
345+
Objects.requireNonNull(logger, "parameter logger cannot be null");
356346
this.logger = logger;
357347
}
358348

@@ -362,7 +352,7 @@ protected void setLogger(@Nonnull final ILogger logger) {
362352
* @param httpProvider The HTTP provider
363353
*/
364354
protected void setHttpProvider(@Nonnull final IHttpProvider<nativeRequestType> httpProvider) {
365-
checkNotNull(httpProvider, "httpProvider");
355+
Objects.requireNonNull(httpProvider, "parameter httpProvider cannot be null");
366356
this.httpProvider = httpProvider;
367357
}
368358

@@ -372,7 +362,7 @@ protected void setHttpProvider(@Nonnull final IHttpProvider<nativeRequestType> h
372362
* @param serializer The serializer
373363
*/
374364
public void setSerializer(@Nonnull final ISerializer serializer) {
375-
checkNotNull(serializer, "serializer");
365+
Objects.requireNonNull(serializer, "parameter serializer cannot be null");
376366
this.serializer = serializer;
377367
}
378368

0 commit comments

Comments
 (0)