2 * Copyright (c) Eric D. Friedman 1998. All Rights Reserved.
3 * Copyright (c) Paul Kinnucan 1998. All Rights Reserved.
6 * $Date: 2002/12/04 07:06:46 $
8 * InterfaceFactory is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2, or (at
11 * your option) any later version.
13 * InterfaceFactory is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * To obtain a copy of the GNU General Public License write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 import java.lang.reflect.Method;
26 import java.lang.reflect.Modifier;
29 * Defines a method signature.
31 * @author Eric D. Friedman
32 * @version $Revision: 1.11 $
35 public class Signature {
38 * The method represented by this signature
40 private Method method;
43 * the parameters in this signature
45 private Class[] parameters;
48 * the exceptions thrown by this signature
50 private Class[] exceptions;
55 private Class returnValue;
58 * The Class that declared this signature
60 private Class declaring;
63 * the InterfaceFactory processing this signature
65 private ClassRegistry registry;
68 * Toggles truncation of package information in signatures
70 private boolean truncate;
73 * Toggles generation of see
78 * Creates a signature for the specified method and register
79 * its types with the specified registry (which may be null).
80 * Removes package info from generated signatures.
82 * @param meth Method represented by this signature
83 * @param aregistry Registry containing this signature
85 public Signature (Method meth, ClassRegistry aregistry) {
86 this(meth, aregistry, true);
90 * Creates a signature for the specified method and register its
91 * types with the specified registry (which may be null).
93 * @param meth Method represented by thi signature
94 * @param aregistry Registry containing this signature
95 * @param truncate toggles removal of package info from generated signatures
97 public Signature (Method meth, ClassRegistry aregistry, boolean truncate) {
98 this(meth, aregistry, truncate, false);
102 * Creates a signature for the specified method and register its
103 * types with the specified registry (which may be null).
105 * @param meth Method represented by thi signature
106 * @param aregistry Registry containing this signature
107 * @param truncate toggles removal of package info from generated signatures
108 * @param see Class to link the see section to or null, if no see
109 * section should be created
111 public Signature (Method meth, ClassRegistry aregistry, boolean truncate,
114 this.registry = aregistry;
115 this.truncate = truncate;
118 parameters = method.getParameterTypes();
119 exceptions = method.getExceptionTypes();
120 returnValue = method.getReturnType();
121 declaring = method.getDeclaringClass();
127 * Toggles truncation of package info .
129 * @param b Truncation toggle
131 public void setTruncating(boolean b) {
136 * Sets the registry used to register this signature's types.
138 * @param registry Registry used to register this signature's type.
140 public void setRegistry(ClassRegistry registry) {
141 this.registry = registry;
145 * Gets the class that declared the method represented by this signature.
147 * @return The class this method was declared in
149 public Class getDeclaringClass () {
154 * Generates a javadoc string for this signature.
156 * @return A javadoc for this method
158 public String toJavaDoc() {
159 NameFactory factory = registry.getNameFactory();
160 StringBuffer buf = new StringBuffer("/**\n *\n *");
162 for (int i = 0; i < parameters.length; i++) {
163 buf.append(" @param "
164 + factory.getParameterName(this, i)
165 + " <description>\n *");
168 if (!"void".equals(returnValue.getName())) {
169 buf.append(" @return <description>" + "\n *");
172 for (int i = 0; i < exceptions.length; i++) {
173 buf.append(" @exception " + exceptions[i].getName()
174 + " <description>\n *");
178 buf.append(" @see ");
179 buf.append(method.getDeclaringClass().getName());
181 buf.append(method.getName());
183 buf.append(getParameters(method.getParameterTypes(), false));
190 return buf.toString();
194 * Gets the signature as a string.
196 * @return Signature of this method.
198 public String toString() {
199 String m = getModifiers();
200 String r = baseName(returnValue);
201 String meth = method.getName();
203 String p = getParameters(parameters);
204 String e = getExceptions(exceptions);
206 return m + " " + r + " " + meth + "(" + p + ")" + e;
210 * Describe <code>paramsEqual</code> method here.
212 * @param p a <code>Class[]</code> value
213 * @return a <code>boolean</code> value
215 public boolean paramsEqual(Class[] p) {
216 int n = parameters.length;
217 boolean res = (p.length == n);
219 for (int i = 0; i < n; ++i) {
220 if (!p[i].equals(parameters[i])) {
230 * Tests whether a given object equals this signature.
231 * The object is considered equal if it is a signature
232 * and it has the same method name and parameter list.
234 * @param compare Test object
235 * @return <code>true</code> if the test object equals this signature.
237 public boolean equals(Object compare) {
238 if (compare instanceof Signature) {
239 Signature sig = (Signature) compare;
240 return method.getName().equals(sig.getMethod().getName())
241 && paramsEqual(sig.getMethod().getParameterTypes());
247 * Gets the method of which this is a signature.
249 * @return Method of which this is a signature.
251 public Method getMethod() {
256 * Computes the basename of the specified class. This returns
257 * "Object" from "java.lang.Object." It returns the "single"
258 * form of an Array object.
260 * In the case of inner classes, the base name includes
261 * the outer class name, e.g.,
263 * mypackage.OuterClass$InnerClass
267 * OuterClass.InnerClass
269 * @param type Class whose basename is required
273 public final String baseName(Class type) {
275 if (type.isArray()) {
281 while (cl.isArray()) {
282 cl = cl.getComponentType();
286 StringBuffer sb = new StringBuffer();
287 sb.append(cl.getName());
289 for (int i = 0; i < dimensions; i++) {
293 name = sb.toString();
294 } catch (Throwable e) {
295 name = type.getName();
298 name = type.getName();
302 // If truncate flag is true, remove
303 // the package name from the class
308 // becomes simply Object.
310 int idx = name.lastIndexOf('.');
313 name = name.substring(idx + 1);
317 // If type is an inner class, its base
318 // name includes the outer class name,
319 // with a $ separating the inner from
320 // the outer name, e.g.,
322 // OuterClass$InnerClass
324 // Replace the $ with a . (period).
325 name = name.replace('$', '.');
331 * Make a list of this method's exceptions separated by commata.
333 * @return Exceptions thrown by this method.
335 public final String getExceptionList() {
336 return getExceptionList(exceptions);
342 * Make a list of all given exceptions separated by commas.
344 * @param except Array of exception types
345 * @return Comma-separated list of exceptions
347 public final String getExceptionList(Class[] except) {
349 if ((null == except) || (except.length == 0)) {
353 StringBuffer buf = new StringBuffer();
355 for (int i = 0; i < except.length; i++) {
356 String type = baseName(except[i]);
359 if (i < except.length - 1) {
365 return buf.toString();
369 * Gets a throw clause listing the exceptions thrown by this method.
371 * @param except Vector of exceptions
373 * @return Exceptions thrown by this method.
375 private final String getExceptions(Class[] except) {
376 String res = getExceptionList (except);
378 if (res.length() == 0) {
382 return " throws " + res;
386 * Gets a parameter list for this method; parameters are named
387 * by the NameFactory whose default implementation uses param1
390 * @return Parameter list in string form
392 public final String getParameters () {
393 return getParameters(parameters, true);
397 * Gets a parameter list for this method; parameters are named
398 * by the NameFactory whose default implementation uses param1
401 * @param params Parameters of this method
403 * @return Parameter list in string form
405 public final String getParameters (Class[] params) {
406 return getParameters(params, true);
410 * Gets a parameter list for this method; parameters are named
411 * by the NameFactory whose default implementation uses param1
414 * @param params Parameters of this method
415 * @param withName toggles parameter names
417 * @return Parameter list in string form
419 public final String getParameters (Class[] params, boolean withName) {
421 if ((null == params) || (params.length == 0)) {
425 StringBuffer buf = new StringBuffer();
426 NameFactory factory = registry.getNameFactory();
428 for (int i = 0; i < params.length; i++) {
429 String type = baseName(params[i]);
430 String name = factory.getParameterName(this, i);
438 if (i < params.length - 1) {
444 return buf.toString();
448 * Get the names of all parameters of this signature,
451 * @return te parameter names seperated by comma.
453 public final String getParameterNames () {
454 return getParameterNames(method.getParameterTypes());
458 * Gets a list of parameter names for this method; parameters are
459 * named by the NameFactory whose default implementation uses param1
460 * .. paramn. Class type names are not included in the result
461 * Contributed by Charles Hart <cfhart@Z-TEL.com>
463 * @param params Parameters of this method
465 * @return Parameter list in string form */
466 public final String getParameterNames (Class[] params) {
468 if ((null == params) || (params.length == 0)) {
472 StringBuffer buf = new StringBuffer();
473 NameFactory factory = registry.getNameFactory();
475 for (int i = 0; i < params.length; i++) {
476 String type = baseName(params[i]);
477 String name = factory.getParameterName(this, i);
481 if (i < params.length - 1) {
487 return buf.toString();
491 * Gets list of modifiers for this method.
492 * Interface methods are always public and may be synchronized
494 * @return a <code>String</code> containing a list of modifiers
496 public final String getModifiers () {
497 StringBuffer buf = new StringBuffer("public");
498 int mod = method.getModifiers();
500 if (Modifier.isSynchronized(mod)) {
501 buf.append(" synchronized");
504 return buf.toString();
508 * Gets the base type of the return value.
510 * @return Base type of return value.
512 public String getReturnBaseType () {
513 return baseName(returnValue);
517 * Register this Signature's types with the SignatureRegistry
519 private final void register() {
520 if (null != registry) {
521 registry.registerImport(declaring);
522 registry.registerImport(returnValue);
524 for (int i = 0; i < parameters.length; i++) {
525 registry.registerImport(parameters[i]);
528 for (int i = 0; i < exceptions.length; i++) {
529 registry.registerImport(exceptions[i]);
534 } // end of Signature class
537 * $Log: Signature.java,v $
538 * Revision 1.11 2002/12/04 07:06:46 paulk
539 * Updated to handle implementation of interfaces that reference inner classes.
541 * Revision 1.10 2002/06/06 05:12:44 paulk
542 * DefaultNameFactory now generates meaningful method parameter names based
543 * on the parameter type or the method name. Thanks to Ole Arndt.
545 * Revision 1.9 2002/05/14 06:38:43 paulk
546 * Enhances code generation wizards for implementing interfaces, abstract
547 * classes, etc., to use customizable templates to generate skeleton methods
548 * instead of hard-wired skeletons. Thanks to "Dr. Michael Lipp" <lipp@danet.de>
549 * for proposing and implementing this improvement.
551 * Revision 1.8 2001/09/16 16:24:13 paulk
552 * Corrected bug in the constructor Signature (Method meth, ClassRegistry aregistry,
554 * Thanks to petter.mahlen@chello.se.
556 * Revision 1.7 2000/08/03 04:28:41 paulk
557 * Add support for generating a see secton in the Javadoc comment for a method.
558 * Thanks to raffael.herzog@comartis.com
560 * Revision 1.6 2000/07/14 05:26:56 paulk
561 * Adds support for delegation wizard.
565 // End of Signature.java