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.lang.reflect.Method;
018    
019    import java.util.Comparator;
020    
021    /**
022     * @author Shuyang Zhou
023     * @author Brian Wing Shun Chan
024     */
025    public class MethodComparator implements Comparator<Method> {
026    
027            public int compare(Method method1, Method method2) {
028                    String name1 = method1.getName();
029                    String name2 = method2.getName();
030    
031                    int value = name1.compareTo(name2);
032    
033                    if (value != 0){
034                            return value;
035                    }
036    
037                    Class<?>[] parameterTypes1 = method1.getParameterTypes();
038                    Class<?>[] parameterTypes2 = method2.getParameterTypes();
039    
040                    int index = 0;
041    
042                    while ((index < parameterTypes1.length) &&
043                               (index < parameterTypes2.length)) {
044    
045                            Class<?> parameterType1 = parameterTypes1[index];
046                            Class<?> parameterType2 = parameterTypes2[index];
047    
048                            String parameterTypeName1 = parameterType1.getName();
049                            String parameterTypeName2 = parameterType2.getName();
050    
051                            value = parameterTypeName1.compareTo(parameterTypeName2);
052    
053                            if (value != 0) {
054                                    return value;
055                            }
056    
057                            index++;
058                    }
059    
060                    if (index < (parameterTypes1.length -1)) {
061                            return -1;
062                    }
063                    else {
064                            return 1;
065                    }
066            }
067    
068    }