| MethodComparator.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
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 }