001    /**
002     * Copyright (c) 2000-2012 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                    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            @Override
067            public boolean equals(Object obj) {
068                    if (obj == null) {
069                            return false;
070                    }
071    
072                    MethodKey methodKey = (MethodKey)obj;
073    
074                    if (toString().equals(methodKey.toString())) {
075                            return true;
076                    }
077                    else {
078                            return false;
079                    }
080            }
081    
082            public String getClassName() {
083                    return _className;
084            }
085    
086            public String getMethodName() {
087                    return _methodName;
088            }
089    
090            public Class<?>[] getParameterTypes() {
091                    return _parameterTypes;
092            }
093    
094            @Override
095            public int hashCode() {
096                    return toString().hashCode();
097            }
098    
099            @Override
100            public String toString() {
101                    return _toString();
102            }
103    
104            private String _toString() {
105                    if (_toString == null) {
106                            if ((_parameterTypes != null) && (_parameterTypes.length > 0)) {
107                                    StringBundler sb = new StringBundler(
108                                            3 + _parameterTypes.length);
109    
110                                    sb.append(_className);
111                                    sb.append(_methodName);
112                                    sb.append(StringPool.DASH);
113    
114                                    for (Class<?> parameterType : _parameterTypes) {
115                                            sb.append(parameterType.getName());
116                                    }
117    
118                                    _toString = sb.toString();
119                            }
120                            else {
121                                    _toString = _className.concat(_methodName);
122                            }
123                    }
124    
125                    return _toString;
126            }
127    
128            private String _className;
129            private String _methodName;
130            private Class<?>[] _parameterTypes;
131            private String _toString;
132    
133    }