From a4d4d15ca996905fcc7e5c012e0b41677b9470d0 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 21 Nov 2014 04:56:38 +0300 Subject: [PATCH] Improve code after conversion, fix some warnings --- .../serialization/MemberDeserializer.kt | 52 +++++++------------ .../serialization/TypeDeserializer.java | 15 +++--- .../serialization/context/context.kt | 11 ++-- .../DeserializedClassDescriptor.kt | 2 +- 4 files changed, 30 insertions(+), 50 deletions(-) diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt index ee777103f85..5cfe3d20392 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/MemberDeserializer.kt @@ -27,7 +27,6 @@ import org.jetbrains.jet.lang.resolve.DescriptorFactory import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable.CallableKind.* import org.jetbrains.jet.descriptors.serialization.ProtoBuf.TypeParameter -import java.util.ArrayList public class MemberDeserializer(private val context: DeserializationContext) { private val components: DeserializationComponents get() = context.components @@ -62,7 +61,7 @@ public class MemberDeserializer(private val context: DeserializationContext) { local.typeDeserializer.type(proto.getReturnType()), local.typeDeserializer.getOwnTypeParameters(), getDispatchReceiverParameter(), - local.typeDeserializer.typeOrNull(if (proto.hasReceiverType()) proto.getReceiverType() else null) + if (proto.hasReceiverType()) local.typeDeserializer.type(proto.getReceiverType()) else null ) val getter = if (Flags.HAS_GETTER.get(flags)) { @@ -104,7 +103,6 @@ public class MemberDeserializer(private val context: DeserializationContext) { ) val setterLocal = local.childContext(setter, listOf()) val valueParameters = setterLocal.memberDeserializer.valueParameters(proto, AnnotatedCallableKind.PROPERTY_SETTER) - assert(valueParameters.size() == 1) { "Property setter should have a single value parameter: $setter" } setter.initialize(valueParameters.single()) setter } @@ -119,12 +117,8 @@ public class MemberDeserializer(private val context: DeserializationContext) { if (Flags.HAS_CONSTANT.get(flags)) { property.setCompileTimeInitializer( components.storageManager.createNullableLazyValue { - val containingDeclaration = - context.containingDeclaration as? ClassOrPackageFragmentDescriptor - ?: error("Only members in classes or package fragments should be serialized: ${context.containingDeclaration}") - components.constantLoader.loadPropertyConstant( - containingDeclaration, proto, context.nameResolver, AnnotatedCallableKind.PROPERTY - ) + val container = context.containingDeclaration.asClassOrPackage() + components.constantLoader.loadPropertyConstant(container, proto, context.nameResolver, AnnotatedCallableKind.PROPERTY) } ) } @@ -139,7 +133,7 @@ public class MemberDeserializer(private val context: DeserializationContext) { val function = DeserializedSimpleFunctionDescriptor.create(context.containingDeclaration, proto, context.nameResolver, annotations) val local = context.childContext(function, proto.getTypeParameterList()) function.initialize( - local.typeDeserializer.typeOrNull(if (proto.hasReceiverType()) proto.getReceiverType() else null), + if (proto.hasReceiverType()) local.typeDeserializer.type(proto.getReceiverType()) else null, getDispatchReceiverParameter(), local.typeDeserializer.getOwnTypeParameters(), local.memberDeserializer.valueParameters(proto, AnnotatedCallableKind.FUNCTION), @@ -171,48 +165,38 @@ public class MemberDeserializer(private val context: DeserializationContext) { } private fun getAnnotations(proto: Callable, flags: Int, kind: AnnotatedCallableKind): Annotations { - val containingDeclaration = - context.containingDeclaration as? ClassOrPackageFragmentDescriptor - ?: error("Only members in classes or package fragments should be serialized: ${context.containingDeclaration}") return if (Flags.HAS_ANNOTATIONS.get(flags)) - components.annotationLoader.loadCallableAnnotations(containingDeclaration, proto, context.nameResolver, kind) + components.annotationLoader.loadCallableAnnotations( + context.containingDeclaration.asClassOrPackage(), proto, context.nameResolver, kind + ) else Annotations.EMPTY } public fun typeParameters(protos: List): List { - val result = ArrayList(protos.size()) - for (i in protos.indices) { - val proto = protos.get(i) - result.add(DeserializedTypeParameterDescriptor( + return protos.withIndices().map { val (i, proto) = it + DeserializedTypeParameterDescriptor( components.storageManager, context.typeDeserializer, proto, context.containingDeclaration, context.nameResolver.getName(proto.getName()), variance(proto.getVariance()), proto.getReified(), i - )) + ) } - return result } private fun valueParameters(callable: Callable, kind: AnnotatedCallableKind): List { - val classOrPackage = - context.containingDeclaration.getContainingDeclaration() as? ClassOrPackageFragmentDescriptor - ?: error("Only members in classes or package fragments should be serialized: ${context.containingDeclaration}") + val containerOfCallable = context.containingDeclaration.getContainingDeclaration().asClassOrPackage() - val protos = callable.getValueParameterList() - val result = ArrayList(protos.size()) - for (i in protos.indices) { - val proto = protos.get(i) - result.add(ValueParameterDescriptorImpl( + return callable.getValueParameterList().withIndices().map { val (i, proto) = it + ValueParameterDescriptorImpl( context.containingDeclaration, null, i, - getParameterAnnotations(classOrPackage, callable, kind, proto), + getParameterAnnotations(containerOfCallable, callable, kind, proto), context.nameResolver.getName(proto.getName()), context.typeDeserializer.type(proto.getType()), Flags.DECLARES_DEFAULT_VALUE.get(proto.getFlags()), - context.typeDeserializer.typeOrNull(if (proto.hasVarargElementType()) proto.getVarargElementType() else null), + if (proto.hasVarargElementType()) context.typeDeserializer.type(proto.getVarargElementType()) else null, SourceElement.NO_SOURCE - )) + ) } - return result } private fun getParameterAnnotations( @@ -226,4 +210,8 @@ public class MemberDeserializer(private val context: DeserializationContext) { else Annotations.EMPTY } + + private fun DeclarationDescriptor.asClassOrPackage(): ClassOrPackageFragmentDescriptor = + this as? ClassOrPackageFragmentDescriptor + ?: error("Only members in classes or package fragments should be serialized: $this") } diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java index 7ed040f78c9..ab077cbe34a 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java @@ -76,14 +76,6 @@ public class TypeDeserializer { return UtilsPackage.toReadOnlyList(typeParameterDescriptors.values()); } - @Nullable - public JetType typeOrNull(@Nullable ProtoBuf.Type proto) { - if (proto == null) { - return null; - } - return type(proto); - } - @NotNull public JetType type(@NotNull ProtoBuf.Type proto) { if (proto.hasFlexibleTypeCapabilitiesId()) { @@ -169,7 +161,12 @@ public class TypeDeserializer { TypeParameterDescriptor typeParameterDescriptor = (TypeParameterDescriptor) descriptor; return typeParameterDescriptor.getDefaultType().getMemberScope(); } - return ((ClassDescriptor) descriptor).getMemberScope(typeArguments); + else if (descriptor instanceof ClassDescriptor) { + return ((ClassDescriptor) descriptor).getMemberScope(typeArguments); + } + else { + throw new IllegalStateException("Unsupported classifier: " + descriptor); + } } @Override diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt index 55d54985247..d46e5f4c900 100644 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/context/context.kt @@ -17,10 +17,9 @@ package org.jetbrains.jet.descriptors.serialization.context import org.jetbrains.jet.storage.StorageManager -import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader import org.jetbrains.jet.lang.descriptors.* import org.jetbrains.jet.descriptors.serialization.* -import org.jetbrains.jet.descriptors.serialization.ProtoBuf.TypeParameter +import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader import org.jetbrains.jet.lang.resolve.name.ClassId @@ -53,14 +52,10 @@ public class DeserializationContext( val memberDeserializer: MemberDeserializer = MemberDeserializer(this) - // Not using default arguments here because Java code calls this function - fun childContext(descriptor: DeclarationDescriptor, typeParameterProtos: List): DeserializationContext = - childContext(descriptor, typeParameterProtos, this.nameResolver) - fun childContext( descriptor: DeclarationDescriptor, - typeParameterProtos: List, - nameResolver: NameResolver + typeParameterProtos: List, + nameResolver: NameResolver = this.nameResolver ): DeserializationContext { val child = DeserializationContext(components, nameResolver, descriptor, parentTypeDeserializer = this.typeDeserializer) 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 e2f7263eb16..2c1ee57c3f7 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 @@ -133,7 +133,7 @@ public class DeserializedClassDescriptor( private fun computeSuperTypes(): Collection { val supertypes = ArrayList(classProto.getSupertypeCount()) for (supertype in classProto.getSupertypeList()) { - supertypes.add(context.typeDeserializer.`type`(supertype)) + supertypes.add(context.typeDeserializer.type(supertype)) } return supertypes }