001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.lang.reflect.Method;
018    
019    /**
020     * @author Shuyang Zhou
021     * @author Brian Wing Shun Chan
022     */
023    public class MethodTargetClassKey {
024    
025            public MethodTargetClassKey(Method method, Class<?> targetClass) {
026                    _method = method;
027                    _targetClass = targetClass;
028            }
029    
030            public boolean equals(Object obj) {
031                    if (this == obj) {
032                            return true;
033                    }
034    
035                    if (!(obj instanceof MethodTargetClassKey)) {
036                            return false;
037                    }
038    
039                    MethodTargetClassKey methodTargetClassKey = (MethodTargetClassKey)obj;
040    
041                    if (_targetClass == methodTargetClassKey._targetClass &&
042                            Validator.equals(_method, methodTargetClassKey._method)) {
043    
044                            return true;
045                    }
046    
047                    return false;
048            }
049    
050            public Method getMethod() {
051                    return _method;
052            }
053    
054            public Class<?> getTargetClass() {
055                    return _targetClass;
056            }
057    
058            public Method getTargetMethod() {
059                    if (_targetMethod == null && _targetClass != null) {
060                            try {
061                                    _targetMethod = _targetClass.getDeclaredMethod(
062                                            _method.getName(), _method.getParameterTypes());
063                            }
064                            catch (Throwable t) {
065                            }
066                    }
067    
068                    return _targetMethod;
069            }
070    
071            public int hashCode() {
072                    if (_hashCode == 0) {
073                            int hashCode = 77;
074    
075                            if (_method != null) {
076                                    hashCode += _method.hashCode();
077                            }
078    
079                            hashCode = 11 * hashCode;
080    
081                            if (_targetClass != null) {
082                                    hashCode += _targetClass.hashCode();
083                            }
084    
085                            _hashCode = hashCode;
086                    }
087    
088                    return _hashCode;
089            }
090    
091            public String toString() {
092                    if (_toString == null) {
093                            Class<?>[] parameterTypes = _method.getParameterTypes();
094    
095                            StringBundler sb = new StringBundler(parameterTypes.length * 2 + 6);
096    
097                            sb.append(_method.getDeclaringClass().getName());
098                            sb.append(StringPool.PERIOD);
099                            sb.append(_method.getName());
100                            sb.append(StringPool.OPEN_PARENTHESIS);
101    
102                            for (int i = 0; i < parameterTypes.length; i++) {
103                                    sb.append(parameterTypes[i].getName());
104    
105                                    if ((i + 1) < parameterTypes.length) {
106                                            sb.append(StringPool.COMMA);
107                                    }
108                            }
109    
110                            sb.append(StringPool.CLOSE_PARENTHESIS);
111    
112                            if (_targetClass != null) {
113                                    sb.append(StringPool.AT);
114                                    sb.append(_targetClass.getName());
115                            }
116    
117                            _toString = sb.toString();
118                    }
119    
120                    return _toString;
121            }
122    
123            private int _hashCode;
124            private Method _method;
125            private Class<?> _targetClass;
126            private Method _targetMethod;
127            private String _toString;
128    
129    }