Parcelable: Cast types deserialized with Parcel.readValue() (KT-19747)
This commit is contained in:
committed by
Yan Zhulanow
parent
eee28d8507
commit
f8ca714c45
+1
-1
@@ -201,7 +201,7 @@ interface ParcelSerializer {
|
||||
if (strict && !type.annotations.hasAnnotation(RAWVALUE_ANNOTATION_FQNAME))
|
||||
throw IllegalArgumentException("Illegal type")
|
||||
else
|
||||
GenericParcelSerializer
|
||||
GenericParcelSerializer(asmType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -25,9 +25,7 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
|
||||
|
||||
internal val PARCEL_TYPE = Type.getObjectType("android/os/Parcel")
|
||||
|
||||
internal object GenericParcelSerializer : ParcelSerializer {
|
||||
override val asmType: Type = Type.getObjectType("java/lang/Object")
|
||||
|
||||
internal class GenericParcelSerializer(override val asmType: Type) : ParcelSerializer {
|
||||
override fun writeValue(v: InstructionAdapter) {
|
||||
v.invokevirtual(PARCEL_TYPE.internalName, "writeValue", "(Ljava/lang/Object;)V", false)
|
||||
}
|
||||
@@ -36,6 +34,7 @@ internal object GenericParcelSerializer : ParcelSerializer {
|
||||
v.aconst(asmType) // -> parcel, type
|
||||
v.invokevirtual("java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false) // -> parcel, classloader
|
||||
v.invokevirtual(PARCEL_TYPE.internalName, "readValue", "(Ljava/lang/ClassLoader;)Ljava/lang/Object;", false)
|
||||
v.castIfNeeded(asmType)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.android.parcel.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import java.io.Serializable
|
||||
|
||||
class JHelp(var j1: String) {
|
||||
val j2 = 9
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
class J(val j: @RawValue JHelp) : Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = J(JHelp("A"))
|
||||
|
||||
var exceptionCaught = false
|
||||
try {
|
||||
test.writeToParcel(parcel, 0)
|
||||
} catch (e: RuntimeException) {
|
||||
if (e.message!!.contains("Parcel: unable to marshal value test.JHelp")) {
|
||||
exceptionCaught = true
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
if (!exceptionCaught) {
|
||||
error("Exception should be thrown")
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.android.parcel.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import java.io.Serializable
|
||||
|
||||
interface IJHelp {
|
||||
val j1: String
|
||||
}
|
||||
|
||||
class JHelp(override var j1: String): IJHelp, Serializable {
|
||||
val j2 = 9
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
class J(val j: @RawValue JHelp) : Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = J(JHelp("A"))
|
||||
test.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
|
||||
val test2 = readFromParcel<J>(parcel)
|
||||
|
||||
assert(test.j.j1 == test2.j.j1)
|
||||
}
|
||||
@@ -47,4 +47,6 @@ class ParcelBoxTest : AbstractParcelBoxTest() {
|
||||
fun testObjects() = doTest("objects")
|
||||
fun testNestedParcelable() = doTest("nestedParcelable")
|
||||
fun testKt19749() = doTest("kt19749")
|
||||
fun testKt19747() = doTest("kt19747")
|
||||
fun testKt19747_2() = doTest("kt19747_2")
|
||||
}
|
||||
Reference in New Issue
Block a user