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.io.Serializable;
018    
019    import java.lang.reflect.Method;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     * @author Shuyang Zhou
024     */
025    public class MethodKey implements Serializable {
026    
027            public MethodKey(Method method) {
028                    this(
029                            method.getDeclaringClass().getName(), method.getName(),
030                            method.getParameterTypes());
031            }
032    
033            public MethodKey(
034                    String className, String methodName, Class<?>... parameterTypes) {
035    
036                    _className = className;
037                    _methodName = methodName;
038                    _parameterTypes = parameterTypes;
039            }
040    
041            public MethodKey(
042                            String className, String methodName, String[] parameterTypeNames)
043                    throws ClassNotFoundException {
044    
045                    _className = className;
046                    _methodName = methodName;
047    
048                    _parameterTypes = new Class[parameterTypeNames.length];
049    
050                    for (int i = 0; i < parameterTypeNames.length; i++) {
051                            String parameterTypeName = parameterTypeNames[i];
052    
053                            _parameterTypes[i] = Class.forName(parameterTypeName);
054                    }
055            }
056    
057            public boolean equals(Object obj) {
058                    if (obj == null) {
059                            return false;
060                    }
061    
062                    MethodKey methodKey = (MethodKey)obj;
063    
064                    if (toString().equals(methodKey.toString())) {
065                            return true;
066                    }
067                    else {
068                            return false;
069                    }
070            }
071    
072            public String getClassName() {
073                    return _className;
074            }
075    
076            public String getMethodName() {
077                    return _methodName;
078            }
079    
080            public Class<?>[] getParameterTypes() {
081                    return _parameterTypes;
082            }
083    
084            public int hashCode() {
085                    return toString().hashCode();
086            }
087    
088            public String toString() {
089                    return _toString();
090            }
091    
092            private String _toString() {
093                    if (_toString == null) {
094                            StringBundler sb = new StringBundler();
095    
096                            sb.append(_className);
097                            sb.append(_methodName);
098    
099                            if ((_parameterTypes != null) && (_parameterTypes.length > 0)) {
100                                    sb.append(StringPool.DASH);
101    
102                                    for (Class<?> parameterType : _parameterTypes) {
103                                            sb.append(parameterType.getName());
104                                    }
105                            }
106    
107                            _toString = sb.toString();
108                    }
109    
110                    return _toString;
111            }
112    
113            private String _className;
114            private String _methodName;
115            private Class<?>[] _parameterTypes;
116            private String _toString;
117    
118    }