001    /**
002     * Copyright (c) 2000-present 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.monitoring;
016    
017    import com.liferay.portal.kernel.util.HashUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.lang.reflect.Method;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class MethodSignature {
027    
028            public MethodSignature(Method method) {
029                    Class<?> clazz = method.getDeclaringClass();
030    
031                    _className = clazz.getName();
032    
033                    _methodName = method.getName();
034    
035                    Class<?>[] parameterTypes = method.getParameterTypes();
036    
037                    _parameterTypeNames = new String[parameterTypes.length];
038    
039                    for (int i = 0; i < parameterTypes.length; i++) {
040                            _parameterTypeNames[i] = parameterTypes[i].getName();
041                    }
042            }
043    
044            public MethodSignature(
045                    String className, String methodName, String[] parameterTypeNames) {
046    
047                    _className = className;
048                    _methodName = methodName;
049                    _parameterTypeNames = parameterTypeNames;
050            }
051    
052            @Override
053            public boolean equals(Object obj) {
054                    if (this == obj) {
055                            return true;
056                    }
057    
058                    if (!(obj instanceof MethodSignature)) {
059                            return false;
060                    }
061    
062                    MethodSignature methodSignature = (MethodSignature)obj;
063    
064                    if (_className.equals(methodSignature._className) &&
065                            _methodName.equals(methodSignature._methodName) &&
066                            (_parameterTypeNames.length ==
067                                    methodSignature._parameterTypeNames.length)) {
068    
069                            for (int i = 0; i < _parameterTypeNames.length; i++) {
070                                    if (!_parameterTypeNames[i].equals(
071                                                    methodSignature._parameterTypeNames[i])) {
072    
073                                            return false;
074                                    }
075                            }
076    
077                            return true;
078                    }
079    
080                    return false;
081            }
082    
083            public String getClassName() {
084                    return _className;
085            }
086    
087            public String getMethodName() {
088                    return _methodName;
089            }
090    
091            public String[] getParameterTypeNames() {
092                    return _parameterTypeNames;
093            }
094    
095            @Override
096            public int hashCode() {
097                    int hashCode = HashUtil.hash(0, _className);
098    
099                    hashCode = HashUtil.hash(hashCode, _methodName);
100    
101                    for (String parameterTypeName : _parameterTypeNames) {
102                            hashCode = HashUtil.hash(hashCode, parameterTypeName);
103                    }
104    
105                    return hashCode;
106            }
107    
108            @Override
109            public String toString() {
110                    StringBundler sb = new StringBundler(
111                            _parameterTypeNames.length * 2 + 5);
112    
113                    sb.append("{className=");
114                    sb.append(_className);
115                    sb.append(", methodName=");
116                    sb.append(_methodName);
117                    sb.append(", parameterTypeNames=[");
118    
119                    for (String parameterTypeName : _parameterTypeNames) {
120                            sb.append(parameterTypeName);
121                            sb.append(StringPool.COMMA_AND_SPACE);
122                    }
123    
124                    sb.setIndex(sb.index() - 1);
125    
126                    sb.append("]}");
127    
128                    return sb.toString();
129            }
130    
131            private final String _className;
132            private final String _methodName;
133            private final String[] _parameterTypeNames;
134    
135    }