Introduce ClassUtils.isVoidType() utility method

This commit is contained in:
Sam Brannen 2024-01-30 16:27:34 +01:00
parent 398cc01650
commit 067638ae6e
8 changed files with 32 additions and 16 deletions

View File

@ -27,6 +27,7 @@ import org.springframework.core.convert.ConversionService;
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 @@ class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware {
@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)) {

View File

@ -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 abstract class ClassUtils {
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 abstract class ClassUtils {
* @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) ||

View File

@ -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.Message;
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 @@ public abstract class AbstractEncoderMethodReturnValueHandler implements Handler
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);
}

View File

@ -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.DataBufferFactory;
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 @@ final class DefaultRSocketRequester implements RSocketRequester {
}
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() {

View File

@ -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.lang.Nullable;
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 @@ final class RSocketServiceMethod {
Class<?> actualType = actualParam.getNestedParameterType();
Function<RSocketRequestValues, Publisher<?>> 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 ?

View File

@ -350,7 +350,7 @@ final class HttpServiceMethod {
Class<?> paramType = param.getNestedParameterType();
Function<HttpRequestValues, Object> 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 @@ final class HttpServiceMethod {
Class<?> actualType = isSuspending ? actualParam.getParameterType() : actualParam.getNestedParameterType();
Function<HttpRequestValues, Publisher<?>> responseFunction;
if (actualType.equals(void.class) || actualType.equals(Void.class)) {
if (ClassUtils.isVoidType(actualType)) {
responseFunction = client::exchangeForMono;
}
else if (reactiveAdapter != null && reactiveAdapter.isNoValue()) {

View File

@ -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.codec.HttpMessageWriter;
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 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa
}
}
if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
if (ClassUtils.isVoidType(elementType.resolve())) {
return Mono.from((Publisher<Void>) publisher);
}

View File

@ -39,6 +39,7 @@ import org.springframework.http.HttpStatusCode;
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 class ViewResolutionResultHandler extends HandlerResultHandlerSupport imp
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)) {