001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.resolve;
018    
019    import kotlin.Unit;
020    import kotlin.jvm.functions.Function1;
021    import org.jetbrains.kotlin.descriptors.*;
022    import org.jetbrains.kotlin.name.Name;
023    import org.jetbrains.kotlin.renderer.AnnotationArgumentsRenderingPolicy;
024    import org.jetbrains.kotlin.renderer.DescriptorRenderer;
025    import org.jetbrains.kotlin.renderer.DescriptorRendererModifier;
026    import org.jetbrains.kotlin.renderer.DescriptorRendererOptions;
027    import org.jetbrains.kotlin.types.KotlinType;
028    
029    import java.util.Comparator;
030    import java.util.List;
031    
032    import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry;
033    
034    public class MemberComparator implements Comparator<DeclarationDescriptor> {
035        public static final MemberComparator INSTANCE = new MemberComparator();
036    
037        private static final DescriptorRenderer RENDERER = DescriptorRenderer.Companion.withOptions(
038                new Function1<DescriptorRendererOptions, Unit>() {
039                    @Override
040                    public Unit invoke(DescriptorRendererOptions options) {
041                        options.setWithDefinedIn(false);
042                        options.setVerbose(true);
043                        options.setAnnotationArgumentsRenderingPolicy(AnnotationArgumentsRenderingPolicy.UNLESS_EMPTY);
044                        options.setModifiers(DescriptorRendererModifier.ALL);
045                        return Unit.INSTANCE;
046                    }
047                });
048    
049        private MemberComparator() {
050        }
051    
052        private static int getDeclarationPriority(DeclarationDescriptor descriptor) {
053            if (isEnumEntry(descriptor)) {
054                return 8;
055            }
056            else if (descriptor instanceof ConstructorDescriptor) {
057                return 7;
058            }
059            else if (descriptor instanceof PropertyDescriptor) {
060                if (((PropertyDescriptor)descriptor).getExtensionReceiverParameter() == null) {
061                    return 6;
062                }
063                else {
064                    return 5;
065                }
066            }
067            else if (descriptor instanceof FunctionDescriptor) {
068                if (((FunctionDescriptor)descriptor).getExtensionReceiverParameter() == null) {
069                    return 4;
070                }
071                else {
072                    return 3;
073                }
074            }
075            else if (descriptor instanceof ClassDescriptor) {
076                return 2;
077            }
078            else if (descriptor instanceof TypeAliasDescriptor) {
079                return 1;
080            }
081            return 0;
082        }
083    
084        @Override
085        public int compare(DeclarationDescriptor o1, DeclarationDescriptor o2) {
086            int prioritiesCompareTo = getDeclarationPriority(o2) - getDeclarationPriority(o1);
087            if (prioritiesCompareTo != 0) {
088                return prioritiesCompareTo;
089            }
090            if (isEnumEntry(o1) && isEnumEntry(o2)) {
091                //never reorder enum entries
092                return 0;
093            }
094    
095            int namesCompareTo = o1.getName().compareTo(o2.getName());
096            if (namesCompareTo != 0) {
097                return namesCompareTo;
098            }
099    
100            if (o1 instanceof TypeAliasDescriptor && o2 instanceof TypeAliasDescriptor) {
101                TypeAliasDescriptor ta1 = (TypeAliasDescriptor) o1;
102                TypeAliasDescriptor ta2 = (TypeAliasDescriptor) o2;
103                String r1 = RENDERER.renderType(ta1.getUnderlyingType());
104                String r2 = RENDERER.renderType(ta2.getUnderlyingType());
105                int underlyingTypesCompareTo = r1.compareTo(r2);
106                if (underlyingTypesCompareTo != 0) {
107                    return underlyingTypesCompareTo;
108                }
109            }
110            else if (o1 instanceof CallableDescriptor && o2 instanceof CallableDescriptor) {
111                CallableDescriptor c1 = (CallableDescriptor) o1;
112                CallableDescriptor c2 = (CallableDescriptor) o2;
113    
114                ReceiverParameterDescriptor c1ReceiverParameter = c1.getExtensionReceiverParameter();
115                ReceiverParameterDescriptor c2ReceiverParameter = c2.getExtensionReceiverParameter();
116                assert (c1ReceiverParameter != null) == (c2ReceiverParameter != null);
117                if (c1ReceiverParameter != null) {
118                    String r1 = RENDERER.renderType(c1ReceiverParameter.getType());
119                    String r2 = RENDERER.renderType(c2ReceiverParameter.getType());
120                    int receiversCompareTo = r1.compareTo(r2);
121                    if (receiversCompareTo != 0) {
122                        return receiversCompareTo;
123                    }
124                }
125    
126                List<ValueParameterDescriptor> c1ValueParameters = c1.getValueParameters();
127                List<ValueParameterDescriptor> c2ValueParameters = c2.getValueParameters();
128                for (int i = 0; i < Math.min(c1ValueParameters.size(), c2ValueParameters.size()); i++) {
129                    String p1 = RENDERER.renderType(c1ValueParameters.get(i).getType());
130                    String p2 = RENDERER.renderType(c2ValueParameters.get(i).getType());
131                    int parametersCompareTo = p1.compareTo(p2);
132                    if (parametersCompareTo != 0) {
133                        return parametersCompareTo;
134                    }
135                }
136    
137                int valueParametersNumberCompareTo = c1ValueParameters.size() - c2ValueParameters.size();
138                if (valueParametersNumberCompareTo != 0) {
139                    return valueParametersNumberCompareTo;
140                }
141    
142                List<TypeParameterDescriptor> c1TypeParameters = c1.getTypeParameters();
143                List<TypeParameterDescriptor> c2TypeParameters = c2.getTypeParameters();
144                for (int i = 0; i < Math.min(c1TypeParameters.size(), c2TypeParameters.size()); i++) {
145                    List<KotlinType> c1Bounds = c1TypeParameters.get(i).getUpperBounds();
146                    List<KotlinType> c2Bounds = c2TypeParameters.get(i).getUpperBounds();
147                    int boundsCountCompareTo = c1Bounds.size() - c2Bounds.size();
148                    if (boundsCountCompareTo != 0) {
149                        return boundsCountCompareTo;
150                    }
151                    for (int j = 0; j < c1Bounds.size(); j++) {
152                        String b1 = RENDERER.renderType(c1Bounds.get(j));
153                        String b2 = RENDERER.renderType(c2Bounds.get(j));
154                        int boundCompareTo = b1.compareTo(b2);
155                        if (boundCompareTo != 0) {
156                            return boundCompareTo;
157                        }
158                    }
159                }
160    
161                int typeParametersCompareTo = c1TypeParameters.size() - c2TypeParameters.size();
162                if (typeParametersCompareTo != 0) {
163                    return typeParametersCompareTo;
164                }
165    
166                if (c1 instanceof CallableMemberDescriptor && c2 instanceof CallableMemberDescriptor) {
167                    CallableMemberDescriptor.Kind c1Kind = ((CallableMemberDescriptor) c1).getKind();
168                    CallableMemberDescriptor.Kind c2Kind = ((CallableMemberDescriptor) c2).getKind();
169                    int kindsCompareTo = c1Kind.ordinal() - c2Kind.ordinal();
170                    if (kindsCompareTo != 0) {
171                        return kindsCompareTo;
172                    }
173                }
174            }
175            else if (o1 instanceof ClassDescriptor && o2 instanceof ClassDescriptor) {
176                ClassDescriptor class1 = (ClassDescriptor) o1;
177                ClassDescriptor class2 = (ClassDescriptor) o2;
178    
179                if (class1.getKind().ordinal() != class2.getKind().ordinal()) {
180                    return class1.getKind().ordinal() - class2.getKind().ordinal();
181                }
182    
183                if (class1.isCompanionObject() != class2.isCompanionObject()) {
184                    return class1.isCompanionObject() ? 1 : -1;
185                }
186            }
187            else {
188                throw new AssertionError(String.format(
189                        "Unsupported pair of descriptors:\n'" +
190                        "%s' Class: %s\n" +
191                        "%s' Class: %s",
192                        o1, o1.getClass(), o2, o2.getClass()));
193            }
194    
195            int renderDiff = RENDERER.render(o1).compareTo(RENDERER.render(o2));
196            if (renderDiff != 0) return renderDiff;
197    
198            Name firstModuleName = DescriptorUtils.getContainingModule(o1).getName();
199            Name secondModuleName = DescriptorUtils.getContainingModule(o2).getName();
200    
201            return firstModuleName.compareTo(secondModuleName);
202        }
203    }