001    /**
002     * Copyright (c) 2000-2013 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    
030            @Override
031            public boolean equals(Object obj) {
032                    if (this == obj) {
033                            return true;
034                    }
035    
036                    if (!(obj instanceof MethodTargetClassKey)) {
037                            return false;
038                    }
039    
040                    MethodTargetClassKey methodTargetClassKey = (MethodTargetClassKey)obj;
041    
042                    if ((_targetClass == methodTargetClassKey._targetClass) &&
043                            Validator.equals(_method, methodTargetClassKey._method)) {
044    
045                            return true;
046                    }
047    
048                    return false;
049            }
050    
051            public Method getMethod() {
052                    return _method;
053            }
054    
055            public Class<?> getTargetClass() {
056                    return _targetClass;
057            }
058    
059            public Method getTargetMethod() {
060                    if ((_targetMethod == null) && (_targetClass != null)) {
061                            try {
062                                    _targetMethod = _targetClass.getDeclaredMethod(
063                                            _method.getName(), _method.getParameterTypes());
064                            }
065                            catch (Throwable t) {
066                            }
067                    }
068    
069                    return _targetMethod;
070            }
071    
072            @Override
073            public int hashCode() {
074                    if (_hashCode == 0) {
075                            int hashCode = 77;
076    
077                            if (_method != null) {
078                                    hashCode += _method.hashCode();
079                            }
080    
081                            hashCode = 11 * hashCode;
082    
083                            if (_targetClass != null) {
084                                    String targetClassName = _targetClass.getName();
085    
086                                    hashCode += targetClassName.hashCode();
087                            }
088    
089                            _hashCode = hashCode;
090                    }
091    
092                    return _hashCode;
093            }
094    
095            @Override
096            public String toString() {
097                    if (_toString != null) {
098                            return _toString;
099                    }
100    
101                    Class<?>[] parameterTypes = _method.getParameterTypes();
102    
103                    StringBundler sb = new StringBundler(parameterTypes.length * 2 + 6);
104    
105                    sb.append(_method.getDeclaringClass().getName());
106                    sb.append(StringPool.PERIOD);
107                    sb.append(_method.getName());
108                    sb.append(StringPool.OPEN_PARENTHESIS);
109    
110                    for (int i = 0; i < parameterTypes.length; i++) {
111                            sb.append(parameterTypes[i].getName());
112    
113                            if ((i + 1) < parameterTypes.length) {
114                                    sb.append(StringPool.COMMA);
115                            }
116                    }
117    
118                    sb.append(StringPool.CLOSE_PARENTHESIS);
119    
120                    if (_targetClass != null) {
121                            sb.append(StringPool.AT);
122                            sb.append(_targetClass.getName());
123                    }
124    
125                    _toString = sb.toString();
126    
127                    return _toString;
128            }
129    
130            private int _hashCode;
131            private Method _method;
132            private Class<?> _targetClass;
133            private Method _targetMethod;
134            private String _toString;
135    
136    }