Parcelize: Generate non-final 'writeToParcel()' and 'describeContents()' (#KT-24720)
This commit is contained in:
+5
-2
@@ -245,7 +245,10 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
|||||||
val containerAsmType = codegen.typeMapper.mapType(parcelableClass)
|
val containerAsmType = codegen.typeMapper.mapType(parcelableClass)
|
||||||
val creatorAsmType = codegen.typeMapper.mapType(creatorClass)
|
val creatorAsmType = codegen.typeMapper.mapType(creatorClass)
|
||||||
|
|
||||||
createMethod(creatorClass, CREATE_FROM_PARCEL, parcelableClass.builtIns.anyType, "in" to parcelClassType).write(codegen) {
|
createMethod(
|
||||||
|
creatorClass, CREATE_FROM_PARCEL, Modality.FINAL,
|
||||||
|
parcelableClass.builtIns.anyType, "in" to parcelClassType
|
||||||
|
).write(codegen) {
|
||||||
if (parcelerObject != null) {
|
if (parcelerObject != null) {
|
||||||
val (companionAsmType, companionFieldName) = getCompanionClassType(containerAsmType, parcelerObject)
|
val (companionAsmType, companionFieldName) = getCompanionClassType(containerAsmType, parcelerObject)
|
||||||
|
|
||||||
@@ -361,7 +364,7 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
|||||||
val builtIns = parcelableClass.builtIns
|
val builtIns = parcelableClass.builtIns
|
||||||
val parcelableAsmType = codegen.typeMapper.mapType(parcelableClass)
|
val parcelableAsmType = codegen.typeMapper.mapType(parcelableClass)
|
||||||
|
|
||||||
createMethod(creatorClass, NEW_ARRAY,
|
createMethod(creatorClass, NEW_ARRAY, Modality.FINAL,
|
||||||
builtIns.getArrayType(Variance.INVARIANT, builtIns.anyType),
|
builtIns.getArrayType(Variance.INVARIANT, builtIns.anyType),
|
||||||
"size" to builtIns.intType
|
"size" to builtIns.intType
|
||||||
).write(codegen) {
|
).write(codegen) {
|
||||||
|
|||||||
+5
-3
@@ -47,6 +47,7 @@ open class ParcelableResolveExtension : SyntheticResolveExtension {
|
|||||||
fun createMethod(
|
fun createMethod(
|
||||||
classDescriptor: ClassDescriptor,
|
classDescriptor: ClassDescriptor,
|
||||||
componentKind: ParcelableSyntheticComponent.ComponentKind,
|
componentKind: ParcelableSyntheticComponent.ComponentKind,
|
||||||
|
modality: Modality,
|
||||||
returnType: KotlinType,
|
returnType: KotlinType,
|
||||||
vararg parameters: Pair<String, KotlinType>
|
vararg parameters: Pair<String, KotlinType>
|
||||||
): SimpleFunctionDescriptor {
|
): SimpleFunctionDescriptor {
|
||||||
@@ -65,7 +66,7 @@ open class ParcelableResolveExtension : SyntheticResolveExtension {
|
|||||||
|
|
||||||
functionDescriptor.initialize(
|
functionDescriptor.initialize(
|
||||||
null, classDescriptor.thisAsReceiverParameter, emptyList(), valueParameters,
|
null, classDescriptor.thisAsReceiverParameter, emptyList(), valueParameters,
|
||||||
returnType, Modality.FINAL, Visibilities.PUBLIC)
|
returnType, modality, Visibilities.PUBLIC)
|
||||||
|
|
||||||
return functionDescriptor
|
return functionDescriptor
|
||||||
}
|
}
|
||||||
@@ -98,7 +99,7 @@ open class ParcelableResolveExtension : SyntheticResolveExtension {
|
|||||||
&& result.none { it.isDescribeContents() }
|
&& result.none { it.isDescribeContents() }
|
||||||
&& fromSupertypes.none { it.isDescribeContents() }
|
&& fromSupertypes.none { it.isDescribeContents() }
|
||||||
) {
|
) {
|
||||||
result += createMethod(clazz, DESCRIBE_CONTENTS, clazz.builtIns.intType)
|
result += createMethod(clazz, DESCRIBE_CONTENTS, Modality.OPEN, clazz.builtIns.intType)
|
||||||
} else if (name.asString() == WRITE_TO_PARCEL.methodName
|
} else if (name.asString() == WRITE_TO_PARCEL.methodName
|
||||||
&& clazz.isParcelize
|
&& clazz.isParcelize
|
||||||
&& isExperimental()
|
&& isExperimental()
|
||||||
@@ -106,7 +107,8 @@ open class ParcelableResolveExtension : SyntheticResolveExtension {
|
|||||||
) {
|
) {
|
||||||
val builtIns = clazz.builtIns
|
val builtIns = clazz.builtIns
|
||||||
val parcelClassType = resolveParcelClassType(clazz.module) ?: ErrorUtils.createErrorType("Unresolved 'Parcel' type")
|
val parcelClassType = resolveParcelClassType(clazz.module) ?: ErrorUtils.createErrorType("Unresolved 'Parcel' type")
|
||||||
result += createMethod(clazz, WRITE_TO_PARCEL, builtIns.unitType, "parcel" to parcelClassType, "flags" to builtIns.intType)
|
result += createMethod(clazz, WRITE_TO_PARCEL, Modality.OPEN,
|
||||||
|
builtIns.unitType, "parcel" to parcelClassType, "flags" to builtIns.intType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
@@ -22,7 +22,10 @@ import org.jetbrains.kotlin.descriptors.ClassKind
|
|||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension
|
import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension
|
||||||
|
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
|
@RunWith(JUnit3RunnerWithInners::class)
|
||||||
class ParcelBoxTest : AbstractParcelBoxTest() {
|
class ParcelBoxTest : AbstractParcelBoxTest() {
|
||||||
fun testSimple() = doTest("simple")
|
fun testSimple() = doTest("simple")
|
||||||
fun testPrimitiveTypes() = doTest("primitiveTypes")
|
fun testPrimitiveTypes() = doTest("primitiveTypes")
|
||||||
@@ -56,6 +59,7 @@ class ParcelBoxTest : AbstractParcelBoxTest() {
|
|||||||
fun testKt20717() = doTest("kt20717")
|
fun testKt20717() = doTest("kt20717")
|
||||||
fun testEnumObject() = doTest("enumObject")
|
fun testEnumObject() = doTest("enumObject")
|
||||||
fun testIntArray() = doTest("intArray")
|
fun testIntArray() = doTest("intArray")
|
||||||
|
fun testOpenParcelize() = doTest("openParcelize")
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParcelBoxTestWithSerializableLikeExtension : AbstractParcelBoxTest() {
|
class ParcelBoxTestWithSerializableLikeExtension : AbstractParcelBoxTest() {
|
||||||
|
|||||||
Vendored
+19
@@ -0,0 +1,19 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
@file:JvmName("TestKt")
|
||||||
|
package test
|
||||||
|
|
||||||
|
import kotlinx.android.parcel.*
|
||||||
|
import android.os.Parcel
|
||||||
|
import android.os.Parcelable
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
open class Base(val a: String) : Parcelable
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
class Inh(var b: Int) : Base(""), Parcelable
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
Inh(0)
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user