From e942bff4a2875d3fdf70ff5055769605ef0f641b Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 7 Mar 2019 14:23:54 +0100 Subject: [PATCH] Reformat module 'serialization', fix inspections --- .../serialization/AnnotationSerializer.kt | 2 +- .../serialization/ContractSerializer.kt | 8 +- .../serialization/DescriptorSerializer.kt | 141 ++++++++---------- .../serialization/SerializerExtension.kt | 32 ++-- .../serialization/SerializerExtensionBase.kt | 32 ++-- .../kotlin/serialization/StringTableImpl.kt | 6 +- 6 files changed, 109 insertions(+), 112 deletions(-) diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt index 33f6ca7e1c8..d934556bb8d 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt @@ -61,7 +61,7 @@ class AnnotationSerializer(private val stringTable: DescriptorAwareStringTable) override fun visitBooleanValue(value: BooleanValue, data: Unit) { type = Type.BOOLEAN - setIntValue(if (value.value) 1 else 0) + intValue = if (value.value) 1 else 0 } override fun visitByteValue(value: ByteValue, data: Unit) { diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/ContractSerializer.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/ContractSerializer.kt index 5d497cd19a6..f4048a33846 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/ContractSerializer.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/ContractSerializer.kt @@ -63,10 +63,10 @@ class ContractSerializer { is ReturnsEffectDeclaration -> { when { - effectDeclaration.value == ConstantReference.NOT_NULL -> builder.effectType = - ProtoBuf.Effect.EffectType.RETURNS_NOT_NULL - effectDeclaration.value == ConstantReference.WILDCARD -> builder.effectType = - ProtoBuf.Effect.EffectType.RETURNS_CONSTANT + effectDeclaration.value == ConstantReference.NOT_NULL -> + builder.effectType = ProtoBuf.Effect.EffectType.RETURNS_NOT_NULL + effectDeclaration.value == ConstantReference.WILDCARD -> + builder.effectType = ProtoBuf.Effect.EffectType.RETURNS_CONSTANT else -> { builder.effectType = ProtoBuf.Effect.EffectType.RETURNS_CONSTANT builder.addEffectConstructorArgument(contractExpressionProto(effectDeclaration.value, contractDescription)) diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt index e3a3501a817..924623ab7ca 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt @@ -41,18 +41,20 @@ import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes import java.util.* class DescriptorSerializer private constructor( - private val containingDeclaration: DeclarationDescriptor?, - private val typeParameters: Interner, - private val extension: SerializerExtension, - val typeTable: MutableTypeTable, - private val versionRequirementTable: MutableVersionRequirementTable, - private val serializeTypeTableToFunction: Boolean + private val containingDeclaration: DeclarationDescriptor?, + private val typeParameters: Interner, + private val extension: SerializerExtension, + val typeTable: MutableTypeTable, + private val versionRequirementTable: MutableVersionRequirementTable, + private val serializeTypeTableToFunction: Boolean ) { private val contractSerializer = ContractSerializer() - fun createChildSerializer(descriptor: DeclarationDescriptor): DescriptorSerializer = - DescriptorSerializer(descriptor, Interner(typeParameters), extension, typeTable, versionRequirementTable, - serializeTypeTableToFunction = false) + private fun createChildSerializer(descriptor: DeclarationDescriptor): DescriptorSerializer = + DescriptorSerializer( + descriptor, Interner(typeParameters), extension, typeTable, versionRequirementTable, + serializeTypeTableToFunction = false + ) val stringTable: DescriptorAwareStringTable get() = extension.stringTable @@ -84,8 +86,7 @@ class DescriptorSerializer private constructor( for (supertype in classDescriptor.typeConstructor.supertypes) { if (useTypeTable()) { builder.addSupertypeId(typeId(supertype)) - } - else { + } else { builder.addSupertype(type(supertype)) } } @@ -98,10 +99,10 @@ class DescriptorSerializer private constructor( } val callableMembers = - extension.customClassMembersProducer?.getCallableMembers(classDescriptor) + extension.customClassMembersProducer?.getCallableMembers(classDescriptor) ?: sort( - DescriptorUtils.getAllDescriptors(classDescriptor.defaultType.memberScope) - .filterIsInstance() + DescriptorUtils.getAllDescriptors(classDescriptor.defaultType.memberScope) + .filterIsInstance() ) for (descriptor in callableMembers) { @@ -117,8 +118,7 @@ class DescriptorSerializer private constructor( for (descriptor in nestedClassifiers) { if (descriptor is TypeAliasDescriptor) { typeAliasProto(descriptor)?.let { builder.addTypeAlias(it) } - } - else { + } else { if (descriptor is ClassDescriptor && !extension.shouldSerializeNestedClass(descriptor)) { continue } @@ -126,8 +126,7 @@ class DescriptorSerializer private constructor( val name = getSimpleNameIndex(descriptor.name) if (isEnumEntry(descriptor)) { builder.addEnumEntry(enumEntryProto(descriptor as ClassDescriptor)) - } - else { + } else { builder.addNestedClassName(name) } } @@ -168,7 +167,7 @@ class DescriptorSerializer private constructor( if (!classDescriptor.isInline && !classDescriptor.hasInlineClassTypesInSignature()) return builder.addVersionRequirement( - DescriptorSerializer.writeLanguageVersionRequirement(LanguageFeature.InlineClasses, versionRequirementTable) + writeLanguageVersionRequirement(LanguageFeature.InlineClasses, versionRequirementTable) ) } @@ -246,8 +245,7 @@ class DescriptorSerializer private constructor( if (useTypeTable()) { builder.returnTypeId = local.typeId(descriptor.type) - } - else { + } else { builder.setReturnType(local.type(descriptor.type)) } @@ -259,8 +257,7 @@ class DescriptorSerializer private constructor( if (receiverParameter != null) { if (useTypeTable()) { builder.receiverTypeId = local.typeId(receiverParameter.type) - } - else { + } else { builder.setReceiverType(local.type(receiverParameter.type)) } } @@ -281,11 +278,11 @@ class DescriptorSerializer private constructor( } private fun normalizeVisibility(descriptor: DeclarationDescriptorWithVisibility) = - // It can be necessary for Java classes serialization having package-private visibility - if (extension.shouldUseNormalizedVisibility()) - descriptor.visibility.normalize() - else - descriptor.visibility + // It can be necessary for Java classes serialization having package-private visibility + if (extension.shouldUseNormalizedVisibility()) + descriptor.visibility.normalize() + else + descriptor.visibility fun functionProto(descriptor: FunctionDescriptor): ProtoBuf.Function.Builder? { if (!extension.shouldSerializeFunction(descriptor)) return null @@ -310,8 +307,7 @@ class DescriptorSerializer private constructor( if (useTypeTable()) { builder.returnTypeId = local.typeId(descriptor.returnType!!) - } - else { + } else { builder.setReturnType(local.type(descriptor.returnType!!)) } @@ -323,8 +319,7 @@ class DescriptorSerializer private constructor( if (receiverParameter != null) { if (useTypeTable()) { builder.receiverTypeId = local.typeId(receiverParameter.type) - } - else { + } else { builder.setReceiverType(local.type(receiverParameter.type)) } } @@ -357,7 +352,7 @@ class DescriptorSerializer private constructor( return builder } - fun constructorProto(descriptor: ConstructorDescriptor): ProtoBuf.Constructor.Builder { + private fun constructorProto(descriptor: ConstructorDescriptor): ProtoBuf.Constructor.Builder { val builder = ProtoBuf.Constructor.newBuilder() val local = createChildSerializer(descriptor) @@ -430,16 +425,14 @@ class DescriptorSerializer private constructor( val underlyingType = descriptor.underlyingType if (useTypeTable()) { builder.underlyingTypeId = local.typeId(underlyingType) - } - else { + } else { builder.setUnderlyingType(local.type(underlyingType)) } val expandedType = descriptor.expandedType if (useTypeTable()) { builder.expandedTypeId = local.typeId(expandedType) - } - else { + } else { builder.setExpandedType(local.type(expandedType)) } @@ -475,8 +468,7 @@ class DescriptorSerializer private constructor( if (useTypeTable()) { builder.typeId = typeId(descriptor.type) - } - else { + } else { builder.setType(type(descriptor.type)) } @@ -484,8 +476,7 @@ class DescriptorSerializer private constructor( if (varargElementType != null) { if (useTypeTable()) { builder.varargElementTypeId = typeId(varargElementType) - } - else { + } else { builder.setVarargElementType(type(varargElementType)) } } @@ -518,8 +509,7 @@ class DescriptorSerializer private constructor( for (upperBound in upperBounds) { if (useTypeTable()) { builder.addUpperBoundId(typeId(upperBound)) - } - else { + } else { builder.addUpperBound(type(upperBound)) } } @@ -545,8 +535,7 @@ class DescriptorSerializer private constructor( extension.serializeFlexibleType(flexibleType, lowerBound, upperBound) if (useTypeTable()) { lowerBound.flexibleUpperBoundId = typeTable[upperBound] - } - else { + } else { lowerBound.setFlexibleUpperBound(upperBound) } return lowerBound @@ -558,8 +547,7 @@ class DescriptorSerializer private constructor( return functionType } - val descriptor = type.constructor.declarationDescriptor - when (descriptor) { + when (val descriptor = type.constructor.declarationDescriptor) { is ClassDescriptor, is TypeAliasDescriptor -> { val possiblyInnerType = type.buildPossiblyInnerType() ?: error("possiblyInnerType should not be null: $type") fillFromPossiblyInnerType(builder, possiblyInnerType) @@ -567,8 +555,7 @@ class DescriptorSerializer private constructor( is TypeParameterDescriptor -> { if (descriptor.containingDeclaration === containingDeclaration) { builder.typeParameterName = getSimpleNameIndex(descriptor.name) - } - else { + } else { builder.typeParameter = getTypeParameterId(descriptor) } @@ -584,8 +571,7 @@ class DescriptorSerializer private constructor( if (abbreviation != null) { if (useTypeTable()) { builder.abbreviatedTypeId = typeId(abbreviation) - } - else { + } else { builder.setAbbreviatedType(type(abbreviation)) } } @@ -612,8 +598,7 @@ class DescriptorSerializer private constructor( fillFromPossiblyInnerType(outerBuilder, type.outerType!!) if (useTypeTable()) { builder.outerTypeId = typeTable[outerBuilder] - } - else { + } else { builder.setOuterType(outerBuilder) } } @@ -624,8 +609,7 @@ class DescriptorSerializer private constructor( if (typeProjection.isStarProjection) { builder.projection = ProtoBuf.Type.Argument.Projection.STAR - } - else { + } else { val projection = projection(typeProjection.projectionKind) if (projection != builder.projection) { @@ -634,8 +618,7 @@ class DescriptorSerializer private constructor( if (useTypeTable()) { builder.typeId = typeId(typeProjection.type) - } - else { + } else { builder.setType(type(typeProjection.type)) } } @@ -691,8 +674,8 @@ class DescriptorSerializer private constructor( val proto = ProtoBuf.VersionRequirement.newBuilder() VersionRequirement.Version(major, minor, patch).encode( - writeVersion = { proto.version = it }, - writeVersionFull = { proto.versionFull = it } + writeVersion = { proto.version = it }, + writeVersionFull = { proto.versionFull = it } ) val message = (args[RequireKotlinNames.MESSAGE] as? StringValue)?.value @@ -700,16 +683,18 @@ class DescriptorSerializer private constructor( proto.message = stringTable.getStringIndex(message) } - val level = (args[RequireKotlinNames.LEVEL] as? EnumValue)?.enumEntryName?.asString() - when (level) { - DeprecationLevel.ERROR.name -> { /* ERROR is the default level */ } + when ((args[RequireKotlinNames.LEVEL] as? EnumValue)?.enumEntryName?.asString()) { + DeprecationLevel.ERROR.name -> { + // ERROR is the default level + } DeprecationLevel.WARNING.name -> proto.level = ProtoBuf.VersionRequirement.Level.WARNING DeprecationLevel.HIDDEN.name -> proto.level = ProtoBuf.VersionRequirement.Level.HIDDEN } - val versionKind = (args[RequireKotlinNames.VERSION_KIND] as? EnumValue)?.enumEntryName?.asString() - when (versionKind) { - ProtoBuf.VersionRequirement.VersionKind.LANGUAGE_VERSION.name -> { /* LANGUAGE_VERSION is the default kind */ } + when ((args[RequireKotlinNames.VERSION_KIND] as? EnumValue)?.enumEntryName?.asString()) { + ProtoBuf.VersionRequirement.VersionKind.LANGUAGE_VERSION.name -> { + // LANGUAGE_VERSION is the default kind + } ProtoBuf.VersionRequirement.VersionKind.COMPILER_VERSION.name -> proto.versionKind = ProtoBuf.VersionRequirement.VersionKind.COMPILER_VERSION ProtoBuf.VersionRequirement.VersionKind.API_VERSION.name -> @@ -725,13 +710,13 @@ class DescriptorSerializer private constructor( } private fun getClassifierId(descriptor: ClassifierDescriptorWithTypeParameters): Int = - stringTable.getFqNameIndex(descriptor) + stringTable.getFqNameIndex(descriptor) private fun getSimpleNameIndex(name: Name): Int = - stringTable.getStringIndex(name.asString()) + stringTable.getStringIndex(name.asString()) private fun getTypeParameterId(descriptor: TypeParameterDescriptor): Int = - typeParameters.intern(descriptor) + typeParameters.intern(descriptor) private fun getAccessorFlags(accessor: PropertyAccessorDescriptor): Int = Flags.getAccessorFlags( hasAnnotations(accessor), @@ -745,14 +730,18 @@ class DescriptorSerializer private constructor( companion object { @JvmStatic fun createTopLevel(extension: SerializerExtension): DescriptorSerializer { - return DescriptorSerializer(null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), - serializeTypeTableToFunction = false) + return DescriptorSerializer( + null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), + serializeTypeTableToFunction = false + ) } @JvmStatic fun createForLambda(extension: SerializerExtension): DescriptorSerializer { - return DescriptorSerializer(null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), - serializeTypeTableToFunction = true) + return DescriptorSerializer( + null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), + serializeTypeTableToFunction = true + ) } @JvmStatic @@ -801,10 +790,10 @@ class DescriptorSerializer private constructor( descriptor != null && descriptor.nonSourceAnnotations.isNotEmpty() fun sort(descriptors: Collection): List = - ArrayList(descriptors).apply { - //NOTE: the exact comparator does matter here - Collections.sort(this, MemberComparator.INSTANCE) - } + ArrayList(descriptors).apply { + //NOTE: the exact comparator does matter here + Collections.sort(this, MemberComparator.INSTANCE) + } fun writeLanguageVersionRequirement( languageFeature: LanguageFeature, diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.kt index 31bec49f0dc..08e215e9927 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.kt @@ -36,31 +36,35 @@ abstract class SerializerExtension { open fun serializeClass( - descriptor: ClassDescriptor, - proto: ProtoBuf.Class.Builder, - versionRequirementTable: MutableVersionRequirementTable, - childSerializer: DescriptorSerializer + descriptor: ClassDescriptor, + proto: ProtoBuf.Class.Builder, + versionRequirementTable: MutableVersionRequirementTable, + childSerializer: DescriptorSerializer ) { } open fun serializePackage(packageFqName: FqName, proto: ProtoBuf.Package.Builder) { } - open fun serializeConstructor(descriptor: ConstructorDescriptor, - proto: ProtoBuf.Constructor.Builder, - childSerializer: DescriptorSerializer) { + open fun serializeConstructor( + descriptor: ConstructorDescriptor, + proto: ProtoBuf.Constructor.Builder, + childSerializer: DescriptorSerializer + ) { } - open fun serializeFunction(descriptor: FunctionDescriptor, - proto: ProtoBuf.Function.Builder, - childSerializer: DescriptorSerializer) { + open fun serializeFunction( + descriptor: FunctionDescriptor, + proto: ProtoBuf.Function.Builder, + childSerializer: DescriptorSerializer + ) { } open fun serializeProperty( - descriptor: PropertyDescriptor, - proto: ProtoBuf.Property.Builder, - versionRequirementTable: MutableVersionRequirementTable, - childSerializer: DescriptorSerializer + descriptor: PropertyDescriptor, + proto: ProtoBuf.Property.Builder, + versionRequirementTable: MutableVersionRequirementTable, + childSerializer: DescriptorSerializer ) { } diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtensionBase.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtensionBase.kt index 3bd57bd8d1b..45080882a9d 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtensionBase.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtensionBase.kt @@ -28,10 +28,10 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt override val stringTable = StringTableImpl() override fun serializeClass( - descriptor: ClassDescriptor, - proto: ProtoBuf.Class.Builder, - versionRequirementTable: MutableVersionRequirementTable, - childSerializer: DescriptorSerializer + descriptor: ClassDescriptor, + proto: ProtoBuf.Class.Builder, + versionRequirementTable: MutableVersionRequirementTable, + childSerializer: DescriptorSerializer ) { for (annotation in descriptor.nonSourceAnnotations) { proto.addExtension(protocol.classAnnotation, annotationSerializer.serializeAnnotation(annotation)) @@ -42,27 +42,31 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt proto.setExtension(protocol.packageFqName, stringTable.getPackageFqNameIndex(packageFqName)) } - override fun serializeConstructor(descriptor: ConstructorDescriptor, - proto: ProtoBuf.Constructor.Builder, - childSerializer: DescriptorSerializer) { + override fun serializeConstructor( + descriptor: ConstructorDescriptor, + proto: ProtoBuf.Constructor.Builder, + childSerializer: DescriptorSerializer + ) { for (annotation in descriptor.nonSourceAnnotations) { proto.addExtension(protocol.constructorAnnotation, annotationSerializer.serializeAnnotation(annotation)) } } - override fun serializeFunction(descriptor: FunctionDescriptor, - proto: ProtoBuf.Function.Builder, - childSerializer: DescriptorSerializer) { + override fun serializeFunction( + descriptor: FunctionDescriptor, + proto: ProtoBuf.Function.Builder, + childSerializer: DescriptorSerializer + ) { for (annotation in descriptor.nonSourceAnnotations) { proto.addExtension(protocol.functionAnnotation, annotationSerializer.serializeAnnotation(annotation)) } } override fun serializeProperty( - descriptor: PropertyDescriptor, - proto: ProtoBuf.Property.Builder, - versionRequirementTable: MutableVersionRequirementTable, - childSerializer: DescriptorSerializer + descriptor: PropertyDescriptor, + proto: ProtoBuf.Property.Builder, + versionRequirementTable: MutableVersionRequirementTable, + childSerializer: DescriptorSerializer ) { for (annotation in descriptor.nonSourceAnnotations) { proto.addExtension(protocol.propertyAnnotation, annotationSerializer.serializeAnnotation(annotation)) diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/StringTableImpl.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/StringTableImpl.kt index 1a1e13a30ce..486afbff2ee 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/StringTableImpl.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/StringTableImpl.kt @@ -37,8 +37,8 @@ open class StringTableImpl : DescriptorAwareStringTable { val otherFqName = other.fqName return fqName.parentQualifiedName == otherFqName.parentQualifiedName - && fqName.shortName == otherFqName.shortName - && fqName.kind == otherFqName.kind + && fqName.shortName == otherFqName.shortName + && fqName.kind == otherFqName.kind } } @@ -55,7 +55,7 @@ open class StringTableImpl : DescriptorAwareStringTable { builder.kind = QualifiedName.Kind.CLASS builder.parentQualifiedName = - classId.outerClassId?.let(this::getQualifiedClassNameIndex) + classId.outerClassId?.let(this::getQualifiedClassNameIndex) ?: getPackageFqNameIndex(classId.packageFqName) builder.shortName = getStringIndex(classId.shortClassName.asString())