From 5f90a7eee9df803bb866c74b47682f076b3e4561 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 10 Feb 2017 10:53:08 +0100 Subject: [PATCH] Delegate to common ClassUtils.getQualifiedName Issue: SPR-15237 (cherry picked from commit 81aca78) --- .../expression/spel/ast/FormatHelper.java | 37 +++++-------------- .../spel/support/ReflectionHelperTests.java | 5 ++- 2 files changed, 13 insertions(+), 29 deletions(-) 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..57f00826c6d 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. @@ -19,6 +19,7 @@ package org.springframework.expression.spel.ast; import java.util.List; import org.springframework.core.convert.TypeDescriptor; +import org.springframework.util.ClassUtils; /** * Utility methods (formatters, etc) used during parsing and evaluation. @@ -28,10 +29,10 @@ import org.springframework.core.convert.TypeDescriptor; public class FormatHelper { /** - * Produce a nice string for a given method name with specified arguments. + * Produce a readable representation for a given method name with specified arguments. * @param name the name of the method * @param argumentTypes the types of the arguments to the method - * @return nicely formatted string, eg. foo(String,int) + * @return a nicely formatted representation, e.g. {@code foo(String,int)} */ public static String formatMethodForMessage(String name, List argumentTypes) { StringBuilder sb = new StringBuilder(name); @@ -53,32 +54,14 @@ public class FormatHelper { } /** - * Produce a nice string for a given class object. - * For example, a string array will have the formatted name "java.lang.String[]". - * @param clazz The class whose name is to be formatted - * @return a formatted string suitable for message inclusion + * Determine a readable name for a given Class object. + *

A String array will have the formatted name "java.lang.String[]". + * @param clazz the Class whose name is to be formatted + * @return a formatted String suitable for message inclusion + * @see ClassUtils#getQualifiedName(Class) */ public static String formatClassNameForMessage(Class clazz) { - 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 != null ? ClassUtils.getQualifiedName(clazz) : "null"); } } 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 c02949d1970..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-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. @@ -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)); @@ -535,7 +536,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests { } private List getTypeDescriptors(Class... types) { - List typeDescriptors = new ArrayList(types.length); + List typeDescriptors = new ArrayList<>(types.length); for (Class type : types) { typeDescriptors.add(TypeDescriptor.valueOf(type)); }