Skip to content

Commit a63ebe7

Browse files
committed
Refine null-safety in spring-web and spring-websocket
See spring-projectsgh-32475
1 parent c4b6150 commit a63ebe7

File tree

59 files changed

+106
-23
lines changed

Some content is hidden

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

59 files changed

+106
-23
lines changed

Diff for: spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public String getDetailMessageCode() {
211211
}
212212

213213
@Override
214+
@Nullable
214215
public Object[] getDetailMessageArguments() {
215216
return this.detailMessageArguments;
216217
}

Diff for: spring-web/src/main/java/org/springframework/web/ErrorResponseException.java

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public String getDetailMessageCode() {
165165
}
166166

167167
@Override
168+
@Nullable
168169
public Object[] getDetailMessageArguments() {
169170
return this.messageDetailArguments;
170171
}

Diff for: spring-web/src/main/java/org/springframework/web/HttpMediaTypeException.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected HttpMediaTypeException(String message, List<MediaType> supportedMediaT
7373
* resolving the problem "detail" through a {@code MessageSource}
7474
* @since 6.0
7575
*/
76-
protected HttpMediaTypeException(String message, List<MediaType> supportedMediaTypes,
76+
protected HttpMediaTypeException(@Nullable String message, List<MediaType> supportedMediaTypes,
7777
@Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {
7878

7979
super(message);
@@ -102,6 +102,7 @@ public String getDetailMessageCode() {
102102
}
103103

104104
@Override
105+
@Nullable
105106
public Object[] getDetailMessageArguments() {
106107
return this.messageDetailArguments;
107108
}

Diff for: spring-web/src/main/java/org/springframework/web/HttpMediaTypeNotSupportedException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public HttpMediaTypeNotSupportedException(String message) {
6363
* @param mediaTypes list of supported media types
6464
* @since 6.0.5
6565
*/
66-
public HttpMediaTypeNotSupportedException(String message, List<MediaType> mediaTypes) {
66+
public HttpMediaTypeNotSupportedException(@Nullable String message, List<MediaType> mediaTypes) {
6767
super(message, mediaTypes, PARSE_ERROR_DETAIL_CODE, null);
6868
this.contentType = null;
6969
this.httpMethod = null;

Diff for: spring-web/src/main/java/org/springframework/web/accept/ServletPathExtensionContentNegotiationStrategy.java

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)
9999
* @since 4.3
100100
*/
101101
@Override
102+
@Nullable
102103
public MediaType getMediaTypeForResource(Resource resource) {
103104
MediaType mediaType = null;
104105
String mimeType = this.servletContext.getMimeType(resource.getFilename());

Diff for: spring-web/src/main/java/org/springframework/web/bind/ServletRequestBindingException.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class ServletRequestBindingException extends ServletException implements
5050
* Constructor with a message only.
5151
* @param msg the detail message
5252
*/
53-
public ServletRequestBindingException(String msg) {
53+
public ServletRequestBindingException(@Nullable String msg) {
5454
this(msg, null, null);
5555
}
5656

@@ -59,7 +59,7 @@ public ServletRequestBindingException(String msg) {
5959
* @param msg the detail message
6060
* @param cause the root cause
6161
*/
62-
public ServletRequestBindingException(String msg, Throwable cause) {
62+
public ServletRequestBindingException(@Nullable String msg, @Nullable Throwable cause) {
6363
this(msg, cause, null, null);
6464
}
6565

@@ -73,7 +73,7 @@ public ServletRequestBindingException(String msg, Throwable cause) {
7373
* @since 6.0
7474
*/
7575
protected ServletRequestBindingException(
76-
String msg, @Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {
76+
@Nullable String msg, @Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {
7777

7878
this(msg, null, messageDetailCode, messageDetailArguments);
7979
}
@@ -88,7 +88,7 @@ protected ServletRequestBindingException(
8888
* resolving the problem "detail" through a {@code MessageSource}
8989
* @since 6.0
9090
*/
91-
protected ServletRequestBindingException(String msg, @Nullable Throwable cause,
91+
protected ServletRequestBindingException(@Nullable String msg, @Nullable Throwable cause,
9292
@Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {
9393

9494
super(msg, cause);
@@ -118,6 +118,7 @@ public String getDetailMessageCode() {
118118
}
119119

120120
@Override
121+
@Nullable
121122
public Object[] getDetailMessageArguments() {
122123
return this.messageDetailArguments;
123124
}

Diff for: spring-web/src/main/java/org/springframework/web/bind/support/BindParamNameResolver.java

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.bind.support;
1818

1919
import org.springframework.core.MethodParameter;
20+
import org.springframework.lang.Nullable;
2021
import org.springframework.util.StringUtils;
2122
import org.springframework.validation.DataBinder;
2223
import org.springframework.web.bind.annotation.BindParam;
@@ -32,6 +33,7 @@
3233
public final class BindParamNameResolver implements DataBinder.NameResolver {
3334

3435
@Override
36+
@Nullable
3537
public String resolveName(MethodParameter parameter) {
3638
BindParam bindParam = parameter.getParameterAnnotation(BindParam.class);
3739
if (bindParam != null) {

Diff for: spring-web/src/main/java/org/springframework/web/bind/support/WebExchangeDataBinder.java

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ protected static void addBindValue(Map<String, Object> params, String key, List<
162162
private record MapValueResolver(Map<String, Object> map) implements ValueResolver {
163163

164164
@Override
165+
@Nullable
165166
public Object resolveValue(String name, Class<?> type) {
166167
return this.map.get(name);
167168
}

Diff for: spring-web/src/main/java/org/springframework/web/client/HttpMessageConverterExtractor.java

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverte
8484

8585

8686
@Override
87+
@Nullable
8788
@SuppressWarnings({"rawtypes", "unchecked", "resource"})
8889
public T extractData(ClientHttpResponse response) throws IOException {
8990
IntrospectingClientHttpResponse responseWrapper = new IntrospectingClientHttpResponse(response);

Diff for: spring-web/src/main/java/org/springframework/web/client/ResourceAccessException.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020

21+
import org.springframework.lang.Nullable;
22+
2123
/**
2224
* Exception thrown when an I/O error occurs.
2325
*
@@ -42,7 +44,7 @@ public ResourceAccessException(String msg) {
4244
* @param msg the message
4345
* @param ex the {@code IOException}
4446
*/
45-
public ResourceAccessException(String msg, IOException ex) {
47+
public ResourceAccessException(String msg, @Nullable IOException ex) {
4648
super(msg, ex);
4749
}
4850

Diff for: spring-web/src/main/java/org/springframework/web/client/RestClientException.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.core.NestedRuntimeException;
2020
import org.springframework.http.client.ClientHttpResponse;
21+
import org.springframework.lang.Nullable;
2122

2223
/**
2324
* Base class for exceptions thrown by {@link RestTemplate} in case a request
@@ -47,7 +48,7 @@ public RestClientException(String msg) {
4748
* @param msg the message
4849
* @param ex the exception
4950
*/
50-
public RestClientException(String msg, Throwable ex) {
51+
public RestClientException(String msg, @Nullable Throwable ex) {
5152
super(msg, ex);
5253
}
5354

Diff for: spring-web/src/main/java/org/springframework/web/client/support/RestClientAdapter.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.http.HttpHeaders;
2626
import org.springframework.http.HttpMethod;
2727
import org.springframework.http.ResponseEntity;
28+
import org.springframework.lang.Nullable;
2829
import org.springframework.util.Assert;
2930
import org.springframework.web.client.RestClient;
3031
import org.springframework.web.service.invoker.HttpExchangeAdapter;
@@ -69,6 +70,7 @@ public HttpHeaders exchangeForHeaders(HttpRequestValues values) {
6970
}
7071

7172
@Override
73+
@Nullable
7274
public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
7375
return newRequest(values).retrieve().body(bodyType);
7476
}

Diff for: spring-web/src/main/java/org/springframework/web/context/request/FacesRequestAttributes.java

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626

27+
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
2829
import org.springframework.util.ReflectionUtils;
2930
import org.springframework.util.StringUtils;
@@ -102,6 +103,7 @@ protected Map<String, Object> getAttributeMap(int scope) {
102103

103104

104105
@Override
106+
@Nullable
105107
public Object getAttribute(String name, int scope) {
106108
return getAttributeMap(scope).get(name);
107109
}
@@ -130,6 +132,7 @@ public void registerDestructionCallback(String name, Runnable callback, int scop
130132
}
131133

132134
@Override
135+
@Nullable
133136
public Object resolveReference(String key) {
134137
return switch (key) {
135138
case REFERENCE_REQUEST -> getExternalContext().getRequest();

Diff for: spring-web/src/main/java/org/springframework/web/context/request/FacesWebRequest.java

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public Object getNativeResponse() {
5858
}
5959

6060
@Override
61+
@Nullable
6162
@SuppressWarnings("unchecked")
6263
public <T> T getNativeRequest(@Nullable Class<T> requiredType) {
6364
if (requiredType != null) {
@@ -70,6 +71,7 @@ public <T> T getNativeRequest(@Nullable Class<T> requiredType) {
7071
}
7172

7273
@Override
74+
@Nullable
7375
@SuppressWarnings("unchecked")
7476
public <T> T getNativeResponse(@Nullable Class<T> requiredType) {
7577
if (requiredType != null) {

Diff for: spring-web/src/main/java/org/springframework/web/context/request/ServletRequestAttributes.java

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ private HttpSession obtainSession() {
143143

144144

145145
@Override
146+
@Nullable
146147
public Object getAttribute(String name, int scope) {
147148
if (scope == SCOPE_REQUEST) {
148149
if (!isRequestActive()) {
@@ -242,6 +243,7 @@ public void registerDestructionCallback(String name, Runnable callback, int scop
242243
}
243244

244245
@Override
246+
@Nullable
245247
public Object resolveReference(String key) {
246248
if (REFERENCE_REQUEST.equals(key)) {
247249
return this.request;

Diff for: spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java

+3
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,19 @@ public Object getNativeRequest() {
9898
}
9999

100100
@Override
101+
@Nullable
101102
public Object getNativeResponse() {
102103
return getResponse();
103104
}
104105

105106
@Override
107+
@Nullable
106108
public <T> T getNativeRequest(@Nullable Class<T> requiredType) {
107109
return WebUtils.getNativeRequest(getRequest(), requiredType);
108110
}
109111

110112
@Override
113+
@Nullable
111114
public <T> T getNativeResponse(@Nullable Class<T> requiredType) {
112115
HttpServletResponse response = getResponse();
113116
return (response != null ? WebUtils.getNativeResponse(response, requiredType) : null);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletRes
101101
* container processing thread has exited.
102102
*/
103103
@Override
104-
public void setTimeout(Long timeout) {
104+
public void setTimeout(@Nullable Long timeout) {
105105
Assert.state(!isAsyncStarted(), "Cannot change the timeout with concurrent handling in progress");
106106
this.timeout = timeout;
107107
}

Diff for: spring-web/src/main/java/org/springframework/web/context/support/ServletContextAttributeFactoryBean.java

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public Object getObject() throws Exception {
7777
}
7878

7979
@Override
80+
@Nullable
8081
public Class<?> getObjectType() {
8182
return (this.attribute != null ? this.attribute.getClass() : null);
8283
}

Diff for: spring-web/src/main/java/org/springframework/web/context/support/StaticWebApplicationContext.java

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public void setConfigLocations(String... configLocations) {
139139
}
140140

141141
@Override
142+
@Nullable
142143
public String[] getConfigLocations() {
143144
return null;
144145
}

Diff for: spring-web/src/main/java/org/springframework/web/cors/DefaultCorsProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected boolean handleInternal(ServerHttpRequest request, ServerHttpResponse r
155155
responseHeaders.setAccessControlAllowMethods(allowMethods);
156156
}
157157

158-
if (preFlightRequest && !allowHeaders.isEmpty()) {
158+
if (preFlightRequest && !CollectionUtils.isEmpty(allowHeaders)) {
159159
responseHeaders.setAccessControlAllowHeaders(allowHeaders);
160160
}
161161

Diff for: spring-web/src/main/java/org/springframework/web/cors/reactive/DefaultCorsProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected boolean handleInternal(ServerWebExchange exchange,
153153
responseHeaders.setAccessControlAllowMethods(allowMethods);
154154
}
155155

156-
if (preFlightRequest && !allowHeaders.isEmpty()) {
156+
if (preFlightRequest && !CollectionUtils.isEmpty(allowHeaders)) {
157157
responseHeaders.setAccessControlAllowHeaders(allowHeaders);
158158
}
159159

Diff for: spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ public int getRemotePort() {
340340

341341
@SuppressWarnings("DataFlowIssue")
342342
@Override
343+
@Nullable
343344
public Object getAttribute(String name) {
344345
if (name.equals(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)) {
345346
return this.forwardedPrefixExtractor.getErrorRequestUri();

Diff for: spring-web/src/main/java/org/springframework/web/filter/ServerHttpObservationFilter.java

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
139139
return observation;
140140
}
141141

142+
@Nullable
142143
private Throwable unwrapServletException(Throwable ex) {
143144
return (ex instanceof ServletException) ? ex.getCause() : ex;
144145
}

Diff for: spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public Method resolveMethodByExceptionType(Class<? extends Throwable> exceptionT
174174
* Return the {@link Method} mapped to the given exception type, or
175175
* {@link #NO_MATCHING_EXCEPTION_HANDLER_METHOD} if none.
176176
*/
177+
@Nullable
177178
private Method getMappedMethod(Class<? extends Throwable> exceptionType) {
178179
List<Class<? extends Throwable>> matches = new ArrayList<>();
179180
for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) {

Diff for: spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentConversionNotSupportedException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class MethodArgumentConversionNotSupportedException extends ConversionNot
3737

3838

3939
public MethodArgumentConversionNotSupportedException(@Nullable Object value,
40-
@Nullable Class<?> requiredType, String name, MethodParameter param, Throwable cause) {
40+
@Nullable Class<?> requiredType, String name, MethodParameter param, @Nullable Throwable cause) {
4141

4242
super(value, requiredType, cause);
4343
this.name = name;

Diff for: spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentTypeMismatchException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class MethodArgumentTypeMismatchException extends TypeMismatchException {
3737

3838

3939
public MethodArgumentTypeMismatchException(@Nullable Object value,
40-
@Nullable Class<?> requiredType, String name, MethodParameter param, Throwable cause) {
40+
@Nullable Class<?> requiredType, String name, MethodParameter param, @Nullable Throwable cause) {
4141

4242
super(value, requiredType, cause);
4343
this.name = name;

Diff for: spring-web/src/main/java/org/springframework/web/multipart/MultipartFileResource.java

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public long contentLength() {
6565
}
6666

6767
@Override
68+
@Nullable
6869
public String getFilename() {
6970
return this.multipartFile.getOriginalFilename();
7071
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public Map<String, String[]> getParameterMap() {
130130
}
131131

132132
@Override
133+
@Nullable
133134
public String getMultipartContentType(String paramOrFileName) {
134135
MultipartFile file = getFile(paramOrFileName);
135136
if (file != null) {
@@ -141,6 +142,7 @@ public String getMultipartContentType(String paramOrFileName) {
141142
}
142143

143144
@Override
145+
@Nullable
144146
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
145147
String contentType = getMultipartContentType(paramOrFileName);
146148
if (contentType != null) {
@@ -167,6 +169,7 @@ protected final void setMultipartParameters(Map<String, String[]> multipartParam
167169
* lazily initializing it if necessary.
168170
* @see #initializeMultipart()
169171
*/
172+
@SuppressWarnings("NullAway")
170173
protected Map<String, String[]> getMultipartParameters() {
171174
if (this.multipartParameters == null) {
172175
initializeMultipart();
@@ -187,6 +190,7 @@ protected final void setMultipartParameterContentTypes(Map<String, String> multi
187190
* lazily initializing it if necessary.
188191
* @see #initializeMultipart()
189192
*/
193+
@SuppressWarnings("NullAway")
190194
protected Map<String, String> getMultipartParameterContentTypes() {
191195
if (this.multipartParameterContentTypes == null) {
192196
initializeMultipart();

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

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public Map<String, String[]> getParameterMap() {
176176
}
177177

178178
@Override
179+
@Nullable
179180
public String getMultipartContentType(String paramOrFileName) {
180181
try {
181182
Part part = getPart(paramOrFileName);
@@ -187,6 +188,7 @@ public String getMultipartContentType(String paramOrFileName) {
187188
}
188189

189190
@Override
191+
@Nullable
190192
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
191193
try {
192194
Part part = getPart(paramOrFileName);

0 commit comments

Comments
 (0)