michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.annotationProcessors.utils; michael@0: michael@0: import java.lang.reflect.Constructor; michael@0: import java.lang.reflect.Field; michael@0: import java.lang.reflect.Member; michael@0: import java.lang.reflect.Method; michael@0: import java.util.Comparator; michael@0: michael@0: public class AlphabeticAnnotatableEntityComparator implements Comparator { michael@0: @Override michael@0: public int compare(T aLhs, T aRhs) { michael@0: // Constructors, Methods, Fields. michael@0: boolean lIsConstructor = aLhs instanceof Constructor; michael@0: boolean rIsConstructor = aRhs instanceof Constructor; michael@0: boolean lIsMethod = aLhs instanceof Method; michael@0: boolean rIsField = aRhs instanceof Field; michael@0: michael@0: if (lIsConstructor) { michael@0: if (!rIsConstructor) { michael@0: return -1; michael@0: } michael@0: } else if (lIsMethod) { michael@0: if (rIsConstructor) { michael@0: return 1; michael@0: } else if (rIsField) { michael@0: return -1; michael@0: } michael@0: } else { michael@0: if (!rIsField) { michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: // Verify these objects are the same type and cast them. michael@0: if (aLhs instanceof Method) { michael@0: return compare((Method) aLhs, (Method) aRhs); michael@0: } else if (aLhs instanceof Field) { michael@0: return compare((Field) aLhs, (Field) aRhs); michael@0: } else { michael@0: return compare((Constructor) aLhs, (Constructor) aRhs); michael@0: } michael@0: } michael@0: michael@0: // Alas, the type system fails us. michael@0: private static int compare(Method aLhs, Method aRhs) { michael@0: // Initially, attempt to differentiate the methods be name alone.. michael@0: String lName = aLhs.getName(); michael@0: String rName = aRhs.getName(); michael@0: michael@0: int ret = lName.compareTo(rName); michael@0: if (ret != 0) { michael@0: return ret; michael@0: } michael@0: michael@0: // The names were the same, so we need to compare signatures to find their uniqueness.. michael@0: lName = Utils.getTypeSignatureStringForMethod(aLhs); michael@0: rName = Utils.getTypeSignatureStringForMethod(aRhs); michael@0: michael@0: return lName.compareTo(rName); michael@0: } michael@0: michael@0: private static int compare(Constructor aLhs, Constructor aRhs) { michael@0: // The names will be the same, so we need to compare signatures to find their uniqueness.. michael@0: String lName = Utils.getTypeSignatureString(aLhs); michael@0: String rName = Utils.getTypeSignatureString(aRhs); michael@0: michael@0: return lName.compareTo(rName); michael@0: } michael@0: michael@0: private static int compare(Field aLhs, Field aRhs) { michael@0: // Compare field names.. michael@0: String lName = aLhs.getName(); michael@0: String rName = aRhs.getName(); michael@0: michael@0: return lName.compareTo(rName); michael@0: } michael@0: }