diff --git a/integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java b/integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java index 2a8974959d60..59da59d7ef58 100644 --- a/integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java +++ b/integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java @@ -27,6 +27,7 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.expression.TypeConverter; +import org.springframework.util.ClassUtils; /** * Copied from Spring Integration for purposes of reproducing @@ -91,7 +92,7 @@ public boolean canConvert(TypeDescriptor sourceTypeDescriptor, TypeDescriptor ta @Override public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) { - if (targetType.getType() == Void.class || targetType.getType() == void.class) { + if (ClassUtils.isVoidType(targetType.getType())) { return null; } if (conversionService.canConvert(sourceType, targetType)) { diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index 77fa719717ea..0a0fe6d99322 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -549,6 +549,18 @@ public static Class resolvePrimitiveIfNecessary(Class clazz) { return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz); } + /** + * Determine if the given type represents either {@code Void} or {@code void}. + * @param type the type to check + * @return {@code true} if the type represents {@code Void} or {@code void} + * @since 6.1.4 + * @see Void + * @see Void#TYPE + */ + public static boolean isVoidType(Class type) { + return (type == void.class || type == Void.class); + } + /** * Delegate for {@link org.springframework.beans.BeanUtils#isSimpleValueType}. * Also used by {@link ObjectUtils#nullSafeConciseToString}. @@ -565,7 +577,7 @@ public static Class resolvePrimitiveIfNecessary(Class clazz) { * @since 6.1 */ public static boolean isSimpleValueType(Class type) { - return (Void.class != type && void.class != type && + return (!isVoidType(type) && (isPrimitiveOrWrapper(type) || Enum.class.isAssignableFrom(type) || CharSequence.class.isAssignableFrom(type) || diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractEncoderMethodReturnValueHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractEncoderMethodReturnValueHandler.java index 38536d9a8dad..bfb05d91ed84 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractEncoderMethodReturnValueHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractEncoderMethodReturnValueHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2024 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. @@ -41,6 +41,7 @@ import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessagingException; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.MimeType; /** @@ -144,7 +145,7 @@ private Flux encodeContent( ResolvableType.forInstance(content) : returnValueType); } - if (elementType.resolve() == void.class || elementType.resolve() == Void.class) { + if (ClassUtils.isVoidType(elementType.resolve())) { return Flux.from(publisher).cast(DataBuffer.class); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java index f318abd11307..43ea6d6f64b3 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -37,6 +37,7 @@ import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.MimeType; /** @@ -119,7 +120,7 @@ public RequestSpec metadata(Object metadata, @Nullable MimeType mimeType) { } private static boolean isVoid(ResolvableType elementType) { - return (Void.class.equals(elementType.resolve()) || void.class.equals(elementType.resolve())); + return ClassUtils.isVoidType(elementType.resolve()); } private DataBufferFactory bufferFactory() { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/RSocketServiceMethod.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/RSocketServiceMethod.java index 66c4da3ac339..e4cf46db7ba3 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/RSocketServiceMethod.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/RSocketServiceMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -38,6 +38,7 @@ import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.MimeType; import org.springframework.util.StringUtils; import org.springframework.util.StringValueResolver; @@ -134,9 +135,7 @@ private static Function initResponseFunction( Class actualType = actualParam.getNestedParameterType(); Function> responseFunction; - if (actualType.equals(void.class) || actualType.equals(Void.class) || - (reactiveAdapter != null && reactiveAdapter.isNoValue())) { - + if (ClassUtils.isVoidType(actualType) || (reactiveAdapter != null && reactiveAdapter.isNoValue())) { responseFunction = values -> { RSocketRequester.RetrieveSpec retrieveSpec = initRequest(requester, values); return (values.getPayload() == null && values.getPayloadValue() == null ? diff --git a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java index f715ba50fea8..ca63327d242d 100644 --- a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java +++ b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java @@ -350,7 +350,7 @@ public static ResponseFunction create(HttpExchangeAdapter client, Method method) Class paramType = param.getNestedParameterType(); Function responseFunction; - if (paramType.equals(void.class) || paramType.equals(Void.class)) { + if (ClassUtils.isVoidType(paramType)) { responseFunction = requestValues -> { client.exchange(requestValues); return null; @@ -436,7 +436,7 @@ public static ResponseFunction create(ReactorHttpExchangeAdapter client, Method Class actualType = isSuspending ? actualParam.getParameterType() : actualParam.getNestedParameterType(); Function> responseFunction; - if (actualType.equals(void.class) || actualType.equals(Void.class)) { + if (ClassUtils.isVoidType(actualType)) { responseFunction = client::exchangeForMono; } else if (reactiveAdapter != null && reactiveAdapter.isNoValue()) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java index b7e7339948b2..25556ee76c1c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -38,6 +38,7 @@ import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.accept.RequestedContentTypeResolver; @@ -155,7 +156,7 @@ protected Mono writeBody(@Nullable Object body, MethodParameter bodyParame } } - if (elementType.resolve() == void.class || elementType.resolve() == Void.class) { + if (ClassUtils.isVoidType(elementType.resolve())) { return Mono.from((Publisher) publisher); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java index 85f24c21de29..46bdc86b593e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java @@ -39,6 +39,7 @@ import org.springframework.http.MediaType; import org.springframework.lang.Nullable; import org.springframework.ui.Model; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.reactive.BindingContext; @@ -202,7 +203,7 @@ public Mono handleResult(ServerWebExchange exchange, HandlerResult result) clazz = returnValue.getClass(); } - if (returnValue == NO_VALUE || clazz == void.class || clazz == Void.class) { + if (returnValue == NO_VALUE || ClassUtils.isVoidType(clazz)) { viewsMono = resolveViews(getDefaultViewName(exchange), locale); } else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {