1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.lang.reflect.Method;
18  
19  import java.util.Comparator;
20  
21  /**
22   * <a href="MethodComparator.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Shuyang Zhou
25   * @author Brian Wing Shun Chan
26   */
27  public class MethodComparator implements Comparator<Method> {
28  
29      public int compare(Method method1, Method method2) {
30          String name1 = method1.getName();
31          String name2 = method2.getName();
32  
33          int value = name1.compareTo(name2);
34  
35          if (value != 0){
36              return value;
37          }
38  
39          Class<?>[] parameterTypes1 = method1.getParameterTypes();
40          Class<?>[] parameterTypes2 = method2.getParameterTypes();
41  
42          int index = 0;
43  
44          while ((index < parameterTypes1.length) &&
45                 (index < parameterTypes2.length)) {
46  
47              Class<?> parameterType1 = parameterTypes1[index];
48              Class<?> parameterType2 = parameterTypes2[index];
49  
50              String parameterTypeName1 = parameterType1.getName();
51              String parameterTypeName2 = parameterType2.getName();
52  
53              value = parameterTypeName1.compareTo(parameterTypeName2);
54  
55              if (value != 0) {
56                  return value;
57              }
58  
59              index++;
60          }
61  
62          if (index < (parameterTypes1.length -1)) {
63              return -1;
64          }
65          else {
66              return 1;
67          }
68      }
69  
70  }