001: package csdl.jblanket.methodset;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: /**
007: * Implements the MethodInfo abstract data type, which provides information
008: * about a single Java method. Method instances are managed by the
009: * MethodSet container.
010: *
011: * @author Philip Johnson
012: * @version $Id: MethodInfo.java,v 1.1 2004/11/07 00:32:38 timshadel Exp $
013: */
014: public class MethodInfo {
015:
016: /** The fully qualified class name. */
017: private String className;
018: /** The method name (no parentheses). */
019: private String methodName;
020: /** A list of fully qualified class names, in order. */
021: private List parameterTypeList;
022: /** The string representation of this method, used for ordering. */
023: private String methodString;
024: /** The hashcode for this method. */
025: private int hashCode;
026:
027: /**
028: * Creates a Method representing information about a Method's type signature.
029: *
030: * @param className The fully qualified class name, such as "java.lang.String".
031: * @param methodName The method name without parentheses, such as "getBaz".
032: * @param parameterTypeList A list, in order, of fully qualified class names
033: * corresponding to the parameters that this method is invoked with.
034: */
035: public MethodInfo(String className, String methodName,
036: List parameterTypeList) {
037: // Make sure we don't get passed any nulls, because that will screw us later.
038: if ((className == null) || (methodName == null)
039: || (parameterTypeList == null)) {
040: throw new RuntimeException(
041: "Args to MethodInfo constructor cannot be null!");
042: }
043: this .className = className;
044: this .methodName = methodName;
045: // Note, we do not copy the passed list, so hopefully it isn't modified later
046: // by the client!
047: this .parameterTypeList = parameterTypeList;
048: this .methodString = this .className + "." + this .methodName
049: + " " + this .parameterTypeList;
050: this .hashCode = this .methodString.hashCode();
051: }
052:
053: /**
054: * Returns the fully qualified class name.
055: *
056: * @return The fully qualified class name.
057: */
058: public String getClassName() {
059: return this .className;
060: }
061:
062: /**
063: * Returns a string representation of the methodInfo, used only for comparison
064: * purposes in the MethodSet container.
065: *
066: * @return The fully qualified class name.
067: */
068: String getMethodString() {
069: return this .methodString;
070: }
071:
072: /**
073: * Returns the method name.
074: *
075: * @return The method name.
076: */
077: public String getMethodName() {
078: return this .methodName;
079: }
080:
081: /**
082: * Returns the list of parameter types.
083: * Note that this list is not copied, so changes made to this list instance
084: * by the client will modify this method's internal representation!
085: *
086: * @return The parameter type list.
087: */
088: public List getParameterTypeList() {
089: return this .parameterTypeList;
090: }
091:
092: /**
093: * Returns the package corresponding to this method. For example, if the method's
094: * class name is "java.lang.String", then the package is "java.lang". If the
095: * method is not in a package, then the empty string is returned.
096: *
097: * @return The package string.
098: */
099: public String getPackageName() {
100: int endPackageIndex = this .className.lastIndexOf('.');
101: return (endPackageIndex >= 0) ? this .className
102: .substring((endPackageIndex + 1)) : "";
103: }
104:
105: /**
106: * Provides a readable representation of the Method.
107: *
108: * @return The MethodSet as a string.
109: */
110: public String 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 obj) {
122: return this .methodString
123: .compareTo(((MethodInfo) obj).methodString);
124: }
125:
126: /**
127: * Two Methods are equal() iff their string representations are equal.
128: *
129: * @param obj An object, which could or could not be a method.
130: * @return True if equal.
131: */
132: public boolean equals(Object obj) {
133: return ((obj instanceof MethodInfo) && (this .methodString
134: .equals(((MethodInfo) obj).methodString)));
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: * Main for one-off testing of this data structure.
148: *
149: * @param args Ignored.
150: */
151: public static void main(String args[]) {
152: ArrayList params = new ArrayList();
153: params.add("java.lang.String");
154: params.add("java.lang.Boolean");
155: MethodInfo method1 = new MethodInfo("foo.bar.Baz", "qux",
156: 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: "
162: + method1.equals(method1));
163: System.out.println("Test non-equal methods: "
164: + method1.equals(method2));
165: }
166: }
|