Skip to content

Commit 067638a

Browse files
committed
Introduce ClassUtils.isVoidType() utility method
1 parent 398cc01 commit 067638a

File tree

8 files changed

+32
-16
lines changed

8 files changed

+32
-16
lines changed

integration-tests/src/test/java/org/springframework/expression/spel/support/BeanFactoryTypeConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.core.convert.TypeDescriptor;
2828
import org.springframework.core.convert.support.DefaultConversionService;
2929
import org.springframework.expression.TypeConverter;
30+
import org.springframework.util.ClassUtils;
3031

3132
/**
3233
* Copied from Spring Integration for purposes of reproducing
@@ -91,7 +92,7 @@ public boolean canConvert(TypeDescriptor sourceTypeDescriptor, TypeDescriptor ta
9192

9293
@Override
9394
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
94-
if (targetType.getType() == Void.class || targetType.getType() == void.class) {
95+
if (ClassUtils.isVoidType(targetType.getType())) {
9596
return null;
9697
}
9798
if (conversionService.canConvert(sourceType, targetType)) {

spring-core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -549,6 +549,18 @@ public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
549549
return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz);
550550
}
551551

552+
/**
553+
* Determine if the given type represents either {@code Void} or {@code void}.
554+
* @param type the type to check
555+
* @return {@code true} if the type represents {@code Void} or {@code void}
556+
* @since 6.1.4
557+
* @see Void
558+
* @see Void#TYPE
559+
*/
560+
public static boolean isVoidType(Class<?> type) {
561+
return (type == void.class || type == Void.class);
562+
}
563+
552564
/**
553565
* Delegate for {@link org.springframework.beans.BeanUtils#isSimpleValueType}.
554566
* Also used by {@link ObjectUtils#nullSafeConciseToString}.
@@ -565,7 +577,7 @@ public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
565577
* @since 6.1
566578
*/
567579
public static boolean isSimpleValueType(Class<?> type) {
568-
return (Void.class != type && void.class != type &&
580+
return (!isVoidType(type) &&
569581
(isPrimitiveOrWrapper(type) ||
570582
Enum.class.isAssignableFrom(type) ||
571583
CharSequence.class.isAssignableFrom(type) ||

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractEncoderMethodReturnValueHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@
4141
import org.springframework.messaging.MessageHeaders;
4242
import org.springframework.messaging.MessagingException;
4343
import org.springframework.util.Assert;
44+
import org.springframework.util.ClassUtils;
4445
import org.springframework.util.MimeType;
4546

4647
/**
@@ -144,7 +145,7 @@ private Flux<DataBuffer> encodeContent(
144145
ResolvableType.forInstance(content) : returnValueType);
145146
}
146147

147-
if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
148+
if (ClassUtils.isVoidType(elementType.resolve())) {
148149
return Flux.from(publisher).cast(DataBuffer.class);
149150
}
150151

spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import org.springframework.core.io.buffer.DataBufferUtils;
3838
import org.springframework.lang.Nullable;
3939
import org.springframework.util.Assert;
40+
import org.springframework.util.ClassUtils;
4041
import org.springframework.util.MimeType;
4142

4243
/**
@@ -119,7 +120,7 @@ public RequestSpec metadata(Object metadata, @Nullable MimeType mimeType) {
119120
}
120121

121122
private static boolean isVoid(ResolvableType elementType) {
122-
return (Void.class.equals(elementType.resolve()) || void.class.equals(elementType.resolve()));
123+
return ClassUtils.isVoidType(elementType.resolve());
123124
}
124125

125126
private DataBufferFactory bufferFactory() {

spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/RSocketServiceMethod.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838
import org.springframework.messaging.rsocket.RSocketRequester;
3939
import org.springframework.messaging.rsocket.RSocketStrategies;
4040
import org.springframework.util.Assert;
41+
import org.springframework.util.ClassUtils;
4142
import org.springframework.util.MimeType;
4243
import org.springframework.util.StringUtils;
4344
import org.springframework.util.StringValueResolver;
@@ -134,9 +135,7 @@ private static Function<RSocketRequestValues, Object> initResponseFunction(
134135
Class<?> actualType = actualParam.getNestedParameterType();
135136

136137
Function<RSocketRequestValues, Publisher<?>> responseFunction;
137-
if (actualType.equals(void.class) || actualType.equals(Void.class) ||
138-
(reactiveAdapter != null && reactiveAdapter.isNoValue())) {
139-
138+
if (ClassUtils.isVoidType(actualType) || (reactiveAdapter != null && reactiveAdapter.isNoValue())) {
140139
responseFunction = values -> {
141140
RSocketRequester.RetrieveSpec retrieveSpec = initRequest(requester, values);
142141
return (values.getPayload() == null && values.getPayloadValue() == null ?

spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public static ResponseFunction create(HttpExchangeAdapter client, Method method)
350350
Class<?> paramType = param.getNestedParameterType();
351351

352352
Function<HttpRequestValues, Object> responseFunction;
353-
if (paramType.equals(void.class) || paramType.equals(Void.class)) {
353+
if (ClassUtils.isVoidType(paramType)) {
354354
responseFunction = requestValues -> {
355355
client.exchange(requestValues);
356356
return null;
@@ -436,7 +436,7 @@ public static ResponseFunction create(ReactorHttpExchangeAdapter client, Method
436436
Class<?> actualType = isSuspending ? actualParam.getParameterType() : actualParam.getNestedParameterType();
437437

438438
Function<HttpRequestValues, Publisher<?>> responseFunction;
439-
if (actualType.equals(void.class) || actualType.equals(Void.class)) {
439+
if (ClassUtils.isVoidType(actualType)) {
440440
responseFunction = client::exchangeForMono;
441441
}
442442
else if (reactiveAdapter != null && reactiveAdapter.isNoValue()) {

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838
import org.springframework.http.converter.HttpMessageNotWritableException;
3939
import org.springframework.lang.Nullable;
4040
import org.springframework.util.Assert;
41+
import org.springframework.util.ClassUtils;
4142
import org.springframework.util.CollectionUtils;
4243
import org.springframework.web.reactive.HandlerMapping;
4344
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
@@ -155,7 +156,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
155156
}
156157
}
157158

158-
if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
159+
if (ClassUtils.isVoidType(elementType.resolve())) {
159160
return Mono.from((Publisher<Void>) publisher);
160161
}
161162

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.http.MediaType;
4040
import org.springframework.lang.Nullable;
4141
import org.springframework.ui.Model;
42+
import org.springframework.util.ClassUtils;
4243
import org.springframework.util.StringUtils;
4344
import org.springframework.web.bind.annotation.ModelAttribute;
4445
import org.springframework.web.reactive.BindingContext;
@@ -202,7 +203,7 @@ public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result)
202203
clazz = returnValue.getClass();
203204
}
204205

205-
if (returnValue == NO_VALUE || clazz == void.class || clazz == Void.class) {
206+
if (returnValue == NO_VALUE || ClassUtils.isVoidType(clazz)) {
206207
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
207208
}
208209
else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {

0 commit comments

Comments
 (0)