From 5cfa7e71bb878cde25b03b79cd5b9c58f12a12df Mon Sep 17 00:00:00 2001 From: Christoph Dreis Date: Wed, 8 Feb 2017 20:36:41 +0100 Subject: [PATCH] Use Class.getTypeName() where appropriate Issue: SPR-15237 --- .../org/springframework/util/ClassUtils.java | 33 +++---------------- .../expression/spel/ast/FormatHelper.java | 20 ++--------- .../spel/support/ReflectionHelperTests.java | 3 +- 3 files changed, 8 insertions(+), 48 deletions(-) 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 2c02cd57a0e..3f8eedc60e5 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -480,28 +480,7 @@ public abstract class ClassUtils { */ public static String getQualifiedName(Class clazz) { Assert.notNull(clazz, "Class must not be null"); - if (clazz.isArray()) { - return getQualifiedNameForArray(clazz); - } - else { - return clazz.getName(); - } - } - - /** - * Build a nice qualified name for an array: - * component type class name + "[]". - * @param clazz the array class - * @return a qualified name for the array class - */ - private static String getQualifiedNameForArray(Class clazz) { - StringBuilder result = new StringBuilder(); - while (clazz.isArray()) { - clazz = clazz.getComponentType(); - result.append(ARRAY_SUFFIX); - } - result.insert(0, clazz.getName()); - return result.toString(); + return clazz.getTypeName(); } /** @@ -552,11 +531,8 @@ public abstract class ClassUtils { } return result.toString(); } - else if (clazz.isArray()) { - return getQualifiedNameForArray(clazz); - } else { - return clazz.getName(); + return clazz.getTypeName(); } } @@ -567,8 +543,7 @@ public abstract class ClassUtils { */ public static boolean matchesTypeName(Class clazz, String typeName) { return (typeName != null && - (typeName.equals(clazz.getName()) || typeName.equals(clazz.getSimpleName()) || - (clazz.isArray() && typeName.equals(getQualifiedNameForArray(clazz))))); + (typeName.equals(clazz.getTypeName()) || typeName.equals(clazz.getSimpleName()))); } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java index 441e7e14749..12895fc3ae7 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,23 +62,7 @@ public class FormatHelper { if (clazz == null) { return "null"; } - if (clazz.isArray()) { - StringBuilder sb = new StringBuilder(); - int dims = 1; - Class baseClass = clazz.getComponentType(); - while (baseClass.isArray()) { - baseClass = baseClass.getComponentType(); - dims++; - } - sb.append(baseClass.getName()); - for (int i = 0; i < dims; i++) { - sb.append("[]"); - } - return sb.toString(); - } - else { - return clazz.getName(); - } + return clazz.getTypeName(); } } diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/support/ReflectionHelperTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/support/ReflectionHelperTests.java index bcd5dbb362e..14f741052d6 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/support/ReflectionHelperTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/support/ReflectionHelperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,6 +48,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests { public void testFormatHelperForClassName() { assertEquals("java.lang.String",FormatHelper.formatClassNameForMessage(String.class)); assertEquals("java.lang.String[]",FormatHelper.formatClassNameForMessage(new String[1].getClass())); + assertEquals("java.lang.String[][]",FormatHelper.formatClassNameForMessage(new String[1][1].getClass())); assertEquals("int[]",FormatHelper.formatClassNameForMessage(new int[1].getClass())); assertEquals("int[][]",FormatHelper.formatClassNameForMessage(new int[1][2].getClass())); assertEquals("null",FormatHelper.formatClassNameForMessage(null));