Revert back visibility in deserialization ctor to public so inheritors can access it from other modules.

Fix instantiating of polymorphic serializer with generics.
Move context creation below as asked.
This commit is contained in:
Leonid Startsev
2019-04-30 12:55:00 +03:00
parent 3610c99163
commit 0ecf20bd62
3 changed files with 26 additions and 20 deletions
@@ -127,12 +127,12 @@ public class ConstructorCodegen {
) {
if (!canHaveDeclaredConstructors(descriptor)) return;
ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor, typeMapper);
KtSecondaryConstructor constructor = (KtSecondaryConstructor) descriptorToDeclaration(constructorDescriptor);
// Synthetic constructors don't have corresponding declarations
if (constructor == null) return;
ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor, typeMapper);
functionCodegen.generateMethod(
JvmDeclarationOriginKt.OtherOrigin(constructor, constructorDescriptor),
constructorDescriptor, constructorContext,
@@ -263,23 +263,9 @@ internal fun AbstractSerialGenerator.stackValueSerializerInstance(codegen: Class
dup()
// instantiate all arg serializers on stack
val signature = StringBuilder("(")
when (serializer.classId) {
enumSerializerId, contextSerializerId, polymorphicSerializerId -> {
// a special way to instantiate enum -- need a enum KClass reference
// GENERIC_ARGUMENT forces boxing in order to obtain KClass
aconst(codegen.typeMapper.mapType(kType, null, TypeMappingMode.GENERIC_ARGUMENT))
AsmUtil.wrapJavaClassIntoKClass(this)
signature.append(AsmTypes.K_CLASS_TYPE.descriptor)
}
referenceArraySerializerId -> {
// a special way to instantiate reference array serializer -- need an element KClass reference
aconst(codegen.typeMapper.mapType(kType.arguments[0].type, null, TypeMappingMode.GENERIC_ARGUMENT))
AsmUtil.wrapJavaClassIntoKClass(this)
signature.append(AsmTypes.K_CLASS_TYPE.descriptor)
}
}
// all serializers get arguments with serializers of their generic types
argSerializers.forEach { (argType, argSerializer) ->
fun instantiate(typeArgument: Pair<KotlinType, ClassDescriptor?>) {
val (argType, argSerializer) = typeArgument
assert(
stackValueSerializerInstance(
codegen,
@@ -295,6 +281,26 @@ internal fun AbstractSerialGenerator.stackValueSerializerInstance(codegen: Class
if (argType.isMarkedNullable) wrapStackValueIntoNullableSerializer()
signature.append(kSerializerType.descriptor)
}
when (serializer.classId) {
enumSerializerId, contextSerializerId, polymorphicSerializerId -> {
// a special way to instantiate enum -- need a enum KClass reference
// GENERIC_ARGUMENT forces boxing in order to obtain KClass
aconst(codegen.typeMapper.mapType(kType, null, TypeMappingMode.GENERIC_ARGUMENT))
AsmUtil.wrapJavaClassIntoKClass(this)
signature.append(AsmTypes.K_CLASS_TYPE.descriptor)
}
referenceArraySerializerId -> {
// a special way to instantiate reference array serializer -- need an element KClass reference
aconst(codegen.typeMapper.mapType(kType.arguments[0].type, null, TypeMappingMode.GENERIC_ARGUMENT))
AsmUtil.wrapJavaClassIntoKClass(this)
signature.append(AsmTypes.K_CLASS_TYPE.descriptor)
// Reference array serializer still needs serializer for its argument type
instantiate(argSerializers[0])
}
// all serializers get arguments with serializers of their generic types
else -> argSerializers.forEach(::instantiate)
}
signature.append(")V")
// invoke constructor
invokespecial(serializerType.internalName, "<init>", signature.toString(), false)
@@ -323,7 +323,7 @@ object KSerializerDescriptorResolver {
functionDescriptor.initialize(
consParams,
Visibilities.INTERNAL
Visibilities.PUBLIC
)
functionDescriptor.returnType = classDescriptor.defaultType