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