From 69bfb64dfd314bda98190499b939aadef5658533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 13 Jan 2025 20:54:33 +0100 Subject: [PATCH] Specify generic type nullness in spring-webflux See gh-34140 --- .../config/WebFluxConfigurerComposite.java | 7 +++++-- .../function/client/DefaultClientResponse.java | 5 +++-- .../condition/CompositeRequestCondition.java | 16 +++++++++------- .../annotation/RequestMappingHandlerAdapter.java | 4 ++-- .../annotation/RequestMappingHandlerMapping.java | 6 +++--- .../web/reactive/result/view/BindStatus.java | 15 +++++++++------ 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurerComposite.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurerComposite.java index 8c72207226..3a3e9f2757 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurerComposite.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurerComposite.java @@ -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 @Nullable T createSingleBean(Function factory, Class beanType) { + private @Nullable T createSingleBean(Function factory, Class beanType) { List result = this.delegates.stream().map(factory).filter(Objects::nonNull).toList(); if (result.isEmpty()) { return null; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java index 907d394190..c7cc077738 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java @@ -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 initDecodeFunction(byte[] body, @Nullable MediaType contentType) { + @SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1126 + private Function initDecodeFunction(byte[] body, @Nullable MediaType contentType) { return targetType -> { if (ObjectUtils.isEmpty(body)) { return null; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/CompositeRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/CompositeRequestCondition.java index 364a4493a4..d3e4ce35d9 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/CompositeRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/CompositeRequestCondition.java @@ -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> getConditions() { + public List<@Nullable RequestCondition> getConditions() { return unwrap(); } - private List> unwrap() { - List> 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 getContent() { + @SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1126 + protected Collection getContent() { return (!isEmpty() ? getConditions() : Collections.emptyList()); } @@ -150,10 +151,11 @@ public class CompositeRequestCondition extends AbstractRequestCondition objectErrors) { - String[] errorCodes = new String[objectErrors.size()]; + private static @Nullable String[] initErrorCodes(List 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") : ""); } /**