001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.expression.spel.ast; 018 019import java.util.List; 020 021import org.springframework.core.convert.TypeDescriptor; 022import org.springframework.util.ClassUtils; 023 024/** 025 * Utility methods (formatters etc) used during parsing and evaluation. 026 * 027 * @author Andy Clement 028 */ 029public class FormatHelper { 030 031 /** 032 * Produce a readable representation for a given method name with specified arguments. 033 * @param name the name of the method 034 * @param argumentTypes the types of the arguments to the method 035 * @return a nicely formatted representation, e.g. {@code foo(String,int)} 036 */ 037 public static String formatMethodForMessage(String name, List<TypeDescriptor> argumentTypes) { 038 StringBuilder sb = new StringBuilder(name); 039 sb.append("("); 040 for (int i = 0; i < argumentTypes.size(); i++) { 041 if (i > 0) { 042 sb.append(","); 043 } 044 TypeDescriptor typeDescriptor = argumentTypes.get(i); 045 if (typeDescriptor != null) { 046 sb.append(formatClassNameForMessage(typeDescriptor.getType())); 047 } 048 else { 049 sb.append(formatClassNameForMessage(null)); 050 } 051 } 052 sb.append(")"); 053 return sb.toString(); 054 } 055 056 /** 057 * Determine a readable name for a given Class object. 058 * <p>A String array will have the formatted name "java.lang.String[]". 059 * @param clazz the Class whose name is to be formatted 060 * @return a formatted String suitable for message inclusion 061 * @see ClassUtils#getQualifiedName(Class) 062 */ 063 public static String formatClassNameForMessage(Class<?> clazz) { 064 return (clazz != null ? ClassUtils.getQualifiedName(clazz) : "null"); 065 } 066 067}