build/annotationProcessors/utils/AlphabeticAnnotatableEntityComparator.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/build/annotationProcessors/utils/AlphabeticAnnotatableEntityComparator.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.annotationProcessors.utils;
     1.9 +
    1.10 +import java.lang.reflect.Constructor;
    1.11 +import java.lang.reflect.Field;
    1.12 +import java.lang.reflect.Member;
    1.13 +import java.lang.reflect.Method;
    1.14 +import java.util.Comparator;
    1.15 +
    1.16 +public class AlphabeticAnnotatableEntityComparator<T extends Member> implements Comparator<T> {
    1.17 +    @Override
    1.18 +    public int compare(T aLhs, T aRhs) {
    1.19 +        // Constructors, Methods, Fields.
    1.20 +        boolean lIsConstructor = aLhs instanceof Constructor;
    1.21 +        boolean rIsConstructor = aRhs instanceof Constructor;
    1.22 +        boolean lIsMethod = aLhs instanceof Method;
    1.23 +        boolean rIsField = aRhs instanceof Field;
    1.24 +
    1.25 +        if (lIsConstructor) {
    1.26 +            if (!rIsConstructor) {
    1.27 +                return -1;
    1.28 +            }
    1.29 +        } else if (lIsMethod) {
    1.30 +            if (rIsConstructor) {
    1.31 +                return 1;
    1.32 +            } else if (rIsField) {
    1.33 +                return -1;
    1.34 +            }
    1.35 +        } else {
    1.36 +            if (!rIsField) {
    1.37 +                return 1;
    1.38 +            }
    1.39 +        }
    1.40 +
    1.41 +        // Verify these objects are the same type and cast them.
    1.42 +        if (aLhs instanceof Method) {
    1.43 +            return compare((Method) aLhs, (Method) aRhs);
    1.44 +        } else if (aLhs instanceof Field) {
    1.45 +            return compare((Field) aLhs, (Field) aRhs);
    1.46 +        } else {
    1.47 +            return compare((Constructor) aLhs, (Constructor) aRhs);
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    // Alas, the type system fails us.
    1.52 +    private static int compare(Method aLhs, Method aRhs) {
    1.53 +        // Initially, attempt to differentiate the methods be name alone..
    1.54 +        String lName = aLhs.getName();
    1.55 +        String rName = aRhs.getName();
    1.56 +
    1.57 +        int ret = lName.compareTo(rName);
    1.58 +        if (ret != 0) {
    1.59 +            return ret;
    1.60 +        }
    1.61 +
    1.62 +        // The names were the same, so we need to compare signatures to find their uniqueness..
    1.63 +        lName = Utils.getTypeSignatureStringForMethod(aLhs);
    1.64 +        rName = Utils.getTypeSignatureStringForMethod(aRhs);
    1.65 +
    1.66 +        return lName.compareTo(rName);
    1.67 +    }
    1.68 +
    1.69 +    private static int compare(Constructor aLhs, Constructor aRhs) {
    1.70 +        // The names will be the same, so we need to compare signatures to find their uniqueness..
    1.71 +        String lName = Utils.getTypeSignatureString(aLhs);
    1.72 +        String rName = Utils.getTypeSignatureString(aRhs);
    1.73 +
    1.74 +        return lName.compareTo(rName);
    1.75 +    }
    1.76 +
    1.77 +    private static int compare(Field aLhs, Field aRhs) {
    1.78 +        // Compare field names..
    1.79 +        String lName = aLhs.getName();
    1.80 +        String rName = aRhs.getName();
    1.81 +
    1.82 +        return lName.compareTo(rName);
    1.83 +    }
    1.84 +}

mercurial