KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > jmx > MethodInfo


1 package org.sapia.soto.jmx;
2
3 import java.lang.reflect.Method JavaDoc;
4
5
6 /**
7  * Encapsulats information pertaining to a method.
8  *
9  * @author Yanick Duchesne
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class MethodInfo {
17   private String JavaDoc _name;
18   private String JavaDoc[] _params;
19   private int _hashCode;
20
21   /**
22    * Creates an instance of this class with the given method object.
23    *
24    * @param method a <code>Method</code> instance.
25    */

26   public MethodInfo(Method JavaDoc method) {
27     this(method.getName(), method.getParameterTypes());
28   }
29
30   /**
31    * Creates an instance of this class corresponding to the method with the
32    * given name, and the given signature - represented as an array of class objects.
33    *
34    * @param name a method name
35    * @param params an array of <code>Class</code> objects corresponding to this
36    * instance's corresponding method signature.
37    */

38   public MethodInfo(String JavaDoc name, Class JavaDoc[] params) {
39     _name = name;
40     _hashCode = name.hashCode();
41     _params = paramsAsString(params);
42   }
43
44   /**
45    * Creates an instance of this class corresponding to the method with the
46    * given name, and the given signature - represented as an array of class names.
47    *
48    * @param name a method name
49    * @param params an array of <code>String</code>s corresponding to this
50    * instance's corresponding method signature.
51    */

52   public MethodInfo(String JavaDoc name, String JavaDoc[] sig) {
53     _name = name;
54     _hashCode = name.hashCode();
55     _params = sig;
56   }
57
58   public int hashCode() {
59     return _hashCode;
60   }
61
62   public boolean equals(Object JavaDoc object) {
63     try {
64       MethodInfo info = (MethodInfo) object;
65
66       return _name.equals(info._name) &&
67       (_params.length == info._params.length) && paramsEqual(info._params);
68     } catch (ClassCastException JavaDoc e) {
69       return false;
70     }
71   }
72
73   static String JavaDoc[] paramsAsString(Class JavaDoc[] params) {
74     String JavaDoc[] sig = new String JavaDoc[params.length];
75
76     for (int i = 0; i < params.length; i++) {
77       sig[i] = params[i].getName();
78     }
79
80     return sig;
81   }
82
83   private boolean paramsEqual(String JavaDoc[] params) {
84     for (int i = 0; i < params.length; i++) {
85       if (!_params[i].equals(params[i])) {
86         return false;
87       }
88     }
89
90     return true;
91   }
92 }
93
Popular Tags