Skip to content

Commit

Permalink
Specify generic type nullness in spring-webflux
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Jan 14, 2025
1 parent 380c9e3 commit 69bfb64
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -63,11 +63,13 @@ public void addFormatters(FormatterRegistry registry) {
}

@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);
}
Expand Down Expand Up @@ -115,11 +117,12 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
}

@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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -232,7 +232,8 @@ public Mono<WebClientResponseException> createException() {
});
}

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -79,20 +79,21 @@ public boolean isEmpty() {
/**
* 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());
}
return result;
}

@Override
protected Collection<?> getContent() {
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1126
protected Collection<? extends @Nullable Object> getContent() {
return (!isEmpty() ? getConditions() : Collections.emptyList());
}

Expand Down Expand Up @@ -150,10 +151,11 @@ private void assertNumberOfConditions(CompositeRequestCondition other) {
}
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -314,7 +314,7 @@ private Mono<HandlerResult> handleException(
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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -306,12 +306,12 @@ protected RequestMappingInfo createRequestMappingInfo(
* 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]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,6 +19,7 @@
import java.beans.PropertyEditor;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -162,8 +163,8 @@ else if (this.expression.endsWith("*")) {
/**
* 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();
Expand Down Expand Up @@ -247,15 +248,17 @@ public boolean isError() {
* 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;
}

/**
* 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") : "");
}

/**
Expand Down

0 comments on commit 69bfb64

Please sign in to comment.