KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > methodset > MethodInfo


1 package csdl.jblanket.methodset;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 /**
7  * Implements the MethodInfo abstract data type, which provides information
8  * about a single Java method. Method instances are managed by the
9  * MethodSet container.
10  *
11  * @author Philip Johnson
12  * @version $Id: MethodInfo.java,v 1.1 2004/11/07 00:32:38 timshadel Exp $
13  */

14 public class MethodInfo {
15
16   /** The fully qualified class name. */
17   private String JavaDoc className;
18   /** The method name (no parentheses). */
19   private String JavaDoc methodName;
20   /** A list of fully qualified class names, in order. */
21   private List JavaDoc parameterTypeList;
22   /** The string representation of this method, used for ordering. */
23   private String JavaDoc methodString;
24   /** The hashcode for this method. */
25   private int hashCode;
26
27   /**
28    * Creates a Method representing information about a Method's type signature.
29    *
30    * @param className The fully qualified class name, such as "java.lang.String".
31    * @param methodName The method name without parentheses, such as "getBaz".
32    * @param parameterTypeList A list, in order, of fully qualified class names
33    * corresponding to the parameters that this method is invoked with.
34    */

35   public MethodInfo(String JavaDoc className, String JavaDoc methodName, List JavaDoc parameterTypeList) {
36     // Make sure we don't get passed any nulls, because that will screw us later.
37
if ((className == null) || (methodName == null) || (parameterTypeList == null)) {
38       throw new RuntimeException JavaDoc("Args to MethodInfo constructor cannot be null!");
39     }
40     this.className = className;
41     this.methodName = methodName;
42     // Note, we do not copy the passed list, so hopefully it isn't modified later
43
// by the client!
44
this.parameterTypeList = parameterTypeList;
45     this.methodString = this.className + "." + this.methodName + " " + this.parameterTypeList;
46     this.hashCode = this.methodString.hashCode();
47   }
48
49
50   /**
51    * Returns the fully qualified class name.
52    *
53    * @return The fully qualified class name.
54    */

55   public String JavaDoc getClassName() {
56     return this.className;
57   }
58
59   /**
60    * Returns a string representation of the methodInfo, used only for comparison
61    * purposes in the MethodSet container.
62    *
63    * @return The fully qualified class name.
64    */

65   String JavaDoc getMethodString() {
66     return this.methodString;
67   }
68
69
70   /**
71    * Returns the method name.
72    *
73    * @return The method name.
74    */

75   public String JavaDoc getMethodName() {
76     return this.methodName;
77   }
78
79
80   /**
81    * Returns the list of parameter types.
82    * Note that this list is not copied, so changes made to this list instance
83    * by the client will modify this method's internal representation!
84    *
85    * @return The parameter type list.
86    */

87   public List JavaDoc getParameterTypeList() {
88     return this.parameterTypeList;
89   }
90
91
92   /**
93    * Returns the package corresponding to this method. For example, if the method's
94    * class name is "java.lang.String", then the package is "java.lang". If the
95    * method is not in a package, then the empty string is returned.
96    *
97    * @return The package string.
98    */

99   public String JavaDoc getPackageName() {
100     int endPackageIndex = this.className.lastIndexOf('.');
101     return (endPackageIndex >= 0) ? this.className.substring((endPackageIndex + 1)) : "";
102   }
103
104
105   /**
106    * Provides a readable representation of the Method.
107    *
108    * @return The MethodSet as a string.
109    */

110   public String JavaDoc toString() {
111     return "[Method " + methodString + "]";
112   }
113
114   /**
115    * Implements the "natural ordering" for methods.
116    * Methods are ordered by their string representation.
117    *
118    * @param obj A method instance.
119    * @return Standard compareTo return values.
120    */

121   public int compareTo(Object JavaDoc obj) {
122     return this.methodString.compareTo(((MethodInfo) obj).methodString);
123   }
124
125   /**
126    * Two Methods are equal() iff their string representations are equal.
127    *
128    * @param obj An object, which could or could not be a method.
129    * @return True if equal.
130    */

131   public boolean equals(Object JavaDoc obj) {
132     return ((obj instanceof MethodInfo) &&
133         (this.methodString.equals(((MethodInfo) obj).methodString)));
134   }
135
136
137   /**
138    * Compute the hashcode following recommendations in "Effective Java".
139    *
140    * @return The hashcode.
141    */

142   public int hashCode() {
143     return this.hashCode;
144   }
145
146
147   /**
148    * Main for one-off testing of this data structure.
149    *
150    * @param args Ignored.
151    */

152   public static void main(String JavaDoc args[]) {
153     ArrayList JavaDoc params = new ArrayList JavaDoc();
154     params.add("java.lang.String");
155     params.add("java.lang.Boolean");
156     MethodInfo method1 = new MethodInfo("foo.bar.Baz", "qux", params);
157     System.out.println(method1);
158     System.out.println("Package: " + method1.getPackageName());
159     MethodInfo method2 = new MethodInfo("Baz", "qux", null);
160     System.out.println("Package: " + method2.getPackageName());
161     System.out.println("Test equal methods: " + method1.equals(method1));
162     System.out.println("Test non-equal methods: " + method1.equals(method2));
163   }
164 }
165
Popular Tags