Parcelize: Skip constructor checks when used with custom Parceler
See https://issuetracker.google.com/177850558
This commit is contained in:
committed by
Alexander Udalov
parent
58c687e42d
commit
16a2aec296
+35
@@ -0,0 +1,35 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.parcelize.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
@Parcelize
|
||||
class A(_a: String) : Parcelable {
|
||||
var a: String = _a
|
||||
private set
|
||||
|
||||
companion object : Parceler<A> {
|
||||
override fun A.write(parcel: Parcel, flags: Int) {
|
||||
parcel.writeString(a)
|
||||
}
|
||||
|
||||
override fun create(parcel: Parcel) = A(parcel.readString())
|
||||
}
|
||||
}
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = A("OK")
|
||||
test.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val test2 = parcelableCreator<A>().createFromParcel(parcel)
|
||||
|
||||
assert(test.a == test2.a)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.parcelize.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
class T(val value: String)
|
||||
|
||||
// There's no warnings on empty constructors, secondary constructors, or
|
||||
// non-parcelable types if there is a custom parceler.
|
||||
@Parcelize
|
||||
class A() : Parcelable {
|
||||
var a: T = T("Fail")
|
||||
|
||||
constructor(value: String) : this() {
|
||||
a = T(value)
|
||||
}
|
||||
|
||||
companion object : Parceler<A> {
|
||||
override fun A.write(parcel: Parcel, flags: Int) {
|
||||
parcel.writeString(a.value)
|
||||
}
|
||||
|
||||
override fun create(parcel: Parcel) = A(parcel.readString())
|
||||
}
|
||||
}
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = A("OK")
|
||||
test.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val test2 = parcelableCreator<A>().createFromParcel(parcel)
|
||||
|
||||
assert(test.a.value == test2.a.value)
|
||||
}
|
||||
Reference in New Issue
Block a user