1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.Serializable;
18  
19  import java.lang.reflect.Method;
20  
21  /**
22   * <a href="MethodKey.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public class MethodKey implements Serializable {
27  
28      /**
29       * @deprecated
30       */
31      public static MethodKey create(
32              String className, String methodName, String[] parameterTypeNames)
33          throws ClassNotFoundException {
34  
35          return new MethodKey(className, methodName, parameterTypeNames);
36      }
37  
38      public MethodKey(Method method) {
39          this(
40              method.getDeclaringClass().getName(), method.getName(),
41              method.getParameterTypes());
42      }
43  
44      public MethodKey(
45          String className, String methodName, Class<?>... parameterTypes) {
46  
47          _className = className;
48          _methodName = methodName;
49          _parameterTypes = parameterTypes;
50      }
51  
52      public MethodKey(
53              String className, String methodName, String[] parameterTypeNames)
54          throws ClassNotFoundException {
55  
56          _className = className;
57          _methodName = methodName;
58  
59          _parameterTypes = new Class[parameterTypeNames.length];
60  
61          ClassLoader classLoader = null;
62  
63          if (parameterTypeNames.length > 0) {
64              Thread currentThread = Thread.currentThread();
65  
66              classLoader = currentThread.getContextClassLoader();
67          }
68  
69          for (int i = 0; i < parameterTypeNames.length; i++) {
70              String parameterTypeName = parameterTypeNames[i];
71  
72              _parameterTypes[i] = Class.forName(
73                  parameterTypeName, true, classLoader);
74          }
75      }
76  
77      public boolean equals(Object obj) {
78          if (obj == null) {
79              return false;
80          }
81  
82          MethodKey methodKey = (MethodKey)obj;
83  
84          if (toString().equals(methodKey.toString())) {
85              return true;
86          }
87          else {
88              return false;
89          }
90      }
91  
92      public String getClassName() {
93          return _className;
94      }
95  
96      public String getMethodName() {
97          return _methodName;
98      }
99  
100     public Class<?>[] getParameterTypes() {
101         return _parameterTypes;
102     }
103 
104     /**
105      * @deprecated Use <code>getParameterTypes</code>.
106      */
107     public Class<?>[] getTypes() {
108         return getParameterTypes();
109     }
110 
111     public int hashCode() {
112         return toString().hashCode();
113     }
114 
115     public String toString() {
116         return _toString();
117     }
118 
119     private String _toString() {
120         if (_toString == null) {
121             StringBundler sb = new StringBundler();
122 
123             sb.append(_className);
124             sb.append(_methodName);
125 
126             if ((_parameterTypes != null) && (_parameterTypes.length > 0)) {
127                 sb.append(StringPool.DASH);
128 
129                 for (Class<?> parameterType : _parameterTypes) {
130                     sb.append(parameterType.getName());
131                 }
132             }
133 
134             _toString = sb.toString();
135         }
136 
137         return _toString;
138     }
139 
140     private String _className;
141     private String _methodName;
142     private Class<?>[] _parameterTypes;
143     private String _toString;
144 
145 }