1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.lang.reflect.Method;
18  
19  /**
20   * <a href="MethodTargetClassKey.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author Shuyang Zhou
23   * @author Brian Wing Shun Chan
24   */
25  public class MethodTargetClassKey {
26  
27      public MethodTargetClassKey(Method method, Class<?> targetClass) {
28          _method = method;
29          _targetClass = targetClass;
30  
31          if (_targetClass != null) {
32              try {
33                  _targetMethod = _targetClass.getDeclaredMethod(
34                      _method.getName(), _method.getParameterTypes());
35              }
36              catch (Throwable t) {
37              }
38          }
39      }
40  
41      public boolean equals(Object obj) {
42          if (this == obj) {
43              return true;
44          }
45  
46          if (!(obj instanceof MethodTargetClassKey)) {
47              return false;
48          }
49  
50          MethodTargetClassKey methodTargetClassKey = (MethodTargetClassKey)obj;
51  
52          if (Validator.equals(_method, methodTargetClassKey._method) &&
53              Validator.equals(_targetClass, methodTargetClassKey._targetClass)) {
54  
55              return true;
56          }
57  
58          return false;
59      }
60  
61      public Method getMethod() {
62          return _method;
63      }
64  
65      public Class<?> getTargetClass() {
66          return _targetClass;
67      }
68  
69      public Method getTargetMethod() {
70          return _targetMethod;
71      }
72  
73      public int hashCode() {
74          if (_hashCode == 0) {
75              int hashCode = 77;
76  
77              if (_method != null) {
78                  hashCode += _method.hashCode();
79              }
80  
81              hashCode = 11 * hashCode;
82  
83              if (_targetClass != null) {
84                  hashCode += _targetClass.hashCode();
85              }
86  
87              _hashCode = hashCode;
88          }
89  
90          return _hashCode;
91      }
92  
93      public String toString() {
94          if (_toString == null) {
95              Class<?>[] parameterTypes = _method.getParameterTypes();
96  
97              StringBundler sb = new StringBundler(parameterTypes.length * 2 + 6);
98  
99              sb.append(_method.getDeclaringClass().getName());
100             sb.append(StringPool.PERIOD);
101             sb.append(_method.getName());
102             sb.append(StringPool.OPEN_PARENTHESIS);
103 
104             for (int i = 0; i < parameterTypes.length; i++) {
105                 sb.append(parameterTypes[i].getName());
106 
107                 if ((i + 1) < parameterTypes.length) {
108                     sb.append(StringPool.COMMA);
109                 }
110             }
111 
112             sb.append(StringPool.CLOSE_PARENTHESIS);
113 
114             if (_targetClass != null) {
115                 sb.append(StringPool.AT);
116                 sb.append(_targetClass.getName());
117             }
118 
119             _toString = sb.toString();
120         }
121 
122         return _toString;
123     }
124 
125     private int _hashCode;
126     private Method _method;
127     private Class<?> _targetClass;
128     private Method _targetMethod;
129     private String _toString;
130 
131 }