3 import java.beans.Introspector;
4 import java.lang.reflect.Method;
7 * Defines a default parameter name factory for the InterfaceFactory.
8 * Tries hard to find a meaningfull name.
10 * Copyright (c) Eric D. Friedman 1998. All Rights Reserved.
11 * Copyright (c) Paul Kinnucan 1998. All Rights Reserved.
13 * $Date: 2002/12/12 05:15:48 $
15 * InterfaceFactory is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2, or (at
18 * your option) any later version.
20 * InterfaceFactory is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * To obtain a copy of the GNU General Public License write to the
26 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
29 * @author Eric D. Friedman
30 * @author Paul Kinnucan
32 * @version $Revision: 1.6 $
35 public class DefaultNameFactory implements NameFactory {
38 * Make up a name for the nth parameter of a method.
39 * First this method tries to recognize bean methods, if the method is named
40 * "setName" and has one parameter, the parameter returned is the
41 * lowercased method name with "set" stripped: "name".
43 * Index Bean Properties are methods starting with "set" and having two
44 * parameters, the first one of which is an int, or methods starting with
45 * "get" and having only one int parameter. In both cases the first
46 * parameter is given the name "index".
50 * void setName(String name);
51 * void setNames(String[] names);
52 * void setName(int index, String name);
53 * String getName(int index);
55 * For other method names the parameters are named according to their
58 * Primitive types are named after the following mapping (now
59 * hardcoded, can perhaps be made configurable):
70 * Array types are named like their base type with the String "Array"
71 * appended, so a "byte[]" parameter will be named "byteArray":
73 * void fill(char[] charArray);
75 * All other parameters will be named according to their type's unqualified
76 * lowercased classname:
78 * void actionPerformed(ActionEvent actionEvent);
80 * To avoid any name clashes, the parameters will have a number appended
81 * if there is another parameter of the same type in the methods
84 * void write(byte[] byteArray, int n, int n1);
85 * void put(String string, String string1);
87 * @param sig - signature of the declaring method
88 * @param num - the parameter number whose name we want.
89 * @return a name for the n'th parameter of the method.
91 public String getParameterName(Signature sig, int num) {
92 Method method = sig.getMethod();
93 String methodName = method.getName();
94 Class[] parameters = method.getParameterTypes();
95 Class type = parameters[num];
97 // To handle inner-class .class files
98 String className = type.getName().replace('$', '.');
100 // special handling for bean methods
101 if (methodName.length() > 3) {
102 if (methodName.startsWith("set")) {
103 if (parameters.length == 2) {
104 // an indexed propery setter
105 if (num == 1 && className.equals("int")) {
110 return methodToParamName(methodName, 3);
114 // a normal propery setter
115 if (parameters.length == 1 && num == 1) {
116 return methodToParamName(methodName, 3);
120 if (methodName.startsWith("get")) {
122 if (parameters.length == 1 && num == 1 && className.equals("int"))
127 return unique(parameters, type, num, getNameFromClass(className));
132 * Generate a name for a parameter from the type of the parameter.
134 * @param className the parameters type
135 * @return a more or less fitting name.
137 private final String getNameFromClass(String className) {
141 // if this is an classname starting with an upper case letter
142 // downcase it and use class name as parameter name
143 int i = className.lastIndexOf('.')+1;
144 if (Character.isUpperCase(className.charAt(i))) {
145 className = Introspector.decapitalize(className.substring(i));
147 // Handle user-defined type array names of the form
149 if (className.endsWith(";")) {
150 className = className.substring(0, className.length()-1) + "Array";
156 // handle primitive arrays
157 if (className.equals("[Z")) {
161 if (className.equals("[B")) {
165 if (className.equals("[C"))
167 if (className.equals("[S"))
169 if (className.equals("[I"))
171 if (className.equals("[J"))
173 if (className.equals("[F"))
175 if (className.equals("[D"))
176 return "doubleArray";
179 if (className.equals("boolean"))
181 if (className.equals("byte"))
183 if (className.equals("char"))
185 if (className.equals("short"))
187 if (className.equals("int"))
189 if (className.equals("long"))
191 if (className.equals("float"))
193 if (className.equals("double"))
201 * Make name unique, look if there a more parameters of this type
202 * before us. In this case append a number.
204 * @param parameters all parameter types of the method
205 * @param type the type of the current parameter
206 * @param num the position of the current parameter
207 * @param name parameter basename
208 * @return a unique parameter name
210 private final String unique(Class[] parameters, Class type, int num, String name) {
211 if (parameters.length > 1) {
213 for (int j = 0; j < num; ++j)
214 if (parameters[j] == type)
225 * Strip the first characters from the string and decapitalize
228 * @param name the original name.
229 * @param strip number of characters to strip from the front of the name.
230 * @return the decapitalized and shortened name.
232 private final String methodToParamName(String name, int strip) {
233 return Introspector.decapitalize(name.substring(strip));
238 * $Log: DefaultNameFactory.java,v $
239 * Revision 1.6 2002/12/12 05:15:48 paulk
240 * Now correctly generates names for method parameters that are arrays of user-defined types.
242 * Revision 1.5 2002/12/04 07:06:47 paulk
243 * Updated to handle implementation of interfaces that reference inner classes.
245 * Revision 1.4 2002/06/06 05:12:44 paulk
246 * DefaultNameFactory now generates meaningful method parameter names based
247 * on the parameter type or the method name. Thanks to Ole Arndt.
251 // End of DefaultNameFactory.java