001    /**
002     * Copyright (c) 2000-2013 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.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            @Override
028            public int compare(Method method1, Method method2) {
029                    String name1 = method1.getName();
030                    String name2 = method2.getName();
031    
032                    int value = name1.compareTo(name2);
033    
034                    if (value != 0) {
035                            return value;
036                    }
037    
038                    Class<?>[] parameterTypes1 = method1.getParameterTypes();
039                    Class<?>[] parameterTypes2 = method2.getParameterTypes();
040    
041                    int index = 0;
042    
043                    while ((index < parameterTypes1.length) &&
044                               (index < parameterTypes2.length)) {
045    
046                            Class<?> parameterType1 = parameterTypes1[index];
047                            Class<?> parameterType2 = parameterTypes2[index];
048    
049                            String parameterTypeName1 = parameterType1.getName();
050                            String parameterTypeName2 = parameterType2.getName();
051    
052                            value = parameterTypeName1.compareTo(parameterTypeName2);
053    
054                            if (value != 0) {
055                                    return value;
056                            }
057    
058                            index++;
059                    }
060    
061                    if (index < (parameterTypes1.length -1)) {
062                            return -1;
063                    }
064                    else {
065                            return 1;
066                    }
067            }
068    
069    }