Parcelize: Resolve parcel serializers on demand
This commit is contained in:
committed by
Yan Zhulanow
parent
cfe168021a
commit
8d364a8a1a
+6
-3
@@ -283,7 +283,9 @@ class ParcelableIrTransformer(private val context: IrPluginContext, private val
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class ParcelableProperty(val field: IrField, val parceler: IrParcelSerializer)
|
private class ParcelableProperty(val field: IrField, parcelerThunk: () -> IrParcelSerializer) {
|
||||||
|
val parceler by lazy(parcelerThunk)
|
||||||
|
}
|
||||||
|
|
||||||
private val IrClass.classParceler: IrParcelSerializer
|
private val IrClass.classParceler: IrParcelSerializer
|
||||||
get() = if (kind == ClassKind.CLASS) {
|
get() = if (kind == ClassKind.CLASS) {
|
||||||
@@ -302,8 +304,9 @@ class ParcelableIrTransformer(private val context: IrPluginContext, private val
|
|||||||
return constructor.valueParameters.map { parameter ->
|
return constructor.valueParameters.map { parameter ->
|
||||||
val property = properties.first { it.name == parameter.name }
|
val property = properties.first { it.name == parameter.name }
|
||||||
val localScope = property.getParcelerScope(toplevelScope)
|
val localScope = property.getParcelerScope(toplevelScope)
|
||||||
val parceler = serializerFactory.get(parameter.type, parcelizeType = defaultType, strict = true, scope = localScope)
|
ParcelableProperty(property.backingField!!) {
|
||||||
ParcelableProperty(property.backingField!!, parceler)
|
serializerFactory.get(parameter.type, parcelizeType = defaultType, strict = true, scope = localScope)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
@@ -64,6 +64,11 @@ public class ParcelBoxTestGenerated extends AbstractParcelBoxTest {
|
|||||||
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/charSequence.kt");
|
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/charSequence.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("customParcelable.kt")
|
||||||
|
public void testCustomParcelable() throws Exception {
|
||||||
|
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/customParcelable.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("customParcelerScoping.kt")
|
@TestMetadata("customParcelerScoping.kt")
|
||||||
public void testCustomParcelerScoping() throws Exception {
|
public void testCustomParcelerScoping() throws Exception {
|
||||||
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/customParcelerScoping.kt");
|
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/customParcelerScoping.kt");
|
||||||
|
|||||||
+5
@@ -64,6 +64,11 @@ public class ParcelIrBoxTestGenerated extends AbstractParcelIrBoxTest {
|
|||||||
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/charSequence.kt");
|
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/charSequence.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("customParcelable.kt")
|
||||||
|
public void testCustomParcelable() throws Exception {
|
||||||
|
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/customParcelable.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("customParcelerScoping.kt")
|
@TestMetadata("customParcelerScoping.kt")
|
||||||
public void testCustomParcelerScoping() throws Exception {
|
public void testCustomParcelerScoping() throws Exception {
|
||||||
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/customParcelerScoping.kt");
|
runTest("plugins/android-extensions/android-extensions-compiler/testData/parcel/box/customParcelerScoping.kt");
|
||||||
|
|||||||
Vendored
+37
@@ -0,0 +1,37 @@
|
|||||||
|
// IGNORE_BACKEND: JVM
|
||||||
|
// See KT-38105
|
||||||
|
// Throws IllegalAccessError, since the code tries to access the private companion field directly from the generated User$Creator class.
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
@file:JvmName("TestKt")
|
||||||
|
package test
|
||||||
|
|
||||||
|
import kotlinx.android.parcel.*
|
||||||
|
import android.os.Parcel
|
||||||
|
import android.os.Parcelable
|
||||||
|
|
||||||
|
data class User(val name: String, val age: Int)
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
data class UserParcelable(val user: User) : Parcelable {
|
||||||
|
private companion object : Parceler<UserParcelable> {
|
||||||
|
override fun UserParcelable.write(parcel: Parcel, flags: Int) {
|
||||||
|
parcel.writeString(user.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun create(parcel: Parcel) = UserParcelable(User(parcel.readString(), 0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box() = parcelTest { parcel ->
|
||||||
|
val userParcelable = UserParcelable(User("John", 20))
|
||||||
|
userParcelable.writeToParcel(parcel, 0)
|
||||||
|
|
||||||
|
val bytes = parcel.marshall()
|
||||||
|
parcel.unmarshall(bytes, 0, bytes.size)
|
||||||
|
|
||||||
|
val userParcelable2 = readFromParcel<UserParcelable>(parcel)
|
||||||
|
|
||||||
|
assert(userParcelable.user.name == userParcelable2.user.name)
|
||||||
|
assert(userParcelable2.user.age == 0)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user