build/annotationProcessors/utils/AlphabeticAnnotatableEntityComparator.java

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:f00fd0700567
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.gecko.annotationProcessors.utils;
6
7 import java.lang.reflect.Constructor;
8 import java.lang.reflect.Field;
9 import java.lang.reflect.Member;
10 import java.lang.reflect.Method;
11 import java.util.Comparator;
12
13 public class AlphabeticAnnotatableEntityComparator<T extends Member> implements Comparator<T> {
14 @Override
15 public int compare(T aLhs, T aRhs) {
16 // Constructors, Methods, Fields.
17 boolean lIsConstructor = aLhs instanceof Constructor;
18 boolean rIsConstructor = aRhs instanceof Constructor;
19 boolean lIsMethod = aLhs instanceof Method;
20 boolean rIsField = aRhs instanceof Field;
21
22 if (lIsConstructor) {
23 if (!rIsConstructor) {
24 return -1;
25 }
26 } else if (lIsMethod) {
27 if (rIsConstructor) {
28 return 1;
29 } else if (rIsField) {
30 return -1;
31 }
32 } else {
33 if (!rIsField) {
34 return 1;
35 }
36 }
37
38 // Verify these objects are the same type and cast them.
39 if (aLhs instanceof Method) {
40 return compare((Method) aLhs, (Method) aRhs);
41 } else if (aLhs instanceof Field) {
42 return compare((Field) aLhs, (Field) aRhs);
43 } else {
44 return compare((Constructor) aLhs, (Constructor) aRhs);
45 }
46 }
47
48 // Alas, the type system fails us.
49 private static int compare(Method aLhs, Method aRhs) {
50 // Initially, attempt to differentiate the methods be name alone..
51 String lName = aLhs.getName();
52 String rName = aRhs.getName();
53
54 int ret = lName.compareTo(rName);
55 if (ret != 0) {
56 return ret;
57 }
58
59 // The names were the same, so we need to compare signatures to find their uniqueness..
60 lName = Utils.getTypeSignatureStringForMethod(aLhs);
61 rName = Utils.getTypeSignatureStringForMethod(aRhs);
62
63 return lName.compareTo(rName);
64 }
65
66 private static int compare(Constructor aLhs, Constructor aRhs) {
67 // The names will be the same, so we need to compare signatures to find their uniqueness..
68 String lName = Utils.getTypeSignatureString(aLhs);
69 String rName = Utils.getTypeSignatureString(aRhs);
70
71 return lName.compareTo(rName);
72 }
73
74 private static int compare(Field aLhs, Field aRhs) {
75 // Compare field names..
76 String lName = aLhs.getName();
77 String rName = aRhs.getName();
78
79 return lName.compareTo(rName);
80 }
81 }

mercurial