1 cananian 1.1.4.1 // HConstructor.java, created Sat Aug  1  4:54:58 1998 by cananian
 2 cananian 1.1.4.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
 3 cananian 1.1.4.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
 4 cananian 1.1.4.1 package harpoon.ClassFile;
 5 cananian 1.1.4.1 
 6 cananian 1.1.4.1 import harpoon.Util.ArrayFactory;
 7 cananian 1.1.4.1 
 8 cananian 1.1.4.1 import java.lang.reflect.Modifier;
 9 cananian 1.1.4.1 
10 cananian 1.1.4.1 /**
11 cananian 1.1.4.1  * An <code>HConstructorImpl</code> is a basic implementation of
12 cananian 1.1.4.1  * <code>HMethod</code>.
13 cananian 1.1.4.1  *
14 cananian 1.1.4.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
15 cananian 1.2      * @version $Id: HConstructorImpl.java,v 1.2 2002/02/25 21:03:03 cananian Exp $
16 cananian 1.1.4.1  * @see HConstructor
17 cananian 1.1.4.1  */
18 cananian 1.1.4.1 abstract class HConstructorImpl extends HMethodImpl implements HConstructor {
19 cananian 1.1.4.1   HConstructorImpl() { name="<init>"; returnType=HClass.Void; }
20 cananian 1.1.4.1   /**
21 cananian 1.1.4.1    * Returns the name of this constructor, as a string.  This is always
22 cananian 1.1.4.1    * the string "<code>&lt;init&gt;</code>".
23 cananian 1.1.4.1    */
24 cananian 1.1.4.1   public String getName() { return "<init>"/*hclass.getName()*/; }
25 cananian 1.1.4.1 
26 cananian 1.1.4.1   /**
27 cananian 1.1.4.1    * Returns a hashcode for this Constructor.  The hashcode is computed as
28 cananian 1.1.4.1    * the exclusive-or of the hashcodes for the underlying constructor's 
29 cananian 1.1.4.1    * declaring class and the constructor's descriptor string.
30 cananian 1.1.4.1    */
31 cananian 1.1.4.1   public int hashCode() { return hashCode(this); }
32 cananian 1.1.4.1   // separated out for re-use
33 cananian 1.1.4.1   static int hashCode(HConstructor hc) {
34 cananian 1.1.4.1     return hc.getDeclaringClass().hashCode() ^ hc.getDescriptor().hashCode(); 
35 cananian 1.1.4.1   }
36 cananian 1.1.4.1 
37 cananian 1.1.4.1   /**
38 cananian 1.1.4.1    * Return a string describing this Constructor.  The string is formatted
39 cananian 1.1.4.1    * as: the constructor access modifiers, if any, followed by the
40 cananian 1.1.4.1    * fully-qualified name of the declaring class, followed by a 
41 cananian 1.1.4.1    * parenthesized, comma-separated list of the constructor's formal
42 cananian 1.1.4.1    * parameter types.  For example: <p>
43 cananian 1.1.4.1    * <DL><DD><CODE>public java.util.Hashtable(int,float)</CODE></DL><p>
44 cananian 1.1.4.1    * The only possible modifiers for constructors are the access modifiers
45 cananian 1.1.4.1    * <code>public</code>, <code>protected</code>, or <code>private</code>.
46 cananian 1.1.4.1    * Only one of these may appear, or none if the constructor has default
47 cananian 1.1.4.1    * (<code>package</code>) access.
48 cananian 1.1.4.1    */
49 cananian 1.1.4.1   public String toString() { return toString(this); }
50 cananian 1.1.4.1   // separated out for re-use.
51 cananian 1.1.4.1   static String toString(HConstructor hc) {
52 cananian 1.1.4.1     StringBuffer r = new StringBuffer();
53 cananian 1.1.4.1     int m = hc.getModifiers();
54 cananian 1.1.4.1     if (m!=0) {
55 cananian 1.1.4.1       r.append(Modifier.toString(m));
56 cananian 1.1.4.1       r.append(' ');
57 cananian 1.1.4.1     }
58 cananian 1.1.4.1     r.append(HClass.getTypeName(hc.getDeclaringClass()));
59 cananian 1.1.4.1     r.append('(');
60 cananian 1.1.4.1     HClass hcp[] = hc.getParameterTypes();
61 cananian 1.1.4.1     for (int i=0; i<hcp.length; i++) {
62 cananian 1.1.4.1       r.append(HClass.getTypeName(hcp[i]));
63 cananian 1.1.4.1       if (i<hcp.length-1)
64 cananian 1.1.4.1         r.append(',');
65 cananian 1.1.4.1     }
66 cananian 1.1.4.1     r.append(')');
67 cananian 1.1.4.1     HClass ecp[] = hc.getExceptionTypes();
68 cananian 1.1.4.1     if (ecp.length > 0) {
69 cananian 1.1.4.1       r.append(" throws ");
70 cananian 1.1.4.1       for (int i=0; i<ecp.length; i++) {
71 cananian 1.1.4.1         r.append(ecp[i].getName()); // can't be primitive or array type.
72 cananian 1.1.4.1         if (i<ecp.length-1)
73 cananian 1.1.4.1           r.append(',');
74 cananian 1.1.4.1       }
75 cananian 1.1.4.1     }
76 cananian 1.1.4.1     return r.toString();
77 cananian 1.1.4.1   }
78 cananian 1.2     }