Skip to content

Commit

Permalink
Introduce ClassUtils.isVoidType() utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jan 30, 2024
1 parent 398cc01 commit 067638a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
16 changes: 14 additions & 2 deletions spring-core/src/main/java/org/springframework/util/ClassUtils.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}.
Expand All @@ -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) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -144,7 +145,7 @@ private Flux<DataBuffer> 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);
}

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

/**
Expand Down Expand Up @@ -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() {
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-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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -134,9 +135,7 @@ private static Function<RSocketRequestValues, Object> initResponseFunction(
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 ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public static ResponseFunction create(HttpExchangeAdapter client, Method method)
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;
Expand Down Expand Up @@ -436,7 +436,7 @@ public static ResponseFunction create(ReactorHttpExchangeAdapter client, Method
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()) {
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-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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -155,7 +156,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
}
}

if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
if (ClassUtils.isVoidType(elementType.resolve())) {
return Mono.from((Publisher<Void>) publisher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -202,7 +203,7 @@ public Mono<Void> 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)) {
Expand Down

0 comments on commit 067638a

Please sign in to comment.