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.serialization;
018
019 import com.google.protobuf.MessageLite;
020 import kotlin.CollectionsKt;
021 import kotlin.jvm.functions.Function1;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
025 import org.jetbrains.kotlin.descriptors.*;
026 import org.jetbrains.kotlin.descriptors.annotations.Annotated;
027 import org.jetbrains.kotlin.name.Name;
028 import org.jetbrains.kotlin.resolve.DescriptorUtils;
029 import org.jetbrains.kotlin.resolve.MemberComparator;
030 import org.jetbrains.kotlin.resolve.constants.ConstantValue;
031 import org.jetbrains.kotlin.resolve.constants.NullValue;
032 import org.jetbrains.kotlin.types.*;
033 import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
034 import org.jetbrains.kotlin.utils.Interner;
035
036 import java.io.ByteArrayOutputStream;
037 import java.io.IOException;
038 import java.util.ArrayList;
039 import java.util.Collection;
040 import java.util.Collections;
041 import java.util.List;
042
043 import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry;
044
045 public class DescriptorSerializer {
046 private final DeclarationDescriptor containingDeclaration;
047 private final Interner<TypeParameterDescriptor> typeParameters;
048 private final SerializerExtension extension;
049 private final MutableTypeTable typeTable;
050 private final boolean serializeTypeTableToFunction;
051
052 private DescriptorSerializer(
053 @Nullable DeclarationDescriptor containingDeclaration,
054 @NotNull Interner<TypeParameterDescriptor> typeParameters,
055 @NotNull SerializerExtension extension,
056 @NotNull MutableTypeTable typeTable,
057 boolean serializeTypeTableToFunction
058 ) {
059 this.containingDeclaration = containingDeclaration;
060 this.typeParameters = typeParameters;
061 this.extension = extension;
062 this.typeTable = typeTable;
063 this.serializeTypeTableToFunction = serializeTypeTableToFunction;
064 }
065
066 @NotNull
067 public byte[] serialize(@NotNull MessageLite message) {
068 try {
069 ByteArrayOutputStream result = new ByteArrayOutputStream();
070 getStringTable().serializeTo(result);
071 message.writeTo(result);
072 return result.toByteArray();
073 }
074 catch (IOException e) {
075 throw ExceptionUtilsKt.rethrow(e);
076 }
077 }
078
079 @NotNull
080 public static DescriptorSerializer createTopLevel(@NotNull SerializerExtension extension) {
081 return new DescriptorSerializer(null, new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), false);
082 }
083
084 @NotNull
085 public static DescriptorSerializer createForLambda(@NotNull SerializerExtension extension) {
086 return new DescriptorSerializer(null, new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), true);
087 }
088
089 @NotNull
090 public static DescriptorSerializer create(@NotNull ClassDescriptor descriptor, @NotNull SerializerExtension extension) {
091 DeclarationDescriptor container = descriptor.getContainingDeclaration();
092 DescriptorSerializer parentSerializer =
093 container instanceof ClassDescriptor
094 ? create((ClassDescriptor) container, extension)
095 : createTopLevel(extension);
096
097 // Calculate type parameter ids for the outer class beforehand, as it would've had happened if we were always
098 // serializing outer classes before nested classes.
099 // Otherwise our interner can get wrong ids because we may serialize classes in any order.
100 DescriptorSerializer serializer = new DescriptorSerializer(
101 descriptor,
102 new Interner<TypeParameterDescriptor>(parentSerializer.typeParameters),
103 parentSerializer.extension,
104 new MutableTypeTable(),
105 false
106 );
107 for (TypeParameterDescriptor typeParameter : descriptor.getDeclaredTypeParameters()) {
108 serializer.typeParameters.intern(typeParameter);
109 }
110 return serializer;
111 }
112
113 @NotNull
114 private DescriptorSerializer createChildSerializer(@NotNull CallableDescriptor callable) {
115 return new DescriptorSerializer(callable, new Interner<TypeParameterDescriptor>(typeParameters), extension, typeTable, false);
116 }
117
118 @NotNull
119 public StringTable getStringTable() {
120 return extension.getStringTable();
121 }
122
123 private boolean useTypeTable() {
124 return extension.shouldUseTypeTable();
125 }
126
127 @NotNull
128 public ProtoBuf.Class.Builder classProto(@NotNull ClassDescriptor classDescriptor) {
129 ProtoBuf.Class.Builder builder = ProtoBuf.Class.newBuilder();
130
131 int flags = Flags.getClassFlags(hasAnnotations(classDescriptor), classDescriptor.getVisibility(), classDescriptor.getModality(),
132 classDescriptor.getKind(), classDescriptor.isInner(), classDescriptor.isCompanionObject(),
133 classDescriptor.isData());
134 if (flags != builder.getFlags()) {
135 builder.setFlags(flags);
136 }
137
138 builder.setFqName(getClassId(classDescriptor));
139
140 for (TypeParameterDescriptor typeParameterDescriptor : classDescriptor.getDeclaredTypeParameters()) {
141 builder.addTypeParameter(typeParameter(typeParameterDescriptor));
142 }
143
144 if (!KotlinBuiltIns.isSpecialClassWithNoSupertypes(classDescriptor)) {
145 // Special classes (Any, Nothing) have no supertypes
146 for (KotlinType supertype : classDescriptor.getTypeConstructor().getSupertypes()) {
147 if (useTypeTable()) {
148 builder.addSupertypeId(typeId(supertype));
149 }
150 else {
151 builder.addSupertype(type(supertype));
152 }
153 }
154 }
155
156 for (ConstructorDescriptor descriptor : classDescriptor.getConstructors()) {
157 builder.addConstructor(constructorProto(descriptor));
158 }
159
160 for (DeclarationDescriptor descriptor : sort(DescriptorUtils.getAllDescriptors(classDescriptor.getDefaultType().getMemberScope()))) {
161 if (descriptor instanceof CallableMemberDescriptor) {
162 CallableMemberDescriptor member = (CallableMemberDescriptor) descriptor;
163 if (member.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) continue;
164
165 if (descriptor instanceof PropertyDescriptor) {
166 builder.addProperty(propertyProto((PropertyDescriptor) descriptor));
167 }
168 else if (descriptor instanceof FunctionDescriptor) {
169 builder.addFunction(functionProto((FunctionDescriptor) descriptor));
170 }
171 }
172 }
173
174 for (DeclarationDescriptor descriptor : sort(DescriptorUtils.getAllDescriptors(classDescriptor.getUnsubstitutedInnerClassesScope()))) {
175 int name = getSimpleNameIndex(descriptor.getName());
176 if (isEnumEntry(descriptor)) {
177 builder.addEnumEntry(name);
178 }
179 else {
180 builder.addNestedClassName(name);
181 }
182 }
183
184 ClassDescriptor companionObjectDescriptor = classDescriptor.getCompanionObjectDescriptor();
185 if (companionObjectDescriptor != null) {
186 builder.setCompanionObjectName(getSimpleNameIndex(companionObjectDescriptor.getName()));
187 }
188
189 ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
190 if (typeTableProto != null) {
191 builder.setTypeTable(typeTableProto);
192 }
193
194 extension.serializeClass(classDescriptor, builder);
195
196 return builder;
197 }
198
199 @NotNull
200 public ProtoBuf.Property.Builder propertyProto(@NotNull PropertyDescriptor descriptor) {
201 ProtoBuf.Property.Builder builder = ProtoBuf.Property.newBuilder();
202
203 DescriptorSerializer local = createChildSerializer(descriptor);
204
205 boolean hasGetter = false;
206 boolean hasSetter = false;
207 boolean lateInit = descriptor.isLateInit();
208 boolean isConst = descriptor.isConst();
209
210 ConstantValue<?> compileTimeConstant = descriptor.getCompileTimeInitializer();
211 boolean hasConstant = !(compileTimeConstant == null || compileTimeConstant instanceof NullValue);
212
213 boolean hasAnnotations = !descriptor.getAnnotations().getAllAnnotations().isEmpty();
214
215 int propertyFlags = Flags.getAccessorFlags(
216 hasAnnotations,
217 descriptor.getVisibility(),
218 descriptor.getModality(),
219 false,
220 false
221 );
222
223 PropertyGetterDescriptor getter = descriptor.getGetter();
224 if (getter != null) {
225 hasGetter = true;
226 int accessorFlags = getAccessorFlags(getter);
227 if (accessorFlags != propertyFlags) {
228 builder.setGetterFlags(accessorFlags);
229 }
230 }
231
232 PropertySetterDescriptor setter = descriptor.getSetter();
233 if (setter != null) {
234 hasSetter = true;
235 int accessorFlags = getAccessorFlags(setter);
236 if (accessorFlags != propertyFlags) {
237 builder.setSetterFlags(accessorFlags);
238 }
239
240 if (!setter.isDefault()) {
241 DescriptorSerializer setterLocal = local.createChildSerializer(setter);
242 for (ValueParameterDescriptor valueParameterDescriptor : setter.getValueParameters()) {
243 builder.setSetterValueParameter(setterLocal.valueParameter(valueParameterDescriptor));
244 }
245 }
246 }
247
248 int flags = Flags.getPropertyFlags(
249 hasAnnotations, descriptor.getVisibility(), descriptor.getModality(), descriptor.getKind(), descriptor.isVar(),
250 hasGetter, hasSetter, hasConstant, isConst, lateInit
251 );
252 if (flags != builder.getFlags()) {
253 builder.setFlags(flags);
254 }
255
256 builder.setName(getSimpleNameIndex(descriptor.getName()));
257
258 if (useTypeTable()) {
259 builder.setReturnTypeId(local.typeId(descriptor.getType()));
260 }
261 else {
262 builder.setReturnType(local.type(descriptor.getType()));
263 }
264
265 for (TypeParameterDescriptor typeParameterDescriptor : descriptor.getTypeParameters()) {
266 builder.addTypeParameter(local.typeParameter(typeParameterDescriptor));
267 }
268
269 ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
270 if (receiverParameter != null) {
271 if (useTypeTable()) {
272 builder.setReceiverTypeId(local.typeId(receiverParameter.getType()));
273 }
274 else {
275 builder.setReceiverType(local.type(receiverParameter.getType()));
276 }
277 }
278
279 extension.serializeProperty(descriptor, builder);
280
281 return builder;
282 }
283
284 @NotNull
285 public ProtoBuf.Function.Builder functionProto(@NotNull FunctionDescriptor descriptor) {
286 ProtoBuf.Function.Builder builder = ProtoBuf.Function.newBuilder();
287
288 DescriptorSerializer local = createChildSerializer(descriptor);
289
290 int flags = Flags.getFunctionFlags(
291 hasAnnotations(descriptor), descriptor.getVisibility(), descriptor.getModality(), descriptor.getKind(),
292 descriptor.isOperator(), descriptor.isInfix(), descriptor.isInline(), descriptor.isTailrec(),
293 descriptor.isExternal()
294 );
295 if (flags != builder.getFlags()) {
296 builder.setFlags(flags);
297 }
298
299 builder.setName(getSimpleNameIndex(descriptor.getName()));
300
301 if (useTypeTable()) {
302 //noinspection ConstantConditions
303 builder.setReturnTypeId(local.typeId(descriptor.getReturnType()));
304 }
305 else {
306 //noinspection ConstantConditions
307 builder.setReturnType(local.type(descriptor.getReturnType()));
308 }
309
310 for (TypeParameterDescriptor typeParameterDescriptor : descriptor.getTypeParameters()) {
311 builder.addTypeParameter(local.typeParameter(typeParameterDescriptor));
312 }
313
314 ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
315 if (receiverParameter != null) {
316 if (useTypeTable()) {
317 builder.setReceiverTypeId(local.typeId(receiverParameter.getType()));
318 }
319 else {
320 builder.setReceiverType(local.type(receiverParameter.getType()));
321 }
322 }
323
324 for (ValueParameterDescriptor valueParameterDescriptor : descriptor.getValueParameters()) {
325 builder.addValueParameter(local.valueParameter(valueParameterDescriptor));
326 }
327
328 if (serializeTypeTableToFunction) {
329 ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
330 if (typeTableProto != null) {
331 builder.setTypeTable(typeTableProto);
332 }
333 }
334
335 extension.serializeFunction(descriptor, builder);
336
337 return builder;
338 }
339
340 @NotNull
341 public ProtoBuf.Constructor.Builder constructorProto(@NotNull ConstructorDescriptor descriptor) {
342 ProtoBuf.Constructor.Builder builder = ProtoBuf.Constructor.newBuilder();
343
344 DescriptorSerializer local = createChildSerializer(descriptor);
345
346 int flags = Flags.getConstructorFlags(hasAnnotations(descriptor), descriptor.getVisibility(), !descriptor.isPrimary());
347 if (flags != builder.getFlags()) {
348 builder.setFlags(flags);
349 }
350
351 for (ValueParameterDescriptor valueParameterDescriptor : descriptor.getValueParameters()) {
352 builder.addValueParameter(local.valueParameter(valueParameterDescriptor));
353 }
354
355 extension.serializeConstructor(descriptor, builder);
356
357 return builder;
358 }
359
360 private static int getAccessorFlags(@NotNull PropertyAccessorDescriptor accessor) {
361 return Flags.getAccessorFlags(
362 hasAnnotations(accessor),
363 accessor.getVisibility(),
364 accessor.getModality(),
365 !accessor.isDefault(),
366 accessor.isExternal()
367 );
368 }
369
370 @NotNull
371 private ProtoBuf.ValueParameter.Builder valueParameter(@NotNull ValueParameterDescriptor descriptor) {
372 ProtoBuf.ValueParameter.Builder builder = ProtoBuf.ValueParameter.newBuilder();
373
374 int flags = Flags.getValueParameterFlags(hasAnnotations(descriptor), descriptor.declaresDefaultValue(),
375 descriptor.isCrossinline(), descriptor.isNoinline());
376 if (flags != builder.getFlags()) {
377 builder.setFlags(flags);
378 }
379
380 builder.setName(getSimpleNameIndex(descriptor.getName()));
381
382 if (useTypeTable()) {
383 builder.setTypeId(typeId(descriptor.getType()));
384 }
385 else {
386 builder.setType(type(descriptor.getType()));
387 }
388
389 KotlinType varargElementType = descriptor.getVarargElementType();
390 if (varargElementType != null) {
391 if (useTypeTable()) {
392 builder.setVarargElementTypeId(typeId(varargElementType));
393 }
394 else {
395 builder.setVarargElementType(type(varargElementType));
396 }
397 }
398
399 extension.serializeValueParameter(descriptor, builder);
400
401 return builder;
402 }
403
404 private ProtoBuf.TypeParameter.Builder typeParameter(TypeParameterDescriptor typeParameter) {
405 ProtoBuf.TypeParameter.Builder builder = ProtoBuf.TypeParameter.newBuilder();
406
407 builder.setId(getTypeParameterId(typeParameter));
408
409 builder.setName(getSimpleNameIndex(typeParameter.getName()));
410
411 if (typeParameter.isReified() != builder.getReified()) {
412 builder.setReified(typeParameter.isReified());
413 }
414
415 ProtoBuf.TypeParameter.Variance variance = variance(typeParameter.getVariance());
416 if (variance != builder.getVariance()) {
417 builder.setVariance(variance);
418 }
419 extension.serializeTypeParameter(typeParameter, builder);
420
421 List<KotlinType> upperBounds = typeParameter.getUpperBounds();
422 if (upperBounds.size() == 1 && KotlinBuiltIns.isDefaultBound(CollectionsKt.single(upperBounds))) return builder;
423
424 for (KotlinType upperBound : upperBounds) {
425 if (useTypeTable()) {
426 builder.addUpperBoundId(typeId(upperBound));
427 }
428 else {
429 builder.addUpperBound(type(upperBound));
430 }
431 }
432
433 return builder;
434 }
435
436 private static ProtoBuf.TypeParameter.Variance variance(Variance variance) {
437 switch (variance) {
438 case INVARIANT:
439 return ProtoBuf.TypeParameter.Variance.INV;
440 case IN_VARIANCE:
441 return ProtoBuf.TypeParameter.Variance.IN;
442 case OUT_VARIANCE:
443 return ProtoBuf.TypeParameter.Variance.OUT;
444 }
445 throw new IllegalStateException("Unknown variance: " + variance);
446 }
447
448 private int typeId(@NotNull KotlinType type) {
449 return typeTable.get(type(type));
450 }
451
452 @NotNull
453 private ProtoBuf.Type.Builder type(@NotNull KotlinType type) {
454 assert !type.isError() : "Can't serialize error types: " + type; // TODO
455
456 if (FlexibleTypesKt.isFlexible(type)) {
457 Flexibility flexibility = FlexibleTypesKt.flexibility(type);
458
459 ProtoBuf.Type.Builder lowerBound = type(flexibility.getLowerBound());
460 lowerBound.setFlexibleTypeCapabilitiesId(getStringTable().getStringIndex(flexibility.getExtraCapabilities().getId()));
461 if (useTypeTable()) {
462 lowerBound.setFlexibleUpperBoundId(typeId(flexibility.getUpperBound()));
463 }
464 else {
465 lowerBound.setFlexibleUpperBound(type(flexibility.getUpperBound()));
466 }
467 return lowerBound;
468 }
469
470 ProtoBuf.Type.Builder builder = ProtoBuf.Type.newBuilder();
471
472 ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
473 if (descriptor instanceof ClassDescriptor) {
474 PossiblyInnerType possiblyInnerType = TypeParameterUtilsKt.buildPossiblyInnerType(type);
475 assert possiblyInnerType != null : "possiblyInnerType should not be null in case of class";
476
477 fillFromPossiblyInnerType(builder, possiblyInnerType);
478
479 }
480 if (descriptor instanceof TypeParameterDescriptor) {
481 TypeParameterDescriptor typeParameter = (TypeParameterDescriptor) descriptor;
482 if (typeParameter.getContainingDeclaration() == containingDeclaration) {
483 builder.setTypeParameterName(getSimpleNameIndex(typeParameter.getName()));
484 }
485 else {
486 builder.setTypeParameter(getTypeParameterId(typeParameter));
487 }
488
489 assert type.getArguments().isEmpty() : "Found arguments for type constructor build on type parameter: " + descriptor;
490 }
491
492 if (type.isMarkedNullable() != builder.getNullable()) {
493 builder.setNullable(type.isMarkedNullable());
494 }
495
496 extension.serializeType(type, builder);
497
498 return builder;
499 }
500
501 private void fillFromPossiblyInnerType(
502 @NotNull ProtoBuf.Type.Builder builder,
503 @NotNull PossiblyInnerType type
504 ) {
505 builder.setClassName(getClassId(type.getClassDescriptor()));
506
507 for (TypeProjection projection : type.getArguments()) {
508 builder.addArgument(typeArgument(projection));
509 }
510
511 if (type.getOuterType() != null) {
512 ProtoBuf.Type.Builder outerBuilder = ProtoBuf.Type.newBuilder();
513 fillFromPossiblyInnerType(outerBuilder, type.getOuterType());
514 if (useTypeTable()) {
515 builder.setOuterTypeId(typeTable.get(outerBuilder));
516 }
517 else {
518 builder.setOuterType(outerBuilder);
519 }
520
521 }
522 }
523
524 @NotNull
525 private ProtoBuf.Type.Argument.Builder typeArgument(@NotNull TypeProjection typeProjection) {
526 ProtoBuf.Type.Argument.Builder builder = ProtoBuf.Type.Argument.newBuilder();
527
528 if (typeProjection.isStarProjection()) {
529 builder.setProjection(ProtoBuf.Type.Argument.Projection.STAR);
530 }
531 else {
532 ProtoBuf.Type.Argument.Projection projection = projection(typeProjection.getProjectionKind());
533
534 if (projection != builder.getProjection()) {
535 builder.setProjection(projection);
536 }
537
538 if (useTypeTable()) {
539 builder.setTypeId(typeId(typeProjection.getType()));
540 }
541 else {
542 builder.setType(type(typeProjection.getType()));
543 }
544 }
545
546 return builder;
547 }
548
549 @NotNull
550 public ProtoBuf.Package.Builder packageProto(@NotNull Collection<PackageFragmentDescriptor> fragments) {
551 return packageProto(fragments, null);
552 }
553
554 @NotNull
555 public ProtoBuf.Package.Builder packageProto(
556 @NotNull Collection<PackageFragmentDescriptor> fragments,
557 @Nullable Function1<DeclarationDescriptor, Boolean> skip
558 ) {
559 ProtoBuf.Package.Builder builder = ProtoBuf.Package.newBuilder();
560
561 Collection<DeclarationDescriptor> members = new ArrayList<DeclarationDescriptor>();
562 for (PackageFragmentDescriptor fragment : fragments) {
563 members.addAll(DescriptorUtils.getAllDescriptors(fragment.getMemberScope()));
564 }
565
566 for (DeclarationDescriptor declaration : sort(members)) {
567 if (skip != null && skip.invoke(declaration)) continue;
568
569 if (declaration instanceof PropertyDescriptor) {
570 builder.addProperty(propertyProto((PropertyDescriptor) declaration));
571 }
572 else if (declaration instanceof FunctionDescriptor) {
573 builder.addFunction(functionProto((FunctionDescriptor) declaration));
574 }
575 }
576
577 ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
578 if (typeTableProto != null) {
579 builder.setTypeTable(typeTableProto);
580 }
581
582 extension.serializePackage(fragments, builder);
583
584 return builder;
585 }
586
587 @NotNull
588 public ProtoBuf.Package.Builder packagePartProto(@NotNull Collection<DeclarationDescriptor> members) {
589 ProtoBuf.Package.Builder builder = ProtoBuf.Package.newBuilder();
590
591 for (DeclarationDescriptor declaration : sort(members)) {
592 if (declaration instanceof PropertyDescriptor) {
593 builder.addProperty(propertyProto((PropertyDescriptor) declaration));
594 }
595 else if (declaration instanceof FunctionDescriptor) {
596 builder.addFunction(functionProto((FunctionDescriptor) declaration));
597 }
598 }
599
600 ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
601 if (typeTableProto != null) {
602 builder.setTypeTable(typeTableProto);
603 }
604
605 return builder;
606 }
607
608 @NotNull
609 private static ProtoBuf.Type.Argument.Projection projection(@NotNull Variance projectionKind) {
610 switch (projectionKind) {
611 case INVARIANT:
612 return ProtoBuf.Type.Argument.Projection.INV;
613 case IN_VARIANCE:
614 return ProtoBuf.Type.Argument.Projection.IN;
615 case OUT_VARIANCE:
616 return ProtoBuf.Type.Argument.Projection.OUT;
617 }
618 throw new IllegalStateException("Unknown projectionKind: " + projectionKind);
619 }
620
621 private int getClassId(@NotNull ClassDescriptor descriptor) {
622 return getStringTable().getFqNameIndex(descriptor);
623 }
624
625 private int getSimpleNameIndex(@NotNull Name name) {
626 return getStringTable().getStringIndex(name.asString());
627 }
628
629 private int getTypeParameterId(@NotNull TypeParameterDescriptor descriptor) {
630 return typeParameters.intern(descriptor);
631 }
632
633 private static boolean hasAnnotations(Annotated descriptor) {
634 return !descriptor.getAnnotations().isEmpty();
635 }
636
637 @NotNull
638 public static <T extends DeclarationDescriptor> List<T> sort(@NotNull Collection<T> descriptors) {
639 List<T> result = new ArrayList<T>(descriptors);
640 //NOTE: the exact comparator does matter here
641 Collections.sort(result, MemberComparator.INSTANCE);
642 return result;
643
644 }
645 }