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