Skip to content

Commit 5fba926

Browse files
committed
Refine null-safety in the spring-web module
Closes gh-34162
1 parent a442c18 commit 5fba926

23 files changed

+39
-39
lines changed

spring-web/src/main/java/org/springframework/http/client/AbstractStreamingClientHttpRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public final void setBody(Body body) {
6262
}
6363

6464
@Override
65-
@SuppressWarnings("NullAway")
65+
@SuppressWarnings("NullAway") // Lambda
6666
protected final ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
6767
if (this.body == null && this.bodyStream != null) {
6868
this.body = outputStream -> this.bodyStream.writeTo(outputStream);

spring-web/src/main/java/org/springframework/http/client/JdkClientHttpRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public URI getURI() {
9494

9595

9696
@Override
97-
@SuppressWarnings("NullAway")
9897
protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
9998
CompletableFuture<HttpResponse<InputStream>> responseFuture = null;
10099
try {
@@ -133,7 +132,8 @@ else if (cause instanceof IOException ioEx) {
133132
throw ioEx;
134133
}
135134
else {
136-
throw new IOException(cause.getMessage(), cause);
135+
String message = (cause == null ? null : cause.getMessage());
136+
throw (message == null ? new IOException(cause) : new IOException(message, cause));
137137
}
138138
}
139139
}

spring-web/src/main/java/org/springframework/http/client/JettyClientHttpRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public URI getURI() {
6969
}
7070

7171
@Override
72-
@SuppressWarnings("NullAway")
7372
protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
7473
if (!headers.isEmpty()) {
7574
this.request.headers(httpFields -> {
@@ -118,7 +117,8 @@ else if (cause instanceof IOException ioEx) {
118117
throw ioEx;
119118
}
120119
else {
121-
throw new IOException(cause.getMessage(), cause);
120+
String message = (cause == null ? null : cause.getMessage());
121+
throw (message == null ? new IOException(cause) : new IOException(message, cause));
122122
}
123123
}
124124
catch (TimeoutException ex) {

spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public Flux<Object> read(
135135
});
136136
}
137137

138-
@SuppressWarnings("NullAway")
138+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
139139
private @Nullable Object buildEvent(List<String> lines, ResolvableType valueType, boolean shouldWrap,
140140
Map<String, Object> hints) {
141141

spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartParser.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
* @author Arjen Poutsma
5050
* @since 5.3
5151
*/
52-
@SuppressWarnings("NullAway")
5352
final class MultipartParser extends BaseSubscriber<DataBuffer> {
5453

5554
private static final byte CR = '\r';
@@ -117,12 +116,14 @@ protected void hookOnSubscribe(Subscription subscription) {
117116
}
118117

119118
@Override
119+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
120120
protected void hookOnNext(DataBuffer value) {
121121
this.requestOutstanding.set(false);
122122
this.state.get().onNext(value);
123123
}
124124

125125
@Override
126+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
126127
protected void hookOnComplete() {
127128
this.state.get().onComplete();
128129
}

spring-web/src/main/java/org/springframework/http/codec/multipart/PartGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* @author Arjen Poutsma
5858
* @since 5.3
5959
*/
60-
@SuppressWarnings("NullAway")
60+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
6161
final class PartGenerator extends BaseSubscriber<MultipartParser.Token> {
6262

6363
private static final Log logger = LogFactory.getLog(PartGenerator.class);

spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outp
189189
}
190190
}
191191

192-
@SuppressWarnings("NullAway")
192+
@SuppressWarnings("NullAway") // Not null assertion performed in StreamUtils#copyRange
193193
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
194194
HttpOutputMessage outputMessage) throws IOException {
195195

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerReadPublisher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* @since 5.0
4848
* @param <T> the type of element signaled
4949
*/
50-
@SuppressWarnings("NullAway")
50+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
5151
public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
5252

5353
/**

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerWriteFlushProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @since 5.0
4141
* @param <T> the type of element signaled to the {@link Subscriber}
4242
*/
43-
@SuppressWarnings("NullAway")
43+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
4444
public abstract class AbstractListenerWriteFlushProcessor<T> implements Processor<Publisher<? extends T>, Void> {
4545

4646
/**

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerWriteProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @since 5.0
4444
* @param <T> the type of element signaled to the {@link Subscriber}
4545
*/
46-
@SuppressWarnings("NullAway")
46+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
4747
public abstract class AbstractListenerWriteProcessor<T> implements Processor<T, Void> {
4848

4949
/**

spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private static URI initUri(HttpServletRequest servletRequest) {
159159
}
160160
}
161161

162-
@SuppressWarnings("NullAway")
162+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
163163
private static HttpHeaders initHeaders(HttpHeaders headerValues, HttpServletRequest request) {
164164

165165
HttpHeaders headers = null;

spring-web/src/main/java/org/springframework/http/server/reactive/WriteResultPublisher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Rossen Stoyanchev
3737
* @since 5.0
3838
*/
39-
@SuppressWarnings("NullAway")
39+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
4040
class WriteResultPublisher implements Publisher<Void> {
4141

4242
/**

spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private String getErrorMessage(
243243
* {@link RestClientResponseException#setBodyConvertFunction(Function)}.
244244
* @since 6.0
245245
*/
246-
@SuppressWarnings("NullAway")
246+
@SuppressWarnings("NullAway") // Lambda
247247
protected Function<ResolvableType, ?> initBodyConvertFunction(ClientHttpResponse response, byte[] body) {
248248
Assert.state(!CollectionUtils.isEmpty(this.messageConverters), "Expected message converters");
249249
return resolvableType -> {

spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletRes
8383
* @param previousRequest the existing request from the last dispatch
8484
* @since 5.3.33
8585
*/
86-
@SuppressWarnings("NullAway")
86+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
8787
StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletResponse response,
8888
@Nullable StandardServletAsyncWebRequest previousRequest) {
8989

@@ -272,7 +272,7 @@ public void setAsyncWebRequest(StandardServletAsyncWebRequest asyncWebRequest) {
272272
}
273273

274274
@Override
275-
@SuppressWarnings("NullAway")
275+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
276276
public ServletOutputStream getOutputStream() throws IOException {
277277
int level = obtainLockOrRaiseException();
278278
try {
@@ -292,7 +292,7 @@ public ServletOutputStream getOutputStream() throws IOException {
292292
}
293293

294294
@Override
295-
@SuppressWarnings("NullAway")
295+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
296296
public PrintWriter getWriter() throws IOException {
297297
int level = obtainLockOrRaiseException();
298298
try {

spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public void startCallableProcessing(Callable<?> callable, Object... processingCo
298298
* via {@link #getConcurrentResultContext()}
299299
* @throws Exception if concurrent processing failed to start
300300
*/
301-
@SuppressWarnings("NullAway")
301+
@SuppressWarnings("NullAway") // Lambda
302302
public void startCallableProcessing(final WebAsyncTask<?> webAsyncTask, Object... processingContext)
303303
throws Exception {
304304

@@ -419,7 +419,7 @@ private void setConcurrentResultAndDispatch(@Nullable Object result) {
419419
* @see #getConcurrentResult()
420420
* @see #getConcurrentResultContext()
421421
*/
422-
@SuppressWarnings("NullAway")
422+
@SuppressWarnings("NullAway") // Lambda
423423
public void startDeferredResultProcessing(
424424
final DeferredResult<?> deferredResult, Object... processingContext) throws Exception {
425425

spring-web/src/main/java/org/springframework/web/context/support/ServletContextResourcePatternResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource
104104
* @see ServletContextResource
105105
* @see jakarta.servlet.ServletContext#getResourcePaths
106106
*/
107-
@SuppressWarnings("NullAway")
107+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
108108
protected void doRetrieveMatchingServletContextResources(
109109
ServletContext servletContext, String fullPattern, String dir, Set<Resource> result)
110110
throws IOException {

spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private String trimTrailingSlash(String origin) {
162162
/**
163163
* Variant of {@link #setAllowedOrigins} for adding one origin at a time.
164164
*/
165-
@SuppressWarnings("NullAway")
165+
@SuppressWarnings("NullAway") // Lambda
166166
public void addAllowedOrigin(@Nullable String origin) {
167167
if (origin == null) {
168168
return;
@@ -235,7 +235,7 @@ public CorsConfiguration setAllowedOriginPatterns(@Nullable List<String> allowed
235235
* Variant of {@link #setAllowedOriginPatterns} for adding one origin at a time.
236236
* @since 5.3
237237
*/
238-
@SuppressWarnings("NullAway")
238+
@SuppressWarnings("NullAway") // Lambda
239239
public void addAllowedOriginPattern(@Nullable String originPattern) {
240240
if (originPattern == null) {
241241
return;

spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
public abstract class AbstractMultipartHttpServletRequest extends HttpServletRequestWrapper
4545
implements MultipartHttpServletRequest {
4646

47-
private @Nullable MultiValueMap<String, MultipartFile> multipartFiles;
47+
@SuppressWarnings("NullAway.Init")
48+
private MultiValueMap<String, MultipartFile> multipartFiles;
4849

4950

5051
/**
@@ -135,7 +136,6 @@ protected final void setMultipartFiles(MultiValueMap<String, MultipartFile> mult
135136
* lazily initializing it if necessary.
136137
* @see #initializeMultipart()
137138
*/
138-
@SuppressWarnings("NullAway")
139139
protected MultiValueMap<String, MultipartFile> getMultipartFiles() {
140140
if (this.multipartFiles == null) {
141141
initializeMultipart();

spring-web/src/main/java/org/springframework/web/multipart/support/DefaultMultipartHttpServletRequest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ public class DefaultMultipartHttpServletRequest extends AbstractMultipartHttpSer
4545

4646
private static final String CONTENT_TYPE = "Content-Type";
4747

48-
private @Nullable Map<String, String[]> multipartParameters;
48+
@SuppressWarnings("NullAway.Init")
49+
private Map<String, String[]> multipartParameters;
4950

50-
private @Nullable Map<String, String> multipartParameterContentTypes;
51+
@SuppressWarnings("NullAway.Init")
52+
private Map<String, String> multipartParameterContentTypes;
5153

5254

5355
/**
@@ -164,7 +166,6 @@ protected final void setMultipartParameters(Map<String, String[]> multipartParam
164166
* lazily initializing it if necessary.
165167
* @see #initializeMultipart()
166168
*/
167-
@SuppressWarnings("NullAway")
168169
protected Map<String, String[]> getMultipartParameters() {
169170
if (this.multipartParameters == null) {
170171
initializeMultipart();
@@ -185,7 +186,6 @@ protected final void setMultipartParameterContentTypes(Map<String, String> multi
185186
* lazily initializing it if necessary.
186187
* @see #initializeMultipart()
187188
*/
188-
@SuppressWarnings("NullAway")
189189
protected Map<String, String> getMultipartParameterContentTypes() {
190190
if (this.multipartParameterContentTypes == null) {
191191
initializeMultipart();

spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
*/
5959
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {
6060

61-
private @Nullable Set<String> multipartParameterNames;
61+
@SuppressWarnings("NullAway.Init")
62+
private Set<String> multipartParameterNames;
6263

6364

6465
/**
@@ -138,7 +139,6 @@ protected void initializeMultipart() {
138139
}
139140

140141
@Override
141-
@SuppressWarnings("NullAway")
142142
public Enumeration<String> getParameterNames() {
143143
if (this.multipartParameterNames == null) {
144144
initializeMultipart();
@@ -159,7 +159,6 @@ public Enumeration<String> getParameterNames() {
159159
}
160160

161161
@Override
162-
@SuppressWarnings("NullAway")
163162
public Map<String, String[]> getParameterMap() {
164163
if (this.multipartParameterNames == null) {
165164
initializeMultipart();

spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
8686

8787
private WebSessionManager sessionManager = new DefaultWebSessionManager();
8888

89-
private @Nullable ServerCodecConfigurer codecConfigurer;
89+
@SuppressWarnings("NullAway.Init")
90+
private ServerCodecConfigurer codecConfigurer;
9091

9192
private LocaleContextResolver localeContextResolver = new AcceptHeaderLocaleContextResolver();
9293

@@ -149,7 +150,6 @@ public void setCodecConfigurer(ServerCodecConfigurer codecConfigurer) {
149150
/**
150151
* Return the configured {@link ServerCodecConfigurer}.
151152
*/
152-
@SuppressWarnings("NullAway")
153153
public ServerCodecConfigurer getCodecConfigurer() {
154154
if (this.codecConfigurer == null) {
155155
setCodecConfigurer(ServerCodecConfigurer.create());

spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public InMemoryWebSession(Instant creationTime) {
189189
}
190190

191191
@Override
192-
@SuppressWarnings("NullAway")
192+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
193193
public String getId() {
194194
return this.id.get();
195195
}
@@ -225,7 +225,7 @@ public void start() {
225225
}
226226

227227
@Override
228-
@SuppressWarnings("NullAway")
228+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
229229
public boolean isStarted() {
230230
return this.state.get().equals(State.STARTED) || !getAttributes().isEmpty();
231231
}
@@ -254,7 +254,7 @@ public Mono<Void> invalidate() {
254254
}
255255

256256
@Override
257-
@SuppressWarnings("NullAway")
257+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
258258
public Mono<Void> save() {
259259

260260
checkMaxSessionsLimit();
@@ -292,7 +292,7 @@ public boolean isExpired() {
292292
return isExpired(clock.instant());
293293
}
294294

295-
@SuppressWarnings("NullAway")
295+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
296296
private boolean isExpired(Instant now) {
297297
if (this.state.get().equals(State.EXPIRED)) {
298298
return true;

spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public static HttpRequestValuesInitializer create(
236236
return null;
237237
}
238238

239-
@SuppressWarnings("NullAway")
239+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
240240
private static @Nullable String initUrl(
241241
@Nullable HttpExchange typeAnnotation, HttpExchange methodAnnotation,
242242
@Nullable StringValueResolver embeddedValueResolver) {

0 commit comments

Comments
 (0)