View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.util.codingsupport;
19  
20  import java.lang.reflect.Method;
21  
22  import org.springframework.util.StringUtils;
23  
24  /**
25   * This class helps transforming a class or class related structures into a
26   * string representation.
27   *
28   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/codingsupport/ClassUtils.java $
29   *
30   * @author Andreas Bur (ABU)
31   */
32  public final class ClassUtils {
33  	
34  	/** Char used to mark a boolean array. */
35  	private static final char BOOLEAN_ELEMENT = 'Z';
36  	
37  	/** Char used to mark a byte array. */
38  	private static final char BYTE_ELEMENT = 'B';
39  	
40  	/** Char used to mark a char array. */
41  	private static final char CHAR_ELEMENT = 'C';
42  	
43  	/** Char used to mark a double array. */
44  	private static final char DOUBLE_ELEMENT = 'D';
45  	
46  	/** Char used to mark a float array. */
47  	private static final char FLOAT_ELEMENT = 'F';
48  	
49  	/** Char used to mark a int array. */
50  	private static final char INT_ELEMENT = 'I';
51  	
52  	/** Char used to mark a long array. */
53  	private static final char LONG_ELEMENT = 'J';
54  	
55  	/** Char used to mark a short array. */
56  	private static final char SHORT_ELEMENT = 'S';
57  	
58  	/** Char used to mark a class array. */
59  	private static final char CLASS_INTERFACE_ELEMENT = 'L';
60  	
61  	/** Char used to specify the array's dimension. */
62  	private static final char ARRAY_DIM_SYMBOL = '[';
63  	
64  	/**
65  	 * Hides default constructor.
66  	 */
67  	private ClassUtils() { }
68  	
69  	/**
70  	 * Composes the canonical name of the given class. Arrays are transformed
71  	 * into the same string representation as it is needed to define them
72  	 * in Java (e.g. <code>java.lang.String[][]</code>).
73  	 *
74  	 * @param clazz
75  	 *      The class which canonical name has to be computed.
76  	 *
77  	 * @return Returns the canonical name of the given class.
78  	 */
79  	public static String getCanonicalClassName(Class<?> clazz) {
80  		String name = clazz.getName();
81  		
82  		int arrayDimension = name.lastIndexOf(ARRAY_DIM_SYMBOL) + 1;
83  		if (arrayDimension > 0) {
84  			char element = name.charAt(arrayDimension);
85  			switch (element) {
86  				case BOOLEAN_ELEMENT:
87  					name = boolean.class.getName(); break;
88  				case BYTE_ELEMENT:
89  					name = byte.class.getName(); break;
90  				case CHAR_ELEMENT:
91  					name = char.class.getName(); break;
92  				case DOUBLE_ELEMENT:
93  					name = double.class.getName(); break;
94  				case FLOAT_ELEMENT:
95  					name = float.class.getName(); break;
96  				case INT_ELEMENT:
97  					name = int.class.getName(); break;
98  				case LONG_ELEMENT:
99  					name = long.class.getName(); break;
100 				case SHORT_ELEMENT:
101 					name = short.class.getName(); break;
102 				case CLASS_INTERFACE_ELEMENT:
103 					name = name.substring(arrayDimension + 1,
104 							name.length() - 1);
105 					break;
106 				default:
107 					throw new IllegalArgumentException("Unknown type");
108 			}
109 			
110 			for (int i = 0; i < arrayDimension; i++) {
111 				name += "[]";
112 			}
113 		}
114 		
115 		return name;
116 	}
117 	
118 	/**
119 	 * Determines the class' package name.
120 	 *
121 	 * @param clazz
122 	 *      The class which package name has to be determined.
123 	 *
124 	 * @return Returns the given class' package name.
125 	 */
126 	public static String getPackageName(Class<?> clazz) {
127 		String pkgName;
128 		if (clazz.getPackage() != null
129 				&& StringUtils.hasText(clazz.getPackage().getName())) {
130 			pkgName = clazz.getPackage().getName();
131 		} else {
132 			String className = clazz.getName();
133 			pkgName = className.substring(0, className.lastIndexOf('.'));
134 		}
135 		return pkgName;
136 	}
137 	
138 	/**
139 	 * Transforms the given method into a string representation.
140 	 *
141 	 * @param method
142 	 *      The method to transform.
143 	 *
144 	 * @return Returns a string representation of the given method's signature.
145 	 */
146 	public static String getMethodSignature(Method method) {
147 		Class<?>[] types = method.getParameterTypes();
148 		
149 		StringBuffer buffer = new StringBuffer();
150 		buffer.append(method.getName());
151 		buffer.append("(");
152 		
153 		// add parameters types
154 		for (int i = 0; i < types.length; i++) {
155 			buffer.append(getCanonicalClassName(types[i]));
156 			
157 			if (i < types.length - 1) {
158 				buffer.append(", ");
159 			}
160 		}
161 		buffer.append(")");
162 		
163 		return buffer.toString();
164 	}
165 }