提交 c78960a1 编写于 作者: S Sam Brannen

Merge branch '5.1.x'

...@@ -121,6 +121,7 @@ public abstract class ClassUtils { ...@@ -121,6 +121,7 @@ public abstract class ClassUtils {
primitiveWrapperTypeMap.put(Integer.class, int.class); primitiveWrapperTypeMap.put(Integer.class, int.class);
primitiveWrapperTypeMap.put(Long.class, long.class); primitiveWrapperTypeMap.put(Long.class, long.class);
primitiveWrapperTypeMap.put(Short.class, short.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 // Map entry iteration is less expensive to initialize than forEach with lambdas
for (Map.Entry<Class<?>, Class<?>> entry : primitiveWrapperTypeMap.entrySet()) { for (Map.Entry<Class<?>, Class<?>> entry : primitiveWrapperTypeMap.entrySet()) {
...@@ -463,7 +464,8 @@ public abstract class ClassUtils { ...@@ -463,7 +464,8 @@ public abstract class ClassUtils {
/** /**
* Check if the given class represents a primitive wrapper, * 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 * @param clazz the class to check
* @return whether the given class is a primitive wrapper class * @return whether the given class is a primitive wrapper class
*/ */
...@@ -474,10 +476,12 @@ public abstract class ClassUtils { ...@@ -474,10 +476,12 @@ public abstract class ClassUtils {
/** /**
* Check if the given class represents a primitive (i.e. boolean, byte, * Check if the given class represents a primitive (i.e. boolean, byte,
* char, short, int, long, float, or double) or a primitive wrapper * char, short, int, long, float, or double), {@code void}, or a wrapper for
* (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double). * those types (i.e. Boolean, Byte, Character, Short, Integer, Long, Float,
* Double, or Void).
* @param clazz the class to check * @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) { public static boolean isPrimitiveOrWrapper(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
......
...@@ -40,14 +40,17 @@ import org.springframework.tests.sample.objects.TestObject; ...@@ -40,14 +40,17 @@ import org.springframework.tests.sample.objects.TestObject;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Unit tests for {@link ClassUtils}.
*
* @author Colin Sampaleanu * @author Colin Sampaleanu
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rob Harrop * @author Rob Harrop
* @author Rick Evans * @author Rick Evans
* @author Sam Brannen
*/ */
class ClassUtilsTests { class ClassUtilsTests {
private ClassLoader classLoader = getClass().getClassLoader(); private final ClassLoader classLoader = getClass().getClassLoader();
@BeforeEach @BeforeEach
...@@ -380,6 +383,42 @@ class ClassUtilsTests { ...@@ -380,6 +383,42 @@ class ClassUtilsTests {
assertThat(ClassUtils.determineCommonAncestor(String.class, List.class)).isNull(); 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 { public static class InnerClass {
......
...@@ -162,7 +162,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport ...@@ -162,7 +162,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport
return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) || return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) ||
Model.class.isAssignableFrom(type) || Map.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)); !BeanUtils.isSimpleProperty(type));
} }
......
...@@ -114,7 +114,12 @@ public class ViewResolutionResultHandlerTests { ...@@ -114,7 +114,12 @@ public class ViewResolutionResultHandlerTests {
private void testSupports(MethodParameter returnType, boolean supports) { private void testSupports(MethodParameter returnType, boolean supports) {
ViewResolutionResultHandler resultHandler = resultHandler(mock(ViewResolver.class)); ViewResolutionResultHandler resultHandler = resultHandler(mock(ViewResolver.class));
HandlerResult handlerResult = new HandlerResult(new Object(), null, returnType, this.bindingContext); 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 @Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册