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.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                    ClassLoader classLoader = null;
051    
052                    if (parameterTypeNames.length > 0) {
053                            Thread currentThread = Thread.currentThread();
054    
055                            classLoader = currentThread.getContextClassLoader();
056                    }
057    
058                    for (int i = 0; i < parameterTypeNames.length; i++) {
059                            String parameterTypeName = parameterTypeNames[i];
060    
061                            _parameterTypes[i] = Class.forName(
062                                    parameterTypeName, true, classLoader);
063                    }
064            }
065    
066            public boolean equals(Object obj) {
067                    if (obj == null) {
068                            return false;
069                    }
070    
071                    MethodKey methodKey = (MethodKey)obj;
072    
073                    if (toString().equals(methodKey.toString())) {
074                            return true;
075                    }
076                    else {
077                            return false;
078                    }
079            }
080    
081            public String getClassName() {
082                    return _className;
083            }
084    
085            public String getMethodName() {
086                    return _methodName;
087            }
088    
089            public Class<?>[] getParameterTypes() {
090                    return _parameterTypes;
091            }
092    
093            public int hashCode() {
094                    return toString().hashCode();
095            }
096    
097            public String toString() {
098                    return _toString();
099            }
100    
101            private String _toString() {
102                    if (_toString == null) {
103                            StringBundler sb = new StringBundler();
104    
105                            sb.append(_className);
106                            sb.append(_methodName);
107    
108                            if ((_parameterTypes != null) && (_parameterTypes.length > 0)) {
109                                    sb.append(StringPool.DASH);
110    
111                                    for (Class<?> parameterType : _parameterTypes) {
112                                            sb.append(parameterType.getName());
113                                    }
114                            }
115    
116                            _toString = sb.toString();
117                    }
118    
119                    return _toString;
120            }
121    
122            private String _className;
123            private String _methodName;
124            private Class<?>[] _parameterTypes;
125            private String _toString;
126    
127    }