Code improvements after conversion to Kotlin
This commit is contained in:
+55
-159
@@ -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<TypeParameterDescriptor>(classProto.getTypeParameterCount())
|
||||
private val context = outerContext.withTypes(this).childContext(this, classProto.getTypeParameterList(), typeParameters)
|
||||
|
||||
private val primaryConstructor: NullableLazyValue<ConstructorDescriptor>
|
||||
|
||||
private val annotations: NotNullLazyValue<Annotations>
|
||||
|
||||
private val classObjectDescriptor: NullableLazyValue<ClassDescriptor>
|
||||
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<DeclarationDescriptor>
|
||||
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<TypeParameterDescriptor>(classProto.getTypeParameterCount())
|
||||
this.context = outerContext.withTypes(this).childContext(this, classProto.getTypeParameterList(), typeParameters)
|
||||
|
||||
this.containingDeclaration = outerContext.storageManager.createLazyValue<DeclarationDescriptor>(object : Function0<DeclarationDescriptor> {
|
||||
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<Annotations> { computeAnnotations() }
|
||||
|
||||
this.primaryConstructor = context.storageManager.createNullableLazyValue<ConstructorDescriptor> { computePrimaryConstructor() }
|
||||
|
||||
this.classObjectDescriptor = context.storageManager.createNullableLazyValue<ClassDescriptor> { 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<ConstructorDescriptor> {
|
||||
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<JetType> {
|
||||
val supertypes = ArrayList<JetType>(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<TypeParameterDescriptor>) : AbstractClassTypeConstructor() {
|
||||
private val supertypes = computeSuperTypes()
|
||||
|
||||
override fun getParameters(): List<TypeParameterDescriptor> {
|
||||
return parameters
|
||||
}
|
||||
override fun getParameters() = parameters
|
||||
|
||||
override fun getSupertypes(): Collection<JetType> {
|
||||
// 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<FunctionDescriptor>) {
|
||||
val fromSupertypes = ArrayList<FunctionDescriptor>()
|
||||
@@ -273,13 +189,13 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p
|
||||
generateFakeOverrides(name, fromSupertypes, functions)
|
||||
}
|
||||
|
||||
override fun computeNonDeclaredProperties(name: Name, property: MutableCollection<PropertyDescriptor>) {
|
||||
override fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection<PropertyDescriptor>) {
|
||||
val fromSupertypes = ArrayList<PropertyDescriptor>()
|
||||
for (supertype in classDescriptor.getTypeConstructor().getSupertypes()) {
|
||||
[suppress("UNCHECKED_CAST")]
|
||||
fromSupertypes.addAll(supertype.getMemberScope().getProperties(name) as Collection<PropertyDescriptor>)
|
||||
}
|
||||
generateFakeOverrides(name, fromSupertypes, property)
|
||||
generateFakeOverrides(name, fromSupertypes, descriptors)
|
||||
}
|
||||
|
||||
private fun <D : CallableMemberDescriptor> generateFakeOverrides(name: Name, fromSupertypes: Collection<D>, result: MutableCollection<D>) {
|
||||
@@ -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<DeclarationDescriptor>) {
|
||||
result.addAll(classDescriptor.nestedClasses.getAllDescriptors())
|
||||
@@ -326,22 +238,14 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p
|
||||
}
|
||||
|
||||
private inner class NestedClassDescriptors {
|
||||
private val nestedClassNames: Set<Name>
|
||||
val findClass: MemoizedFunctionToNullable<Name, ClassDescriptor>
|
||||
private val enumEntryNames: Set<Name>
|
||||
|
||||
{
|
||||
this.nestedClassNames = nestedClassNames()
|
||||
this.enumEntryNames = enumEntryNames()
|
||||
private val nestedClassNames = nestedClassNames()
|
||||
private val enumEntryNames = enumEntryNames()
|
||||
|
||||
val findClass: MemoizedFunctionToNullable<Name, ClassDescriptor> = run {
|
||||
val storageManager = context.storageManager
|
||||
val enumMemberNames = storageManager.createLazyValue<Collection<Name>>(object : Function0<Collection<Name>> {
|
||||
override fun invoke(): Collection<Name> {
|
||||
return computeEnumMemberNames()
|
||||
}
|
||||
})
|
||||
val enumMemberNames = storageManager.createLazyValue { computeEnumMemberNames() }
|
||||
|
||||
this.findClass = storageManager.createMemoizedFunctionWithNullableValues<Name, ClassDescriptor> { name ->
|
||||
storageManager.createMemoizedFunctionWithNullableValues<Name, ClassDescriptor> { 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<Name> {
|
||||
if (getKind() != ClassKind.ENUM_CLASS) {
|
||||
return setOf()
|
||||
}
|
||||
if (getKind() != ClassKind.ENUM_CLASS) return setOf()
|
||||
|
||||
val result = HashSet<Name>()
|
||||
val nameResolver = context.nameResolver
|
||||
@@ -394,16 +296,10 @@ public class DeserializedClassDescriptor(outerContext: DeserializationContext, p
|
||||
public fun getAllDescriptors(): Collection<ClassDescriptor> {
|
||||
val result = ArrayList<ClassDescriptor>(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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user