Specify generic type nullness in spring-webflux

See gh-34140
This commit is contained in:
Sébastien Deleuze
2025-01-13 20:54:33 +01:00
parent 380c9e318c
commit 69bfb64dfd
6 changed files with 31 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -63,11 +63,13 @@ public class WebFluxConfigurerComposite implements WebFluxConfigurer {
}
@Override
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1128
public @Nullable Validator getValidator() {
return createSingleBean(WebFluxConfigurer::getValidator, Validator.class);
}
@Override
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1128
public @Nullable MessageCodesResolver getMessageCodesResolver() {
return createSingleBean(WebFluxConfigurer::getMessageCodesResolver, MessageCodesResolver.class);
}
@@ -115,11 +117,12 @@ public class WebFluxConfigurerComposite implements WebFluxConfigurer {
}
@Override
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1128
public @Nullable WebSocketService getWebSocketService() {
return createSingleBean(WebFluxConfigurer::getWebSocketService, WebSocketService.class);
}
private <T> @Nullable T createSingleBean(Function<WebFluxConfigurer, T> factory, Class<T> beanType) {
private <T> @Nullable T createSingleBean(Function<WebFluxConfigurer, @Nullable T> factory, Class<T> beanType) {
List<T> result = this.delegates.stream().map(factory).filter(Objects::nonNull).toList();
if (result.isEmpty()) {
return null;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -232,7 +232,8 @@ class DefaultClientResponse implements ClientResponse {
});
}
private Function<ResolvableType, ?> initDecodeFunction(byte[] body, @Nullable MediaType contentType) {
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1126
private Function<ResolvableType, ? extends @Nullable Object> initDecodeFunction(byte[] body, @Nullable MediaType contentType) {
return targetType -> {
if (ObjectUtils.isEmpty(body)) {
return null;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -79,12 +79,12 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit
/**
* Return the underlying conditions, possibly empty but never {@code null}.
*/
public List<RequestCondition<?>> getConditions() {
public List<@Nullable RequestCondition<?>> getConditions() {
return unwrap();
}
private List<RequestCondition<?>> unwrap() {
List<RequestCondition<?>> result = new ArrayList<>();
private List<@Nullable RequestCondition<?>> unwrap() {
List<@Nullable RequestCondition<?>> result = new ArrayList<>();
for (RequestConditionHolder holder : this.requestConditions) {
result.add(holder.getCondition());
}
@@ -92,7 +92,8 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit
}
@Override
protected Collection<?> getContent() {
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1126
protected Collection<? extends @Nullable Object> getContent() {
return (!isEmpty() ? getConditions() : Collections.emptyList());
}
@@ -150,10 +151,11 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit
}
RequestConditionHolder[] matchingConditions = new RequestConditionHolder[getLength()];
for (int i = 0; i < getLength(); i++) {
matchingConditions[i] = this.requestConditions[i].getMatchingCondition(exchange);
if (matchingConditions[i] == null) {
RequestConditionHolder matchingCondition = this.requestConditions[i].getMatchingCondition(exchange);
if (matchingCondition == null) {
return null;
}
matchingConditions[i] = matchingCondition;
}
return new CompositeRequestCondition(matchingConditions);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -314,7 +314,7 @@ public class RequestMappingHandlerAdapter
Throwable cause = exToExpose.getCause();
exToExpose = (cause != exToExpose ? cause : null);
}
Object[] arguments = new Object[exceptions.size() + 1];
@Nullable Object[] arguments = new Object[exceptions.size() + 1];
exceptions.toArray(arguments); // efficient arraycopy call in ArrayList
arguments[arguments.length - 1] = handlerMethod;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -306,12 +306,12 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
* Resolve placeholder values in the given array of patterns.
* @return a new array with updated patterns
*/
protected String[] resolveEmbeddedValuesInPatterns(String[] patterns) {
protected @Nullable String[] resolveEmbeddedValuesInPatterns(String[] patterns) {
if (this.embeddedValueResolver == null) {
return patterns;
}
else {
String[] resolvedPatterns = new String[patterns.length];
@Nullable String[] resolvedPatterns = new String[patterns.length];
for (int i = 0; i < patterns.length; i++) {
resolvedPatterns[i] = this.embeddedValueResolver.resolveStringValue(patterns[i]);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.web.reactive.result.view;
import java.beans.PropertyEditor;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.jspecify.annotations.Nullable;
@@ -58,7 +59,7 @@ public class BindStatus {
private final @Nullable Errors errors;
private final String[] errorCodes;
private final @Nullable String[] errorCodes;
private String @Nullable [] errorMessages;
@@ -162,8 +163,8 @@ public class BindStatus {
/**
* Extract the error codes from the ObjectError list.
*/
private static String[] initErrorCodes(List<? extends ObjectError> objectErrors) {
String[] errorCodes = new String[objectErrors.size()];
private static @Nullable String[] initErrorCodes(List<? extends ObjectError> objectErrors) {
@Nullable String[] errorCodes = new String[objectErrors.size()];
for (int i = 0; i < objectErrors.size(); i++) {
ObjectError error = objectErrors.get(i);
errorCodes[i] = error.getCode();
@@ -247,7 +248,7 @@ public class BindStatus {
* Return the error codes for the field or object, if any.
* Returns an empty array instead of null if none.
*/
public String[] getErrorCodes() {
public @Nullable String[] getErrorCodes() {
return this.errorCodes;
}
@@ -255,7 +256,9 @@ public class BindStatus {
* Return the first error codes for the field or object, if any.
*/
public String getErrorCode() {
return (!ObjectUtils.isEmpty(this.errorCodes) ? this.errorCodes[0] : "");
return (!ObjectUtils.isEmpty(this.errorCodes) ? Objects.requireNonNull(this.errorCodes[0],
"Error code must not be null") : "");
}
/**