diff --git a/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java b/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java index 384b6cda10..4f80c75084 100644 --- a/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java @@ -583,36 +583,43 @@ public abstract class BeanUtils { } /** - * Check if the given type represents a "simple" property: - * a primitive, a String or other CharSequence, a Number, a Date, - * a Temporal, a URI, a URL, a Locale, a Class, or a corresponding array. + * Check if the given type represents a "simple" property: a simple value + * type or an array of simple value types. + *

See {@link #isSimpleValueType(Class)} for the definition of simple + * value type. *

Used to determine properties to check for a "simple" dependency-check. - * @param clazz the type to check + * @param type the type to check * @return whether the given type represents a "simple" property * @see org.springframework.beans.factory.support.RootBeanDefinition#DEPENDENCY_CHECK_SIMPLE * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#checkDependencies + * @see #isSimpleValueType(Class) */ - public static boolean isSimpleProperty(Class clazz) { - Assert.notNull(clazz, "Class must not be null"); - return isSimpleValueType(clazz) || (clazz.isArray() && isSimpleValueType(clazz.getComponentType())); + public static boolean isSimpleProperty(Class type) { + Assert.notNull(type, "'type' must not be null"); + return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType())); } /** - * Check if the given type represents a "simple" value type: - * a primitive, an enum, a String or other CharSequence, a Number, a Date, - * a Temporal, a URI, a URL, a Locale or a Class. - * @param clazz the type to check + * Check if the given type represents a "simple" value type: a primitive or + * primitive wrapper, an enum, a String or other CharSequence, a Number, a + * Date, a Temporal, a URI, a URL, a Locale, or a Class. + *

{@code Void} and {@code void} are not considered simple value types. + * @param type the type to check * @return whether the given type represents a "simple" value type + * @see #isSimpleProperty(Class) */ - public static boolean isSimpleValueType(Class clazz) { - return (ClassUtils.isPrimitiveOrWrapper(clazz) || - Enum.class.isAssignableFrom(clazz) || - CharSequence.class.isAssignableFrom(clazz) || - Number.class.isAssignableFrom(clazz) || - Date.class.isAssignableFrom(clazz) || - Temporal.class.isAssignableFrom(clazz) || - URI.class == clazz || URL.class == clazz || - Locale.class == clazz || Class.class == clazz); + public static boolean isSimpleValueType(Class type) { + return (type != void.class && type != Void.class && + (ClassUtils.isPrimitiveOrWrapper(type) || + Enum.class.isAssignableFrom(type) || + CharSequence.class.isAssignableFrom(type) || + Number.class.isAssignableFrom(type) || + Date.class.isAssignableFrom(type) || + Temporal.class.isAssignableFrom(type) || + URI.class == type || + URL.class == type || + Locale.class == type || + Class.class == type)); } diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java index d6f67bbcca..bdbfb36909 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java @@ -20,7 +20,13 @@ import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.net.URI; +import java.net.URL; +import java.time.DayOfWeek; +import java.util.Date; import java.util.List; +import java.util.Locale; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -44,6 +50,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException * @author Rob Harrop * @author Chris Beams * @author Sebastien Deleuze + * @author Sam Brannen * @since 19.05.2003 */ public class BeanUtilsTests { @@ -285,6 +292,60 @@ public class BeanUtilsTests { } } + @Test + public void isSimpleValueType() { + Stream.of( + + boolean.class, char.class, byte.class, short.class, int.class, + long.class, float.class, double.class, + + Boolean.class, Character.class, Byte.class, Short.class, Integer.class, + Long.class, Float.class, Double.class, + + DayOfWeek.class, String.class, Date.class, URI.class, URL.class, Locale.class, Class.class + + ).forEach(this::assertIsSimpleValueType); + + Stream.of(int[].class, Object.class, List.class, void.class, Void.class) + .forEach(this::assertIsNotSimpleValueType); + } + + @Test + public void isSimpleProperty() { + Stream.of( + + boolean.class, char.class, byte.class, short.class, int.class, + long.class, float.class, double.class, + + Boolean.class, Character.class, Byte.class, Short.class, Integer.class, + Long.class, Float.class, Double.class, + + DayOfWeek.class, String.class, Date.class, URI.class, URL.class, Locale.class, Class.class, + + boolean[].class, Boolean[].class, Date[].class + + ).forEach(this::assertIsSimpleProperty); + + Stream.of(Object.class, List.class, void.class, Void.class) + .forEach(this::assertIsNotSimpleProperty); + } + + private void assertIsSimpleValueType(Class type) { + assertThat(BeanUtils.isSimpleValueType(type)).as("Type [" + type.getName() + "] should be a simple value type").isTrue(); + } + + private void assertIsNotSimpleValueType(Class type) { + assertThat(BeanUtils.isSimpleValueType(type)).as("Type [" + type.getName() + "] should not be a simple value type").isFalse(); + } + + private void assertIsSimpleProperty(Class type) { + assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should be a simple property").isTrue(); + } + + private void assertIsNotSimpleProperty(Class type) { + assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should not be a simple property").isFalse(); + } + private void assertSignatureEquals(Method desiredMethod, String signature) { assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod); } 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 e0fc31e58e..231bdca82c 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 @@ -160,9 +160,11 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport imp type = result.getReturnType().getGeneric().toClass(); } - return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) || - Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || - void.class == type || Void.class == type || View.class.isAssignableFrom(type) || + return (CharSequence.class.isAssignableFrom(type) || + Rendering.class.isAssignableFrom(type) || + Model.class.isAssignableFrom(type) || + Map.class.isAssignableFrom(type) || + View.class.isAssignableFrom(type) || !BeanUtils.isSimpleProperty(type)); }