build/annotationProcessors/utils/AlphabeticAnnotatableEntityComparator.java

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

     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/. */
     5 package org.mozilla.gecko.annotationProcessors.utils;
     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;
    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;
    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         }
    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     }
    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();
    54         int ret = lName.compareTo(rName);
    55         if (ret != 0) {
    56             return ret;
    57         }
    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);
    63         return lName.compareTo(rName);
    64     }
    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);
    71         return lName.compareTo(rName);
    72     }
    74     private static int compare(Field aLhs, Field aRhs) {
    75         // Compare field names..
    76         String lName = aLhs.getName();
    77         String rName = aRhs.getName();
    79         return lName.compareTo(rName);
    80     }
    81 }

mercurial