Skip to content

Commit d036b5a

Browse files
committed
Do not treat void and Void as simple types in BeanUtils
Prior to this commit, the isSimpleProperty() and isSimpleValueType() methods in BeanUtils treated void and Void as simple types; however, doing so does not make sense in this context, since void implies the lack of a property or value. This commit addresses this by explicitly excluding void and Void in the logic in isSimpleValueType(). This commit also simplifies the implementation of ViewResolutionResultHandler.supports(HandlerResult) to take advantage of this change. Closes gh-23573
1 parent 3a132f8 commit d036b5a

File tree

3 files changed

+101
-25
lines changed

3 files changed

+101
-25
lines changed

spring-beans/src/main/java/org/springframework/beans/BeanUtils.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -554,35 +554,42 @@ public static MethodParameter getWriteMethodParameter(PropertyDescriptor pd) {
554554
}
555555

556556
/**
557-
* Check if the given type represents a "simple" property:
558-
* a primitive, a String or other CharSequence, a Number, a Date,
559-
* a URI, a URL, a Locale, a Class, or a corresponding array.
557+
* Check if the given type represents a "simple" property: a simple value
558+
* type or an array of simple value types.
559+
* <p>See {@link #isSimpleValueType(Class)} for the definition of <em>simple
560+
* value type</em>.
560561
* <p>Used to determine properties to check for a "simple" dependency-check.
561-
* @param clazz the type to check
562+
* @param type the type to check
562563
* @return whether the given type represents a "simple" property
563564
* @see org.springframework.beans.factory.support.RootBeanDefinition#DEPENDENCY_CHECK_SIMPLE
564565
* @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#checkDependencies
566+
* @see #isSimpleValueType(Class)
565567
*/
566-
public static boolean isSimpleProperty(Class<?> clazz) {
567-
Assert.notNull(clazz, "Class must not be null");
568-
return isSimpleValueType(clazz) || (clazz.isArray() && isSimpleValueType(clazz.getComponentType()));
568+
public static boolean isSimpleProperty(Class<?> type) {
569+
Assert.notNull(type, "'type' must not be null");
570+
return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType()));
569571
}
570572

571573
/**
572-
* Check if the given type represents a "simple" value type:
573-
* a primitive, an enum, a String or other CharSequence, a Number, a Date,
574-
* a URI, a URL, a Locale or a Class.
575-
* @param clazz the type to check
574+
* Check if the given type represents a "simple" value type: a primitive or
575+
* primitive wrapper, an enum, a String or other CharSequence, a Number, a
576+
* Date, a URI, a URL, a Locale, or a Class.
577+
* <p>{@code Void} and {@code void} are not considered simple value types.
578+
* @param type the type to check
576579
* @return whether the given type represents a "simple" value type
580+
* @see #isSimpleProperty(Class)
577581
*/
578-
public static boolean isSimpleValueType(Class<?> clazz) {
579-
return (ClassUtils.isPrimitiveOrWrapper(clazz) ||
580-
Enum.class.isAssignableFrom(clazz) ||
581-
CharSequence.class.isAssignableFrom(clazz) ||
582-
Number.class.isAssignableFrom(clazz) ||
583-
Date.class.isAssignableFrom(clazz) ||
584-
URI.class == clazz || URL.class == clazz ||
585-
Locale.class == clazz || Class.class == clazz);
582+
public static boolean isSimpleValueType(Class<?> type) {
583+
return (type != void.class && type != Void.class &&
584+
(ClassUtils.isPrimitiveOrWrapper(type) ||
585+
Enum.class.isAssignableFrom(type) ||
586+
CharSequence.class.isAssignableFrom(type) ||
587+
Number.class.isAssignableFrom(type) ||
588+
Date.class.isAssignableFrom(type) ||
589+
URI.class == type ||
590+
URL.class == type ||
591+
Locale.class == type ||
592+
Class.class == type));
586593
}
587594

588595

spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -19,8 +19,14 @@
1919
import java.beans.Introspector;
2020
import java.beans.PropertyDescriptor;
2121
import java.lang.reflect.Method;
22+
import java.net.URI;
23+
import java.net.URL;
24+
import java.time.DayOfWeek;
2225
import java.util.ArrayList;
26+
import java.util.Date;
2327
import java.util.List;
28+
import java.util.Locale;
29+
import java.util.stream.Stream;
2430

2531
import org.junit.Test;
2632

@@ -32,14 +38,20 @@
3238
import org.springframework.tests.sample.beans.ITestBean;
3339
import org.springframework.tests.sample.beans.TestBean;
3440

35-
import static org.junit.Assert.*;
41+
import static org.junit.Assert.assertEquals;
42+
import static org.junit.Assert.assertFalse;
43+
import static org.junit.Assert.assertNotNull;
44+
import static org.junit.Assert.assertNull;
45+
import static org.junit.Assert.assertTrue;
46+
import static org.junit.Assert.fail;
3647

3748
/**
3849
* Unit tests for {@link BeanUtils}.
3950
*
4051
* @author Juergen Hoeller
4152
* @author Rob Harrop
4253
* @author Chris Beams
54+
* @author Sam Brannen
4355
* @since 19.05.2003
4456
*/
4557
public class BeanUtilsTests {
@@ -275,6 +287,60 @@ public void testSPR6063() {
275287
}
276288
}
277289

290+
@Test
291+
public void isSimpleValueType() {
292+
Stream.of(
293+
294+
boolean.class, char.class, byte.class, short.class, int.class,
295+
long.class, float.class, double.class,
296+
297+
Boolean.class, Character.class, Byte.class, Short.class, Integer.class,
298+
Long.class, Float.class, Double.class,
299+
300+
DayOfWeek.class, String.class, Date.class, URI.class, URL.class, Locale.class, Class.class
301+
302+
).forEach(this::assertIsSimpleValueType);
303+
304+
Stream.of(int[].class, Object.class, List.class, void.class, Void.class)
305+
.forEach(this::assertIsNotSimpleValueType);
306+
}
307+
308+
@Test
309+
public void isSimpleProperty() {
310+
Stream.of(
311+
312+
boolean.class, char.class, byte.class, short.class, int.class,
313+
long.class, float.class, double.class,
314+
315+
Boolean.class, Character.class, Byte.class, Short.class, Integer.class,
316+
Long.class, Float.class, Double.class,
317+
318+
DayOfWeek.class, String.class, Date.class, URI.class, URL.class, Locale.class, Class.class,
319+
320+
boolean[].class, Boolean[].class, Date[].class
321+
322+
).forEach(this::assertIsSimpleProperty);
323+
324+
Stream.of(Object.class, List.class, void.class, Void.class)
325+
.forEach(this::assertIsNotSimpleProperty);
326+
}
327+
328+
private void assertIsSimpleValueType(Class<?> type) {
329+
assertTrue("Type [" + type.getName() + "] should be a simple value type", BeanUtils.isSimpleValueType(type));
330+
}
331+
332+
private void assertIsNotSimpleValueType(Class<?> type) {
333+
assertFalse("Type [" + type.getName() + "] should not be a simple value type", BeanUtils.isSimpleValueType(type));
334+
}
335+
336+
private void assertIsSimpleProperty(Class<?> type) {
337+
assertTrue("Type [" + type.getName() + "] should be a simple property", BeanUtils.isSimpleProperty(type));
338+
}
339+
340+
private void assertIsNotSimpleProperty(Class<?> type) {
341+
assertFalse("Type [" + type.getName() + "] should not be a simple property", BeanUtils.isSimpleProperty(type));
342+
}
343+
278344
private void assertSignatureEquals(Method desiredMethod, String signature) {
279345
assertEquals(desiredMethod, BeanUtils.resolveSignature(signature, MethodSignatureBean.class));
280346
}
@@ -445,6 +511,7 @@ public void setValue(String aValue) {
445511
}
446512
}
447513

514+
@SuppressWarnings("unused")
448515
private static class BeanWithSingleNonDefaultConstructor {
449516

450517
private final String name;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ public boolean supports(HandlerResult result) {
160160
type = result.getReturnType().getGeneric().toClass();
161161
}
162162

163-
return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) ||
164-
Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) ||
165-
Void.class.equals(type) || void.class.equals(type) || View.class.isAssignableFrom(type) ||
163+
return (CharSequence.class.isAssignableFrom(type) ||
164+
Rendering.class.isAssignableFrom(type) ||
165+
Model.class.isAssignableFrom(type) ||
166+
Map.class.isAssignableFrom(type) ||
167+
View.class.isAssignableFrom(type) ||
166168
!BeanUtils.isSimpleProperty(type));
167169
}
168170

0 commit comments

Comments
 (0)