Use SerializerFactory interface and generate function

for sealed and abstract serializable classes.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1116
Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1078
This commit is contained in:
Leonid Startsev
2021-04-20 20:19:11 +03:00
committed by Space
parent ebbef484ad
commit 66f39ca185
5 changed files with 16 additions and 51 deletions
@@ -26,7 +26,7 @@ abstract class SerializableCodegen(
} }
private inline fun ClassDescriptor.shouldHaveSpecificSyntheticMethods(functionPresenceChecker: () -> FunctionDescriptor?) = private inline fun ClassDescriptor.shouldHaveSpecificSyntheticMethods(functionPresenceChecker: () -> FunctionDescriptor?) =
!isInlineClass() && (isAbstractSerializableClass() || isSealedSerializableClass() || functionPresenceChecker() != null) !isInlineClass() && (isAbstractOrSealedSerializableClass() || functionPresenceChecker() != null)
private fun generateSyntheticInternalConstructor() { private fun generateSyntheticInternalConstructor() {
val serializerDescriptor = serializableDescriptor.classSerializer ?: return val serializerDescriptor = serializableDescriptor.classSerializer ?: return
@@ -1,17 +1,6 @@
/* /*
* Copyright 2010-2017 JetBrains s.r.o. * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.jetbrains.kotlinx.serialization.compiler.backend.common package org.jetbrains.kotlinx.serialization.compiler.backend.common
@@ -44,7 +33,7 @@ abstract class SerializableCompanionCodegen(
"probably clash with user-defined function has occurred" "probably clash with user-defined function has occurred"
) )
if (serializableDescriptor.isSerializableObject || serializableDescriptor.isSealedSerializableClass() || serializableDescriptor.isAbstractSerializableClass()) { if (serializableDescriptor.isSerializableObject || serializableDescriptor.isAbstractOrSealedSerializableClass()) {
generateLazySerializerGetter(serializerGetterDescriptor) generateLazySerializerGetter(serializerGetterDescriptor)
} else { } else {
generateSerializerGetter(serializerGetterDescriptor) generateSerializerGetter(serializerGetterDescriptor)
@@ -110,7 +110,7 @@ class SerializableIrGenerator(
if (useFieldMissingOptimization() && if (useFieldMissingOptimization() &&
// for abstract classes fields MUST BE checked in child classes // for abstract classes fields MUST BE checked in child classes
!serializableDescriptor.isAbstractSerializableClass() && !serializableDescriptor.isSealedSerializableClass() !serializableDescriptor.isAbstractOrSealedSerializableClass()
) { ) {
val getDescriptorExpr = if (serializableDescriptor.isStaticSerializable) { val getDescriptorExpr = if (serializableDescriptor.isStaticSerializable) {
getStaticSerialDescriptorExpr() getStaticSerialDescriptorExpr()
@@ -1,17 +1,6 @@
/* /*
* Copyright 2010-2021 JetBrains s.r.o. * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.jetbrains.kotlinx.serialization.compiler.backend.jvm package org.jetbrains.kotlinx.serialization.compiler.backend.jvm
@@ -285,7 +274,7 @@ class SerializableCodegenImpl(
} }
private fun InstructionAdapter.generateOptimizedGoldenMaskCheck(maskVar: Int) { private fun InstructionAdapter.generateOptimizedGoldenMaskCheck(maskVar: Int) {
if (serializableDescriptor.isAbstractSerializableClass() || serializableDescriptor.isSealedSerializableClass()) { if (serializableDescriptor.isAbstractOrSealedSerializableClass()) {
// for abstract classes fields MUST BE checked in child classes // for abstract classes fields MUST BE checked in child classes
return return
} }
@@ -1,17 +1,6 @@
/* /*
* Copyright 2010-2017 JetBrains s.r.o. * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.jetbrains.kotlinx.serialization.compiler.resolve package org.jetbrains.kotlinx.serialization.compiler.resolve
@@ -156,17 +145,14 @@ internal fun Annotated.findSerializableAnnotationDeclaration(): KtAnnotationEntr
// For abstract classes marked with @Serializable, // For abstract classes marked with @Serializable,
// methods are generated anyway although they shouldn't have // methods are generated anyway although they shouldn't have
// generated $serializer and use Polymorphic one. // generated $serializer and use Polymorphic one.
internal fun ClassDescriptor.isAbstractSerializableClass(): Boolean = internal fun ClassDescriptor.isAbstractOrSealedSerializableClass(): Boolean =
isInternalSerializable && modality == Modality.ABSTRACT isInternalSerializable && (modality == Modality.ABSTRACT || modality == Modality.SEALED)
internal fun ClassDescriptor.isSealedSerializableClass(): Boolean =
isInternalSerializable && modality == Modality.SEALED
internal fun ClassDescriptor.polymorphicSerializerIfApplicableAutomatically(): ClassDescriptor? { internal fun ClassDescriptor.polymorphicSerializerIfApplicableAutomatically(): ClassDescriptor? {
val serializer = when { val serializer = when {
this.isAbstractSerializableClass() kind == ClassKind.INTERFACE -> SpecialBuiltins.polymorphicSerializer
|| kind == ClassKind.INTERFACE -> SpecialBuiltins.polymorphicSerializer isInternalSerializable && modality == Modality.ABSTRACT -> SpecialBuiltins.polymorphicSerializer
this.isSealedSerializableClass() -> SpecialBuiltins.sealedSerializer isInternalSerializable && modality == Modality.SEALED -> SpecialBuiltins.sealedSerializer
else -> null else -> null
} }
return serializer?.let { module.getClassFromSerializationPackage(it) } return serializer?.let { module.getClassFromSerializationPackage(it) }
@@ -217,6 +203,7 @@ internal fun ClassDescriptor.needSerializerFactory(): Boolean {
if (!(this.platform?.isNative() == true || this.platform.isJs())) return false if (!(this.platform?.isNative() == true || this.platform.isJs())) return false
val serializableClass = getSerializableClassDescriptorByCompanion(this) ?: return false val serializableClass = getSerializableClassDescriptorByCompanion(this) ?: return false
if (serializableClass.isSerializableObject) return true if (serializableClass.isSerializableObject) return true
if (serializableClass.isAbstractOrSealedSerializableClass()) return true
if (serializableClass.declaredTypeParameters.isEmpty()) return false if (serializableClass.declaredTypeParameters.isEmpty()) return false
return true return true
} }