001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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                    if (_targetClass != null) {
030                            try {
031                                    _targetMethod = _targetClass.getDeclaredMethod(
032                                            _method.getName(), _method.getParameterTypes());
033                            }
034                            catch (Throwable t) {
035                            }
036                    }
037            }
038    
039            public boolean equals(Object obj) {
040                    if (this == obj) {
041                            return true;
042                    }
043    
044                    if (!(obj instanceof MethodTargetClassKey)) {
045                            return false;
046                    }
047    
048                    MethodTargetClassKey methodTargetClassKey = (MethodTargetClassKey)obj;
049    
050                    if (Validator.equals(_method, methodTargetClassKey._method) &&
051                            Validator.equals(_targetClass, methodTargetClassKey._targetClass)) {
052    
053                            return true;
054                    }
055    
056                    return false;
057            }
058    
059            public Method getMethod() {
060                    return _method;
061            }
062    
063            public Class<?> getTargetClass() {
064                    return _targetClass;
065            }
066    
067            public Method getTargetMethod() {
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    }