Skip to content

Commit

Permalink
Refine null-safety in spring-web and spring-websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 25, 2024
1 parent c4b6150 commit a63ebe7
Show file tree
Hide file tree
Showing 59 changed files with 106 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public String getDetailMessageCode() {
}

@Override
@Nullable
public Object[] getDetailMessageArguments() {
return this.detailMessageArguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public String getDetailMessageCode() {
}

@Override
@Nullable
public Object[] getDetailMessageArguments() {
return this.messageDetailArguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected HttpMediaTypeException(String message, List<MediaType> supportedMediaT
* resolving the problem "detail" through a {@code MessageSource}
* @since 6.0
*/
protected HttpMediaTypeException(String message, List<MediaType> supportedMediaTypes,
protected HttpMediaTypeException(@Nullable String message, List<MediaType> supportedMediaTypes,
@Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {

super(message);
Expand Down Expand Up @@ -102,6 +102,7 @@ public String getDetailMessageCode() {
}

@Override
@Nullable
public Object[] getDetailMessageArguments() {
return this.messageDetailArguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public HttpMediaTypeNotSupportedException(String message) {
* @param mediaTypes list of supported media types
* @since 6.0.5
*/
public HttpMediaTypeNotSupportedException(String message, List<MediaType> mediaTypes) {
public HttpMediaTypeNotSupportedException(@Nullable String message, List<MediaType> mediaTypes) {
super(message, mediaTypes, PARSE_ERROR_DETAIL_CODE, null);
this.contentType = null;
this.httpMethod = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)
* @since 4.3
*/
@Override
@Nullable
public MediaType getMediaTypeForResource(Resource resource) {
MediaType mediaType = null;
String mimeType = this.servletContext.getMimeType(resource.getFilename());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ServletRequestBindingException extends ServletException implements
* Constructor with a message only.
* @param msg the detail message
*/
public ServletRequestBindingException(String msg) {
public ServletRequestBindingException(@Nullable String msg) {
this(msg, null, null);
}

Expand All @@ -59,7 +59,7 @@ public ServletRequestBindingException(String msg) {
* @param msg the detail message
* @param cause the root cause
*/
public ServletRequestBindingException(String msg, Throwable cause) {
public ServletRequestBindingException(@Nullable String msg, @Nullable Throwable cause) {
this(msg, cause, null, null);
}

Expand All @@ -73,7 +73,7 @@ public ServletRequestBindingException(String msg, Throwable cause) {
* @since 6.0
*/
protected ServletRequestBindingException(
String msg, @Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {
@Nullable String msg, @Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {

this(msg, null, messageDetailCode, messageDetailArguments);
}
Expand All @@ -88,7 +88,7 @@ protected ServletRequestBindingException(
* resolving the problem "detail" through a {@code MessageSource}
* @since 6.0
*/
protected ServletRequestBindingException(String msg, @Nullable Throwable cause,
protected ServletRequestBindingException(@Nullable String msg, @Nullable Throwable cause,
@Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {

super(msg, cause);
Expand Down Expand Up @@ -118,6 +118,7 @@ public String getDetailMessageCode() {
}

@Override
@Nullable
public Object[] getDetailMessageArguments() {
return this.messageDetailArguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.web.bind.support;

import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.BindParam;
Expand All @@ -32,6 +33,7 @@
public final class BindParamNameResolver implements DataBinder.NameResolver {

@Override
@Nullable
public String resolveName(MethodParameter parameter) {
BindParam bindParam = parameter.getParameterAnnotation(BindParam.class);
if (bindParam != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ protected static void addBindValue(Map<String, Object> params, String key, List<
private record MapValueResolver(Map<String, Object> map) implements ValueResolver {

@Override
@Nullable
public Object resolveValue(String name, Class<?> type) {
return this.map.get(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverte


@Override
@Nullable
@SuppressWarnings({"rawtypes", "unchecked", "resource"})
public T extractData(ClientHttpResponse response) throws IOException {
IntrospectingClientHttpResponse responseWrapper = new IntrospectingClientHttpResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;

import org.springframework.lang.Nullable;

/**
* Exception thrown when an I/O error occurs.
*
Expand All @@ -42,7 +44,7 @@ public ResourceAccessException(String msg) {
* @param msg the message
* @param ex the {@code IOException}
*/
public ResourceAccessException(String msg, IOException ex) {
public ResourceAccessException(String msg, @Nullable IOException ex) {
super(msg, ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.springframework.core.NestedRuntimeException;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;

/**
* Base class for exceptions thrown by {@link RestTemplate} in case a request
Expand Down Expand Up @@ -47,7 +48,7 @@ public RestClientException(String msg) {
* @param msg the message
* @param ex the exception
*/
public RestClientException(String msg, Throwable ex) {
public RestClientException(String msg, @Nullable Throwable ex) {
super(msg, ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.client.RestClient;
import org.springframework.web.service.invoker.HttpExchangeAdapter;
Expand Down Expand Up @@ -69,6 +70,7 @@ public HttpHeaders exchangeForHeaders(HttpRequestValues values) {
}

@Override
@Nullable
public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
return newRequest(values).retrieve().body(bodyType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -102,6 +103,7 @@ protected Map<String, Object> getAttributeMap(int scope) {


@Override
@Nullable
public Object getAttribute(String name, int scope) {
return getAttributeMap(scope).get(name);
}
Expand Down Expand Up @@ -130,6 +132,7 @@ public void registerDestructionCallback(String name, Runnable callback, int scop
}

@Override
@Nullable
public Object resolveReference(String key) {
return switch (key) {
case REFERENCE_REQUEST -> getExternalContext().getRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Object getNativeResponse() {
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <T> T getNativeRequest(@Nullable Class<T> requiredType) {
if (requiredType != null) {
Expand All @@ -70,6 +71,7 @@ public <T> T getNativeRequest(@Nullable Class<T> requiredType) {
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <T> T getNativeResponse(@Nullable Class<T> requiredType) {
if (requiredType != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ private HttpSession obtainSession() {


@Override
@Nullable
public Object getAttribute(String name, int scope) {
if (scope == SCOPE_REQUEST) {
if (!isRequestActive()) {
Expand Down Expand Up @@ -242,6 +243,7 @@ public void registerDestructionCallback(String name, Runnable callback, int scop
}

@Override
@Nullable
public Object resolveReference(String key) {
if (REFERENCE_REQUEST.equals(key)) {
return this.request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,19 @@ public Object getNativeRequest() {
}

@Override
@Nullable
public Object getNativeResponse() {
return getResponse();
}

@Override
@Nullable
public <T> T getNativeRequest(@Nullable Class<T> requiredType) {
return WebUtils.getNativeRequest(getRequest(), requiredType);
}

@Override
@Nullable
public <T> T getNativeResponse(@Nullable Class<T> requiredType) {
HttpServletResponse response = getResponse();
return (response != null ? WebUtils.getNativeResponse(response, requiredType) : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletRes
* container processing thread has exited.
*/
@Override
public void setTimeout(Long timeout) {
public void setTimeout(@Nullable Long timeout) {
Assert.state(!isAsyncStarted(), "Cannot change the timeout with concurrent handling in progress");
this.timeout = timeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public Object getObject() throws Exception {
}

@Override
@Nullable
public Class<?> getObjectType() {
return (this.attribute != null ? this.attribute.getClass() : null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public void setConfigLocations(String... configLocations) {
}

@Override
@Nullable
public String[] getConfigLocations() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected boolean handleInternal(ServerHttpRequest request, ServerHttpResponse r
responseHeaders.setAccessControlAllowMethods(allowMethods);
}

if (preFlightRequest && !allowHeaders.isEmpty()) {
if (preFlightRequest && !CollectionUtils.isEmpty(allowHeaders)) {
responseHeaders.setAccessControlAllowHeaders(allowHeaders);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected boolean handleInternal(ServerWebExchange exchange,
responseHeaders.setAccessControlAllowMethods(allowMethods);
}

if (preFlightRequest && !allowHeaders.isEmpty()) {
if (preFlightRequest && !CollectionUtils.isEmpty(allowHeaders)) {
responseHeaders.setAccessControlAllowHeaders(allowHeaders);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public int getRemotePort() {

@SuppressWarnings("DataFlowIssue")
@Override
@Nullable
public Object getAttribute(String name) {
if (name.equals(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)) {
return this.forwardedPrefixExtractor.getErrorRequestUri();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
return observation;
}

@Nullable
private Throwable unwrapServletException(Throwable ex) {
return (ex instanceof ServletException) ? ex.getCause() : ex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public Method resolveMethodByExceptionType(Class<? extends Throwable> exceptionT
* Return the {@link Method} mapped to the given exception type, or
* {@link #NO_MATCHING_EXCEPTION_HANDLER_METHOD} if none.
*/
@Nullable
private Method getMappedMethod(Class<? extends Throwable> exceptionType) {
List<Class<? extends Throwable>> matches = new ArrayList<>();
for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MethodArgumentConversionNotSupportedException extends ConversionNot


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

super(value, requiredType, cause);
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MethodArgumentTypeMismatchException extends TypeMismatchException {


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

super(value, requiredType, cause);
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public long contentLength() {
}

@Override
@Nullable
public String getFilename() {
return this.multipartFile.getOriginalFilename();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public Map<String, String[]> getParameterMap() {
}

@Override
@Nullable
public String getMultipartContentType(String paramOrFileName) {
MultipartFile file = getFile(paramOrFileName);
if (file != null) {
Expand All @@ -141,6 +142,7 @@ public String getMultipartContentType(String paramOrFileName) {
}

@Override
@Nullable
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
String contentType = getMultipartContentType(paramOrFileName);
if (contentType != null) {
Expand All @@ -167,6 +169,7 @@ protected final void setMultipartParameters(Map<String, String[]> multipartParam
* lazily initializing it if necessary.
* @see #initializeMultipart()
*/
@SuppressWarnings("NullAway")
protected Map<String, String[]> getMultipartParameters() {
if (this.multipartParameters == null) {
initializeMultipart();
Expand All @@ -187,6 +190,7 @@ protected final void setMultipartParameterContentTypes(Map<String, String> multi
* lazily initializing it if necessary.
* @see #initializeMultipart()
*/
@SuppressWarnings("NullAway")
protected Map<String, String> getMultipartParameterContentTypes() {
if (this.multipartParameterContentTypes == null) {
initializeMultipart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public Map<String, String[]> getParameterMap() {
}

@Override
@Nullable
public String getMultipartContentType(String paramOrFileName) {
try {
Part part = getPart(paramOrFileName);
Expand All @@ -187,6 +188,7 @@ public String getMultipartContentType(String paramOrFileName) {
}

@Override
@Nullable
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
try {
Part part = getPart(paramOrFileName);
Expand Down
Loading

0 comments on commit a63ebe7

Please sign in to comment.