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 11197d2b55..d0c83bd92d 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -121,6 +121,7 @@ public abstract class ClassUtils { primitiveWrapperTypeMap.put(Integer.class, int.class); primitiveWrapperTypeMap.put(Long.class, long.class); primitiveWrapperTypeMap.put(Short.class, short.class); + primitiveWrapperTypeMap.put(Void.class, void.class); // Map entry iteration is less expensive to initialize than forEach with lambdas for (Map.Entry, Class> entry : primitiveWrapperTypeMap.entrySet()) { @@ -463,7 +464,8 @@ public abstract class ClassUtils { /** * Check if the given class represents a primitive wrapper, - * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double. + * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, Double, or + * Void. * @param clazz the class to check * @return whether the given class is a primitive wrapper class */ @@ -474,10 +476,12 @@ public abstract class ClassUtils { /** * Check if the given class represents a primitive (i.e. boolean, byte, - * char, short, int, long, float, or double) or a primitive wrapper - * (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double). + * char, short, int, long, float, or double), {@code void}, or a wrapper for + * those types (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, + * Double, or Void). * @param clazz the class to check - * @return whether the given class is a primitive or primitive wrapper class + * @return {@code true} if the given class represents a primitive, void, or + * a wrapper class */ public static boolean isPrimitiveOrWrapper(Class clazz) { Assert.notNull(clazz, "Class must not be null"); diff --git a/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java b/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java index 2b58da23b6..709c7bace1 100644 --- a/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java @@ -40,14 +40,17 @@ import org.springframework.tests.sample.objects.TestObject; import static org.assertj.core.api.Assertions.assertThat; /** + * Unit tests for {@link ClassUtils}. + * * @author Colin Sampaleanu * @author Juergen Hoeller * @author Rob Harrop * @author Rick Evans + * @author Sam Brannen */ class ClassUtilsTests { - private ClassLoader classLoader = getClass().getClassLoader(); + private final ClassLoader classLoader = getClass().getClassLoader(); @BeforeEach @@ -380,6 +383,42 @@ class ClassUtilsTests { assertThat(ClassUtils.determineCommonAncestor(String.class, List.class)).isNull(); } + @Test + public void isPrimitiveWrapper() { + assertThat(ClassUtils.isPrimitiveWrapper(Boolean.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Character.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Byte.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Short.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Integer.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Long.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Float.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Double.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveWrapper(Void.class)).isTrue(); + } + + @Test + public void isPrimitiveOrWrapper() { + assertThat(ClassUtils.isPrimitiveOrWrapper(boolean.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(char.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(byte.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(short.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(int.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(long.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(float.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(double.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(void.class)).isTrue(); + + assertThat(ClassUtils.isPrimitiveOrWrapper(Boolean.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Character.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Byte.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Short.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Integer.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Long.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Float.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Double.class)).isTrue(); + assertThat(ClassUtils.isPrimitiveOrWrapper(Void.class)).isTrue(); + } + public static class InnerClass { 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 691fac712b..66637a2ac8 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 @@ -162,7 +162,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) || Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || - void.class.equals(type) || View.class.isAssignableFrom(type) || + Void.class.equals(type) || void.class.equals(type) || View.class.isAssignableFrom(type) || !BeanUtils.isSimpleProperty(type)); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java index 8e5cd2ef01..386ed8b30b 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java @@ -114,7 +114,12 @@ public class ViewResolutionResultHandlerTests { private void testSupports(MethodParameter returnType, boolean supports) { ViewResolutionResultHandler resultHandler = resultHandler(mock(ViewResolver.class)); HandlerResult handlerResult = new HandlerResult(new Object(), null, returnType, this.bindingContext); - assertThat(resultHandler.supports(handlerResult)).isEqualTo(supports); + if (supports) { + assertThat(resultHandler.supports(handlerResult)).as("return type [" + returnType + "] should be supported").isEqualTo(supports); + } + else { + assertThat(resultHandler.supports(handlerResult)).as("return type [" + returnType + "] should not be supported").isNotEqualTo(supports); + } } @Test