From 19f50960ca39ea942fa89d8a0ae8a924c9bb6ea2 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sat, 1 Nov 2014 09:26:53 +0300 Subject: [PATCH] Code improvements after conversion to Kotlin --- .../DeserializedClassDescriptor.kt | 214 +++++------------- 1 file changed, 55 insertions(+), 159 deletions(-) diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt index e4a043309e5..99fbc67e5de 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.kt @@ -26,88 +26,51 @@ import org.jetbrains.jet.lang.descriptors.impl.AbstractClassDescriptor import org.jetbrains.jet.lang.descriptors.impl.EnumEntrySyntheticClassDescriptor import org.jetbrains.jet.lang.resolve.DescriptorFactory import org.jetbrains.jet.lang.resolve.OverridingUtil -import org.jetbrains.jet.lang.resolve.name.ClassId import org.jetbrains.jet.lang.resolve.name.Name -import org.jetbrains.jet.lang.resolve.scopes.JetScope import org.jetbrains.jet.lang.resolve.scopes.StaticScopeForKotlinClass import org.jetbrains.jet.lang.types.AbstractClassTypeConstructor import org.jetbrains.jet.lang.types.ErrorUtils import org.jetbrains.jet.lang.types.JetType -import org.jetbrains.jet.lang.types.TypeConstructor import org.jetbrains.jet.storage.MemoizedFunctionToNullable -import org.jetbrains.jet.storage.NotNullLazyValue -import org.jetbrains.jet.storage.NullableLazyValue import java.util.* import org.jetbrains.jet.descriptors.serialization import org.jetbrains.jet.lang.resolve.name.SpecialNames.getClassObjectName import org.jetbrains.jet.descriptors.serialization.classKind +import org.jetbrains.jet.utils.addIfNotNull +public fun DeserializedClassDescriptor(globalContext: DeserializationGlobalContext, classData: ClassData): DeserializedClassDescriptor + = DeserializedClassDescriptor(globalContext.withNameResolver(classData.getNameResolver()), classData.getClassProto()) -public fun DeserializedClassDescriptor(globalContext: DeserializationGlobalContext, classData: ClassData): DeserializedClassDescriptor { - return DeserializedClassDescriptor(globalContext.withNameResolver(classData.getNameResolver()), classData.getClassProto()) -} +public class DeserializedClassDescriptor(outerContext: DeserializationContext, private val classProto: ProtoBuf.Class) + : AbstractClassDescriptor(outerContext.storageManager, outerContext.nameResolver.getClassId(classProto.getFqName()).getRelativeClassName().shortName()), ClassDescriptor { -public class DeserializedClassDescriptor(outerContext: DeserializationContext, private val classProto: ProtoBuf.Class) : AbstractClassDescriptor(outerContext.storageManager, outerContext.nameResolver.getClassId(classProto.getFqName()).getRelativeClassName().shortName()), ClassDescriptor { + private val modality = serialization.modality(Flags.MODALITY.get(classProto.getFlags())) + private val visibility = serialization.visibility(Flags.VISIBILITY.get(classProto.getFlags())) + private val kind = classKind(Flags.CLASS_KIND.get(classProto.getFlags())) + private val isInner = Flags.INNER.get(classProto.getFlags()) - private val classId: ClassId - private val memberScope: DeserializedMemberScope + private val classId = outerContext.nameResolver.getClassId(classProto.getFqName()) + private val typeParameters = ArrayList(classProto.getTypeParameterCount()) + private val context = outerContext.withTypes(this).childContext(this, classProto.getTypeParameterList(), typeParameters) - private val primaryConstructor: NullableLazyValue - - private val annotations: NotNullLazyValue - - private val classObjectDescriptor: NullableLazyValue - private val nestedClasses: NestedClassDescriptors private val staticScope = StaticScopeForKotlinClass(this) + private val typeConstructor = DeserializedClassTypeConstructor(typeParameters) + private val memberScope = DeserializedClassMemberScope() + private val nestedClasses = NestedClassDescriptors() - private val containingDeclaration: NotNullLazyValue - private val typeConstructor: DeserializedClassTypeConstructor - private val modality: Modality - private val visibility: Visibility - private val kind: ClassKind - private val isInner: Boolean - private val context: DeserializationContextWithTypes + private val containingDeclaration = outerContext.storageManager.createLazyValue { computeContainingDeclaration() } + private val annotations = context.storageManager.createLazyValue { computeAnnotations() } + private val primaryConstructor = context.storageManager.createNullableLazyValue { computePrimaryConstructor() } + private val classObjectDescriptor = context.storageManager.createNullableLazyValue { computeClassObjectDescriptor() } - { - this.classId = outerContext.nameResolver.getClassId(classProto.getFqName()) - - val typeParameters = ArrayList(classProto.getTypeParameterCount()) - this.context = outerContext.withTypes(this).childContext(this, classProto.getTypeParameterList(), typeParameters) - - this.containingDeclaration = outerContext.storageManager.createLazyValue(object : Function0 { - override fun invoke(): DeclarationDescriptor { - return computeContainingDeclaration() - } - }) - - this.typeConstructor = DeserializedClassTypeConstructor(typeParameters) - this.memberScope = DeserializedClassMemberScope() - - val flags = classProto.getFlags() - this.modality = serialization.modality(Flags.MODALITY.get(flags)) - this.visibility = serialization.visibility(Flags.VISIBILITY.get(flags)) - this.kind = classKind(Flags.CLASS_KIND.get(flags)) - this.isInner = Flags.INNER.get(flags) - - this.annotations = context.storageManager.createLazyValue { computeAnnotations() } - - this.primaryConstructor = context.storageManager.createNullableLazyValue { computePrimaryConstructor() } - - this.classObjectDescriptor = context.storageManager.createNullableLazyValue { computeClassObjectDescriptor() } - - this.nestedClasses = NestedClassDescriptors() - } - - override fun getContainingDeclaration(): DeclarationDescriptor { - return containingDeclaration.invoke() - } + override fun getContainingDeclaration(): DeclarationDescriptor = containingDeclaration() private fun computeContainingDeclaration(): DeclarationDescriptor { if (classId.isTopLevelClass()) { val fragments = context.packageFragmentProvider.getPackageFragments(classId.getPackageFqName()) - assert(fragments.size() == 1) { "there should be exactly one package: " + fragments + ", class id is " + classId } + assert(fragments.size() == 1) { "there should be exactly one package: $fragments, class id is $classId" } return fragments.iterator().next() } else { @@ -115,26 +78,15 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p } } - override fun getTypeConstructor(): TypeConstructor { - return typeConstructor - } + override fun getTypeConstructor() = typeConstructor + override fun getKind() = kind - override fun getKind(): ClassKind { - return kind - } + override fun getModality() = modality - override fun getModality(): Modality { - return modality - } + override fun getVisibility() = visibility - override fun getVisibility(): Visibility { - return visibility - } - - override fun isInner(): Boolean { - return isInner - } + override fun isInner() = isInner private fun computeAnnotations(): Annotations { if (!Flags.HAS_ANNOTATIONS.get(classProto.getFlags())) { @@ -143,17 +95,11 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p return context.annotationLoader.loadClassAnnotations(this, classProto) } - override fun getAnnotations(): Annotations { - return annotations.invoke() - } + override fun getAnnotations(): Annotations = annotations() - override fun getScopeForMemberLookup(): JetScope { - return memberScope - } + override fun getScopeForMemberLookup() = memberScope - override fun getStaticScope(): JetScope { - return staticScope - } + override fun getStaticScope() = staticScope private fun computePrimaryConstructor(): ConstructorDescriptor? { if (!classProto.hasPrimaryConstructor()) return null @@ -168,28 +114,21 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p return context.deserializer.loadCallable(constructorProto.getData()) as ConstructorDescriptor } - override fun getUnsubstitutedPrimaryConstructor(): ConstructorDescriptor? { - return primaryConstructor.invoke() - } + override fun getUnsubstitutedPrimaryConstructor(): ConstructorDescriptor? = primaryConstructor() override fun getConstructors(): Collection { - val constructor = getUnsubstitutedPrimaryConstructor() - if (constructor == null) { - return listOf() - } + val constructor = getUnsubstitutedPrimaryConstructor() ?: return listOf() // TODO: other constructors return listOf(constructor) } private fun computeClassObjectDescriptor(): ClassDescriptor? { - if (!classProto.hasClassObject()) { - return null - } + if (!classProto.hasClassObject()) return null if (getKind() == ClassKind.OBJECT) { val classObjectProto = classProto.getClassObject() if (!classObjectProto.hasData()) { - throw IllegalStateException("Object should have a serialized class object: " + classId) + throw IllegalStateException("Object should have a serialized class object: $classId") } return DeserializedClassDescriptor(context, classObjectProto.getData()) @@ -198,9 +137,7 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p return context.deserializeClass(classId.createNestedClassId(getClassObjectName(getName()))) } - override fun getClassObjectDescriptor(): ClassDescriptor? { - return classObjectDescriptor.invoke() - } + override fun getClassObjectDescriptor(): ClassDescriptor? = classObjectDescriptor() private fun computeSuperTypes(): Collection { val supertypes = ArrayList(classProto.getSupertypeCount()) @@ -210,21 +147,14 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p return supertypes } - override fun toString(): String { - // not using descriptor render to preserve laziness - return "deserialized class " + getName().toString() - } + override fun toString() = "deserialized class ${getName().toString()}" // not using descriptor render to preserve laziness - override fun getSource(): SourceElement { - return SourceElement.NO_SOURCE - } + override fun getSource() = SourceElement.NO_SOURCE private inner class DeserializedClassTypeConstructor(private val parameters: List) : AbstractClassTypeConstructor() { private val supertypes = computeSuperTypes() - override fun getParameters(): List { - return parameters - } + override fun getParameters() = parameters override fun getSupertypes(): Collection { // We cannot have error supertypes because subclasses inherit error functions from them @@ -237,33 +167,19 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p return supertypes } - override fun isFinal(): Boolean { - return !getModality().isOverridable() - } + override fun isFinal() = !getModality().isOverridable() - override fun isDenotable(): Boolean { - return true - } + override fun isDenotable() = true - override fun getDeclarationDescriptor(): ClassifierDescriptor? { - return this@DeserializedClassDescriptor - } + override fun getDeclarationDescriptor() = this@DeserializedClassDescriptor - override fun getAnnotations(): Annotations { - return Annotations.EMPTY // TODO - } + override fun getAnnotations(): Annotations = Annotations.EMPTY // TODO - override fun toString(): String { - return getName().toString() - } + override fun toString() = getName().toString() } private inner class DeserializedClassMemberScope : DeserializedMemberScope(context, this@DeserializedClassDescriptor.classProto.getMemberList()) { - private val classDescriptor: DeserializedClassDescriptor - - { - this.classDescriptor = this@DeserializedClassDescriptor - } + private val classDescriptor: DeserializedClassDescriptor = this@DeserializedClassDescriptor override fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection) { val fromSupertypes = ArrayList() @@ -273,13 +189,13 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p generateFakeOverrides(name, fromSupertypes, functions) } - override fun computeNonDeclaredProperties(name: Name, property: MutableCollection) { + override fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection) { val fromSupertypes = ArrayList() for (supertype in classDescriptor.getTypeConstructor().getSupertypes()) { [suppress("UNCHECKED_CAST")] fromSupertypes.addAll(supertype.getMemberScope().getProperties(name) as Collection) } - generateFakeOverrides(name, fromSupertypes, property) + generateFakeOverrides(name, fromSupertypes, descriptors) } private fun generateFakeOverrides(name: Name, fromSupertypes: Collection, result: MutableCollection) { @@ -312,13 +228,9 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p } } - override fun getImplicitReceiver(): ReceiverParameterDescriptor? { - return classDescriptor.getThisAsReceiverParameter() - } + override fun getImplicitReceiver() = classDescriptor.getThisAsReceiverParameter() - override fun getClassDescriptor(name: Name): ClassifierDescriptor? { - return classDescriptor.nestedClasses.findClass.invoke(name) - } + override fun getClassDescriptor(name: Name): ClassifierDescriptor? = classDescriptor.nestedClasses.findClass(name) override fun addAllClassDescriptors(result: MutableCollection) { result.addAll(classDescriptor.nestedClasses.getAllDescriptors()) @@ -326,22 +238,14 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p } private inner class NestedClassDescriptors { - private val nestedClassNames: Set - val findClass: MemoizedFunctionToNullable - private val enumEntryNames: Set - - { - this.nestedClassNames = nestedClassNames() - this.enumEntryNames = enumEntryNames() + private val nestedClassNames = nestedClassNames() + private val enumEntryNames = enumEntryNames() + val findClass: MemoizedFunctionToNullable = run { val storageManager = context.storageManager - val enumMemberNames = storageManager.createLazyValue>(object : Function0> { - override fun invoke(): Collection { - return computeEnumMemberNames() - } - }) + val enumMemberNames = storageManager.createLazyValue { computeEnumMemberNames() } - this.findClass = storageManager.createMemoizedFunctionWithNullableValues { name -> + storageManager.createMemoizedFunctionWithNullableValues { name -> if (enumEntryNames.contains(name)) { EnumEntrySyntheticClassDescriptor.create(storageManager, this@DeserializedClassDescriptor, name, enumMemberNames, SourceElement.NO_SOURCE) } @@ -364,9 +268,7 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p } private fun enumEntryNames(): Set { - if (getKind() != ClassKind.ENUM_CLASS) { - return setOf() - } + if (getKind() != ClassKind.ENUM_CLASS) return setOf() val result = HashSet() val nameResolver = context.nameResolver @@ -394,16 +296,10 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p public fun getAllDescriptors(): Collection { val result = ArrayList(nestedClassNames.size() + enumEntryNames.size()) for (name in nestedClassNames) { - val descriptor = findClass.invoke(name) - if (descriptor != null) { - result.add(descriptor) - } + result.addIfNotNull(findClass(name)) } for (name in enumEntryNames) { - val descriptor = findClass.invoke(name) - if (descriptor != null) { - result.add(descriptor) - } + result.addIfNotNull(findClass(name)) } return result }