Parcelize: Add the Parcelize compiler plugin with sources extracted from Android Extensions plugin (KT-40030)

This commit is contained in:
Yan Zhulanow
2020-07-07 19:30:23 +09:00
parent c9bca165bc
commit b7796d63d8
152 changed files with 11872 additions and 0 deletions
@@ -0,0 +1,61 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class PrimitiveTypes(
val boo: Boolean, val c: Char, val byt: Byte, val s: Short,
val i: Int, val f: Float, val l: Long, val d: Double,
val nboo: Boolean?, val nc: Char?, val nbyt: Byte?, val ns: Short?,
val ni: Int?, val nf: Float?, val nl: Long?, val nd: Double?,
val jboo: java.lang.Boolean, val jc: java.lang.Character, val jbyt: java.lang.Byte, val js: java.lang.Short,
val ji: java.lang.Integer, val jf: java.lang.Float, val jl: java.lang.Long, val jd: java.lang.Double,
val njboo: java.lang.Boolean?, val njc: java.lang.Character?, val njbyt: java.lang.Byte?, val njs: java.lang.Short?,
val nji: java.lang.Integer?, val njf: java.lang.Float?, val njl: java.lang.Long?, val njd: java.lang.Double?
) : Parcelable
fun box() = parcelTest { parcel ->
val first = PrimitiveTypes(
true, '#', 3.toByte(), 10.toShort(), -300, -5.0f, Long.MAX_VALUE, 3.14,
true, '#', 3.toByte(), 10.toShort(), -300, -5.0f, Long.MAX_VALUE, 3.14,
true as java.lang.Boolean, '#' as java.lang.Character,
3.toByte() as java.lang.Byte, 10.toShort() as java.lang.Short,
-300 as java.lang.Integer, -5.0f as java.lang.Float,
10L as java.lang.Long, 3.14 as java.lang.Double,
true as java.lang.Boolean, '#' as java.lang.Character,
3.toByte() as java.lang.Byte, 10.toShort() as java.lang.Short,
-300 as java.lang.Integer, -5.0f as java.lang.Float,
10L as java.lang.Long, 3.14 as java.lang.Double
)
val second = PrimitiveTypes(
false, '\n', Byte.MIN_VALUE, Short.MIN_VALUE,
Int.MIN_VALUE, Float.POSITIVE_INFINITY, Long.MAX_VALUE, Double.NEGATIVE_INFINITY,
null, null, null, null, null, null, null, null,
false as java.lang.Boolean, '\n' as java.lang.Character,
Byte.MIN_VALUE as java.lang.Byte, Short.MIN_VALUE as java.lang.Short,
Int.MIN_VALUE as java.lang.Integer, Float.POSITIVE_INFINITY as java.lang.Float,
java.lang.Long(Long.MAX_VALUE), java.lang.Double(Double.NEGATIVE_INFINITY),
null, null, null, null, null, null, null, null
)
first.writeToParcel(parcel, 0)
second.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<PrimitiveTypes>(parcel)
val second2 = readFromParcel<PrimitiveTypes>(parcel)
assert(first == first2)
assert(second == second2)
}
@@ -0,0 +1,39 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@Parcelize
data class Test(val a: Array<String>) : Parcelable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as Test
if (!Arrays.equals(a, other.a)) return false
return true
}
override fun hashCode() = Arrays.hashCode(a)
}
fun box() = parcelTest { parcel ->
val first = Test(arrayOf("A", "B"))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,77 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@Parcelize
data class Test(
val a: Array<String>,
val b: Array<String?>,
val c: IntArray,
val d: CharArray?,
val e: Array<IntArray>,
val f: Array<List<String>>,
val g: List<Array<String>>,
val h: Array<String>?
) : Parcelable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as Test
if (!Arrays.equals(a, other.a)) return false
if (!Arrays.equals(b, other.b)) return false
if (!Arrays.equals(c, other.c)) return false
if (!Arrays.equals(d, other.d)) return false
if (!Arrays.deepEquals(e, other.e)) return false
if (!Arrays.equals(f, other.f)) return false
if (g.size != other.g.size) return false
if (!g.zip(other.g).all { (f, s) -> Arrays.equals(f, s) }) return false
if (!Arrays.equals(h, other.h)) return false
return true
}
override fun hashCode(): Int {
var result = Arrays.hashCode(a)
result = 31 * result + Arrays.hashCode(b)
result = 31 * result + Arrays.hashCode(c)
result = 31 * result + (d?.let { Arrays.hashCode(it) } ?: 0)
result = 31 * result + Arrays.hashCode(e)
result = 31 * result + Arrays.hashCode(f)
result = 31 * result + g.hashCode()
result = 31 * result + (h?.let { Arrays.hashCode(it) } ?: 0)
return result
}
}
fun box() = parcelTest { parcel ->
val first = Test(
a = arrayOf("A", "B", "C"),
b = arrayOf("A", null, "B"),
c = intArrayOf(1, 2, 3),
d = null,
e = arrayOf(intArrayOf(2, 4, 1), intArrayOf(10, 20)),
f = arrayOf(listOf("A"), listOf("B", "C")),
g = listOf(arrayOf("Z", "X"), arrayOf()),
h = arrayOf("")
)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,41 @@
// IGNORE_BACKEND: JVM
// See KT-38103
// There is no such thing as a readStrongInterface method to deserialize arbitrary IIinterface implementations
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Binder
import android.os.IBinder
import android.os.IInterface
import android.os.Parcel
import android.os.Parcelable
import java.io.Serializable
class MockBinder : Binder(), Serializable
@Parcelize
class MockIInterface : IInterface, Parcelable {
override fun asBinder(): IBinder = MockBinder()
}
@Parcelize
class ServiceContainer(
val binder: MockBinder,
val iinterface: MockIInterface,
val binderArray: Array<IBinder>,
val binderList: List<IBinder>
) : Parcelable
fun box() = parcelTest { parcel ->
val test = ServiceContainer(MockBinder(), MockIInterface(), arrayOf(MockBinder()), listOf(MockBinder()))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<ServiceContainer>(parcel)
}
@@ -0,0 +1,42 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class BoxedTypes(
val boo: java.lang.Boolean,
val c: java.lang.Character,
val byt: java.lang.Byte,
val s: java.lang.Short,
val i: java.lang.Integer,
val f: java.lang.Float,
val l: java.lang.Long,
val d: java.lang.Double
) : Parcelable
fun box() = parcelTest { parcel ->
val first = BoxedTypes(
true as java.lang.Boolean,
'#' as java.lang.Character,
3.toByte() as java.lang.Byte,
10.toShort() as java.lang.Short,
-300 as java.lang.Integer,
-5.0f as java.lang.Float,
Long.MAX_VALUE as java.lang.Long,
3.14 as java.lang.Double)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<BoxedTypes>(parcel)
assert(first == first2)
}
@@ -0,0 +1,35 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.os.Bundle
@Parcelize
data class User(val a: Bundle) : Parcelable
fun box() = parcelTest { parcel ->
val test = User(Bundle().apply { putChar("A", 'c'); putByte("B", 40.toByte()); putString("C", "ABC") })
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<User>(parcel)
assert(compareBundles(test.a, test2.a))
}
private fun compareBundles(first: Bundle, second: Bundle): Boolean {
if (first.size() != second.size()) return false
for (key in first.keySet()) {
if (first.get(key) != second.get(key)) return false
}
return true
}
@@ -0,0 +1,26 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.text.SpannableString
@Parcelize
data class Test(val simple: CharSequence, val spanned: CharSequence) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test("John", SpannableString("Smith"))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
assert(test.simple.toString() == test2.simple.toString())
assert(test.spanned.toString() == test2.spanned.toString())
}
@@ -0,0 +1,40 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class User(val firstName: String, val secondName: String, val age: Int) : Parcelable {
companion object : Parceler<User> {
override fun User.write(parcel: Parcel, flags: Int) {
parcel.writeString(firstName)
parcel.writeString(secondName)
}
override fun create(parcel: Parcel) = User(parcel.readString(), parcel.readString(), 0)
override fun newArray(size: Int) = arrayOfNulls<User>(size) as Array<User>
}
}
fun box() = parcelTest { parcel ->
val user = User("John", "Smith", 20)
val user2 = User("Joe", "Bloggs", 30)
val array = arrayOf(user, user2)
parcel.writeTypedArray(array, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val creator = User::class.java.getDeclaredField("CREATOR").get(null) as Parcelable.Creator<User>
val result = parcel.createTypedArray(creator)
assert(result.size == 2)
assert(result[0].firstName == user.firstName)
assert(result[1].firstName == user2.firstName)
}
@@ -0,0 +1,38 @@
// 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.parcelize.*
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)
parcel.setDataPosition(0)
val userParcelable2 = readFromParcel<UserParcelable>(parcel)
assert(userParcelable.user.name == userParcelable2.user.name)
assert(userParcelable2.user.age == 0)
}
@@ -0,0 +1,44 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.util.SparseBooleanArray
object Parceler1 : Parceler<Int> {
override fun create(parcel: Parcel) = -parcel.readInt()
override fun Int.write(parcel: Parcel, flags: Int) {
parcel.writeInt(this)
}
}
object Parceler2 : Parceler<Int> {
override fun create(parcel: Parcel) = parcel.readString().length
override fun Int.write(parcel: Parcel, flags: Int) {
parcel.writeString("Abc")
}
}
@Parcelize
@TypeParceler<Int, Parceler1>
data class Ints(val a: Int, @TypeParceler<Int, Parceler2> val b: Int, val c: @WriteWith<Parceler2> Int) : Parcelable
fun box() = parcelTest { parcel ->
val test = Ints(1, 1, 1)
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Ints>(parcel)
assert(test2.a == -test.a)
assert(test2.b == -test.b)
assert(test2.c == 3)
}
@@ -0,0 +1,56 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
object Parceler1 : Parceler<Int> {
override fun create(parcel: Parcel) = -parcel.readInt()
override fun Int.write(parcel: Parcel, flags: Int) {
parcel.writeInt(this)
}
}
object Parceler2 : Parceler<Long> {
override fun create(parcel: Parcel) = parcel.readString().length.toLong()
override fun Long.write(parcel: Parcel, flags: Int) {
parcel.writeString("Abc")
}
}
@Parcelize
data class Test(
val a: Int,
@TypeParceler<Int, Parceler1> val b: Int,
@TypeParceler<Long, Parceler2> val c: Long,
@TypeParceler<Int, Parceler1> val d: List<Int>,
@TypeParceler<Long, Parceler2> val e: LongArray
) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test(5, 5, 50L, listOf(1, 2, 3), longArrayOf(3, 2, 1))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
println(test.toString())
println(test2.toString())
with (test) {
assert(a == 5 && b == 5 && c == 50L && d == listOf(1, 2, 3) && Arrays.equals(e, longArrayOf(3, 2, 1)))
}
with (test2) {
assert(a == 5 && b == -5 && c == 3L && d == listOf(-1, -2, -3) && Arrays.equals(e, longArrayOf(3, 3, 3)))
}
}
@@ -0,0 +1,49 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
object Parceler1 : Parceler<String> {
override fun create(parcel: Parcel) = parcel.readInt().toString()
override fun String.write(parcel: Parcel, flags: Int) {
parcel.writeInt(length)
}
}
typealias Parceler2 = Parceler1
object Parceler3 : Parceler<String> {
override fun create(parcel: Parcel) = parcel.readString().toUpperCase()
override fun String.write(parcel: Parcel, flags: Int) {
parcel.writeString(this)
}
}
@Parcelize
@TypeParceler<String, Parceler2>
data class Test(
val a: String,
@TypeParceler<String, Parceler1> val b: String,
@TypeParceler<String, Parceler3> val c: CharSequence,
val d: @WriteWith<Parceler3> String
) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test("Abc", "Abc", "Abc", "Abc")
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
assert(test.a == "Abc" && test.b == "Abc" && test.c == "Abc" && test.d == "Abc")
assert(test2.a == "3" && test2.b == "3" && test2.c == "Abc" && test2.d == "ABC")
}
@@ -0,0 +1,55 @@
// IGNORE_BACKEND: JVM
// See KT-38107
// The JVM backend is missing support for custom parcelers in List<String>
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
object Parceler1 : Parceler<String> {
override fun create(parcel: Parcel) = parcel.readInt().toString()
override fun String.write(parcel: Parcel, flags: Int) {
parcel.writeInt(length)
}
}
object Parceler2 : Parceler<List<String>> {
override fun create(parcel: Parcel) = listOf(parcel.readString())
override fun List<String>.write(parcel: Parcel, flags: Int) {
parcel.writeString(this.joinToString(","))
}
}
@Parcelize
data class Test(
val a: String,
val b: @WriteWith<Parceler1> String,
val c: List<@WriteWith<Parceler1> String>,
val d: @WriteWith<Parceler2> List<String>,
val e: @WriteWith<Parceler2> List<@WriteWith<Parceler1> String>
) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test("Abc", "Abc", listOf("A", "bc"), listOf("A", "bc"), listOf("A", "bc"))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
with (test) {
assert(a == "Abc" && b == "Abc" && c == listOf("A", "bc") && d == listOf("A", "bc") && e == listOf("A", "bc"))
}
with (test2) {
assert(a == "Abc" && b == "3" && c == listOf("1", "2") && d == listOf("A,bc") && e == listOf("A,bc"))
}
}
@@ -0,0 +1,38 @@
// 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.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class User(val firstName: String, val secondName: String, val age: Int) : Parcelable {
private companion object : Parceler<User> {
override fun User.write(parcel: Parcel, flags: Int) {
parcel.writeString(firstName)
parcel.writeString(secondName)
}
override fun create(parcel: Parcel) = User(parcel.readString(), parcel.readString(), 0)
}
}
fun box() = parcelTest { parcel ->
val user = User("John", "Smith", 20)
user.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val user2 = readFromParcel<User>(parcel)
assert(user.firstName == user2.firstName)
assert(user.secondName == user2.secondName)
assert(user2.age == 0)
}
@@ -0,0 +1,38 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
enum class Color : Parcelable { BLACK, WHITE }
@Parcelize
object Obj : Parcelable
fun box() = parcelTest { parcel ->
val black = Color.BLACK
val obj = Obj
black.writeToParcel(parcel, 0)
obj.writeToParcel(parcel, 0)
println(black)
println(obj)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val black2 = readFromParcel<Color>(parcel)
val obj2 = readFromParcel<Obj>(parcel)
println(black2)
println(obj2)
assert(black2 == black)
assert(obj2 != null)
}
@@ -0,0 +1,27 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
enum class Color {
BLACK, WHITE
}
@Parcelize
data class Test(val name: String, val color: Color) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test("John", Color.WHITE)
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
assert(test == test2)
}
@@ -0,0 +1,25 @@
// IGNORE_BACKEND: JVM
// Parcel.readException throws the exception it reads and only supports a small number of exception types.
// If we have to parcel an exception we should instead use read/writeSerializable (compare KT-31830)
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
class ExceptionContainer(val exn: Exception) : Parcelable
fun box() = parcelTest { parcel ->
val test = ExceptionContainer(java.lang.RuntimeException("Don't throw me."))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<ExceptionContainer>(parcel)
}
@@ -0,0 +1,24 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class Test(val callback: () -> Int = { 0 }, val suspendCallback: suspend () -> Int = { 0 }) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test({ 1 }, { 1 })
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
assert(test.callback() == 1)
}
@@ -0,0 +1,39 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@Parcelize
data class Film(val genres: Array<Int>) : Parcelable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Film
if (!Arrays.equals(genres, other.genres)) return false
return true
}
override fun hashCode(): Int {
return Arrays.hashCode(genres)
}
}
fun box() = parcelTest { parcel ->
val film = Film(arrayOf(3, 5, 7))
film.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val film2 = readFromParcel<Film>(parcel)
assert(film == film2)
}
@@ -0,0 +1,33 @@
// This issue affects AIDL generated files, as reported in KT-25807
// WITH_RUNTIME
// FILE: J.java
import android.os.Parcel;
import test.K;
public class J {
public static K readParcel(Parcel parcel) {
return K.CREATOR.createFromParcel(parcel);
}
}
// FILE: test.kt
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class K(val x: Int) : Parcelable
fun box() = parcelTest { parcel ->
val first = K(0)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val second = J.readParcel(parcel)
assert(first == second)
}
@@ -0,0 +1,35 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
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")
}
}
@@ -0,0 +1,33 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
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)
parcel.setDataPosition(0)
val test2 = readFromParcel<J>(parcel)
assert(test.j.j1 == test2.j.j1)
}
@@ -0,0 +1,29 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.io.Serializable
class MHelp(var m1: String): Serializable {
val m2 = 9
}
@Parcelize
class M(val m: @RawValue MHelp) : Parcelable
fun box() = parcelTest { parcel ->
val test = M(MHelp("A"))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<M>(parcel)
assert(test.m.m1 == test2.m.m1)
}
@@ -0,0 +1,40 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@Parcelize
data class Test(val a: LongArray, val b: List<Long>) : Parcelable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as Test
if (!Arrays.equals(a, other.a)) return false
if (b != other.b) return false
return true
}
override fun hashCode() = Arrays.hashCode(a)
}
fun box() = parcelTest { parcel ->
val first = Test(longArrayOf(1, 2, 3, 4, 5), listOf(1, 2, 3, 4))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,49 @@
// IGNORE_BACKEND: JVM
// See KT-38106
// This feature regressed with the fix for KT-22576
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
enum class ParcelableEnum : Parcelable {
ONE, TWO, THREE;
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(ordinal)
}
override fun describeContents() = 0
companion object {
@JvmField
val CREATOR: Parcelable.Creator<ParcelableEnum> = object : Parcelable.Creator<ParcelableEnum> {
override fun createFromParcel(parcel: Parcel) = ParcelableEnum.ONE
override fun newArray(size: Int) = arrayOfNulls<ParcelableEnum>(size)
}
}
}
@Parcelize
class Test(val parcelableEnum: ParcelableEnum): Parcelable
fun box() = parcelTest { parcel ->
val first = Test(ParcelableEnum.THREE)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first.parcelableEnum == ParcelableEnum.THREE)
assert(first2.parcelableEnum == ParcelableEnum.ONE)
assert(first != first2)
}
@@ -0,0 +1,30 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
fun box() = doTest { creator ->
assert(creator.newArray(5) != null)
}
fun doTest(work: (Parcelable.Creator<DummyParcelable>) -> Unit): String {
val dummy = DummyParcelable(42)
val clazz = dummy.javaClass
val field = clazz.getDeclaredField("CREATOR")
val creator = field.get(dummy) as Parcelable.Creator<DummyParcelable>
val parcel = Parcel.obtain()
dummy.writeToParcel(parcel, 0)
parcel.setDataPosition(0)
work(creator)
return "OK"
}
@Parcelize
data class DummyParcelable(val int: Int): Parcelable
@@ -0,0 +1,29 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
class User : Parcelable
@Parcelize
class User2() : Parcelable
fun box() = parcelTest { parcel ->
val user = User()
val user2 = User2()
user.writeToParcel(parcel, 0)
user2.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
readFromParcel<User>(parcel)
readFromParcel<User2>(parcel)
}
@@ -0,0 +1,39 @@
// IGNORE_BACKEND: JVM
// Fails with a VerifyError in Foo.writeToParcel
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.util.SparseArray
@Parcelize
data class PInt(val x: Int) : Parcelable
@Parcelize
data class Foo(val values: SparseArray<SparseArray<Parcelable>>) : Parcelable
fun box() = parcelTest { parcel ->
val pint = PInt(0)
val sarray = SparseArray<Parcelable>()
sarray.put(0, pint)
val sarray2 = SparseArray<SparseArray<Parcelable>>()
sarray2.put(1, sarray)
val foo = Foo(sarray2)
foo.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val foo2 = readFromParcel<Foo>(parcel)
assert(foo2.values.size() == 1)
assert(foo2.values.get(1) != null) // SparseArray.contains was only added in Android R
assert(foo2.values.get(1).size() == 1)
assert(foo2.values.get(1).get(0) != null)
assert(foo2.values.get(1).get(0) == pint)
}
@@ -0,0 +1,23 @@
// IGNORE_BACKEND: JVM
// StackOverflowError caused by infinite loop in MyObject.writeToParcel
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
private object MyObject : Parcelable
fun box() = parcelTest { parcel ->
MyObject.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
readFromParcel<MyObject>(parcel)
}
@@ -0,0 +1,21 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize class TestParcel : Parcelable {
companion object : Parceler<TestParcel> {
override fun create(parcel: Parcel): TestParcel {
return TestParcel()
}
override fun TestParcel.write(parcel: Parcel, flags: Int) {}
}
}
fun box() = parcelTest { parcel ->
TestParcel()
}
@@ -0,0 +1,55 @@
// WITH_RUNTIME
// FULL_JDK
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.*
@Parcelize
data class Test(
val a: List<String>,
val b: MutableList<String>,
val c: ArrayList<String>,
val d: LinkedList<String>,
val e: Set<String>,
val f: MutableSet<String>,
val g: TreeSet<String>,
val h: HashSet<String>,
val i: LinkedHashSet<String>,
val j: NavigableSet<String>,
val k: SortedSet<String>
) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test(
a = listOf("A"),
b = mutableListOf("B"),
c = ArrayList<String>().apply { this += "C" },
d = LinkedList<String>().apply { this += "D" },
e = setOf("E"),
f = mutableSetOf("F"),
g = TreeSet<String>().apply { this += "G" },
h = HashSet<String>().apply { this += "H" },
i = LinkedHashSet<String>().apply { this += "I" },
j = TreeSet<String>().apply { this += "J" },
k = TreeSet<String>().apply { this += "K" }
)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert((first.d as LinkedList<*>).size == 1)
assert((first2.h as HashSet<*>).size == 1)
assert(first2.j is NavigableSet<*>)
assert(first2.k is SortedSet<*>)
}
@@ -0,0 +1,25 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class Test(val a: List<String>) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test(listOf("A", "B"))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,42 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class Test(
val a: List<String>,
val b: List<String?>,
val c: List<Int>,
val d: List<Int?>,
val e: List<List<String>?>,
val f: List<List<List<Int>>>
) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test(
a = listOf("A", "B"),
b = listOf("A", null, "C"),
c = listOf(1, 2, 3),
d = listOf(1, null, 5),
e = listOf(listOf("A", "B"), listOf(), null),
f = listOf(listOf(listOf(1, 2), listOf(3)), listOf(listOf(5, 3)))
)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert(first2.a == listOf("A", "B"))
assert(first2.b.size == 3)
}
@@ -0,0 +1,47 @@
// WITH_RUNTIME
// FULL_JDK
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.*
@Parcelize
data class Test(
val a: Map<String, String>,
val b: MutableMap<String, String>,
val c: HashMap<String, String>,
val d: LinkedHashMap<String, String>,
val e: TreeMap<String, String>,
val f: SortedMap<String, String>,
val g: NavigableMap<String, String>
) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test(
a = mapOf("A" to "B"),
b = mutableMapOf("A" to "B"),
c = HashMap<String, String>().apply { put("A", "B") },
d = LinkedHashMap<String, String>().apply { put("A", "B") },
e = TreeMap<String, String>().apply { put("A", "B") },
f = TreeMap<String, String>().apply { put("A", "B") },
g = TreeMap<String, String>().apply { put("A", "B") }
)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert((first.c as HashMap<*, *>).size == 1)
assert((first2.e as TreeMap<*, *>).size == 1)
assert(first2.f is SortedMap<*, *>)
assert(first2.g is NavigableMap<*, *>)
}
@@ -0,0 +1,25 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class Test(val a: Map<String, String>) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test(mapOf("A" to "B", "C" to "D"))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,41 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class Test(
val a: Map<String, String>,
val b: Map<String?, String>,
val c: Map<String, String?>,
val d: Map<String, Map<Int, String>>,
val e: Map<Int?, List<String>>,
val f: Map<Boolean, Boolean>,
val g: Map<String, Map<String, Map<String, String>>>
) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test(
a = mapOf("A" to "B", "C" to "D"),
b = mapOf("A" to "B", null to "D", "E" to "F"),
c = mapOf("A" to null, "C" to "D"),
d = mapOf("A" to mapOf(1 to "", 2 to "x")),
e = mapOf(1 to listOf("", ""), null to listOf()),
f = mapOf(true to false, false to true),
g = mapOf("A" to mapOf("B" to mapOf("C" to "D", "E" to "F")))
)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,26 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@Parcelize
class Data(val data: Array<Array<Int>>) : Parcelable
fun box() = parcelTest { parcel ->
val first = Data(arrayOf(arrayOf(0, 1)))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val second = readFromParcel<Data>(parcel)
assert(second.data.size == 1)
assert(Arrays.equals(second.data[0], arrayOf(0, 1)))
}
@@ -0,0 +1,26 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@Parcelize
class Data(val data: List<Array<Int>>) : Parcelable
fun box() = parcelTest { parcel ->
val first = Data(listOf(arrayOf(0, 1)))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val second = readFromParcel<Data>(parcel)
assert(second.data.size == 1)
assert(Arrays.equals(second.data[0], arrayOf(0, 1)))
}
@@ -0,0 +1,28 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@Parcelize
class Data(val data: Map<Array<Int>, Array<Int>>) : Parcelable
fun box() = parcelTest { parcel ->
val first = Data(mapOf(arrayOf(0) to arrayOf(1)))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val second = readFromParcel<Data>(parcel)
assert(second.data.size == 1)
val entry = second.data.entries.single()
assert(Arrays.equals(entry.key, arrayOf(0)))
assert(Arrays.equals(entry.value, arrayOf(1)))
}
@@ -0,0 +1,34 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
// Starts with A, should be loaded before other classes
abstract class AParcelable : Parcelable
@Parcelize
data class P1(val a: String) : AParcelable()
sealed class Sealed : AParcelable()
@Parcelize
data class Sealed1(val a: Int) : Sealed()
@Parcelize
data class Test(val a: P1, val b: AParcelable, val c: Sealed, val d: Sealed1) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test(P1(""), P1("My"), Sealed1(1), Sealed1(5))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
assert(test == test2)
}
@@ -0,0 +1,30 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JVM
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.util.SparseArray
import java.util.Arrays
@Parcelize
class Data(val data: SparseArray<Array<Int>>) : Parcelable
fun box() = parcelTest { parcel ->
var array = SparseArray<Array<Int>>()
array.append(0, arrayOf(0, 1))
val first = Data(array)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val second = readFromParcel<Data>(parcel)
assert(second.data.size() == 1)
assert(Arrays.equals(second.data.get(0), arrayOf(0, 1)))
}
@@ -0,0 +1,29 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class User(val firstName: String, val secondName: String, val age: Int) : Parcelable
fun box() = parcelTest { parcel ->
val user = User("John", "Smith", 20)
val user2 = User("Joe", "Bloggs", 30)
val array = arrayOf(user, user2)
parcel.writeTypedArray(array, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val creator = User::class.java.getDeclaredField("CREATOR").get(null) as Parcelable.Creator<User>
val result = parcel.createTypedArray(creator)
assert(result.size == 2)
assert(result[0].firstName == user.firstName)
assert(result[1].firstName == user2.firstName)
}
@@ -0,0 +1,38 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class Foo(val a: String) : Parcelable
@Parcelize
data class Test(
val str1: String,
val str2: String?,
val int1: Int,
val int2: Int?,
val foo: Foo?
) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test("John", "Smith", 20, 30, Foo("a"))
val second = Test("A", null, 20, null, null)
first.writeToParcel(parcel, 0)
second.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
val second2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert(second == second2)
}
@@ -0,0 +1,29 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class Test(val a: String?) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test("John")
val second = Test(null)
first.writeToParcel(parcel, 0)
second.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<Test>(parcel)
val second2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert(second == second2)
}
@@ -0,0 +1,31 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
object Obj1 {
object Obj2
}
@Parcelize
data class Test(val o1: Obj1, val o2: Obj1.Obj2, val com: Com) : Parcelable {
companion object Com {
}
}
fun box() = parcelTest { parcel ->
val test = Test(Obj1, Obj1.Obj2, Test.Com)
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
assert(test == test2)
}
@@ -0,0 +1,19 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
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"
}
@@ -0,0 +1,41 @@
// IGNORE_BACKEND: JVM
// See KT-38104
// The support for PersistableBundles is broken on JVM.
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.os.BaseBundle
import android.os.PersistableBundle
@Parcelize
data class User(val a: PersistableBundle) : Parcelable
fun box() = parcelTest { parcel ->
val test = User(
PersistableBundle().apply { putLong("A", 1L) }
)
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<User>(parcel)
assert(compareBundles(test.a, test2.a))
}
private fun compareBundles(first: BaseBundle, second: BaseBundle): Boolean {
if (first.size() != second.size()) return false
for (key in first.keySet()) {
if (first.get(key) != second.get(key)) return false
}
return true
}
@@ -0,0 +1,39 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
@Parcelize
data class PrimitiveTypes(
val boo: Boolean,
val c: Char,
val byt: Byte,
val s: Short,
val i: Int,
val f: Float,
val l: Long,
val d: Double
) : Parcelable
fun box() = parcelTest { parcel ->
val first = PrimitiveTypes(true, '#', 3.toByte(), 10.toShort(), -300, -5.0f, Long.MAX_VALUE, 3.14)
val second = PrimitiveTypes(false, '\n', Byte.MIN_VALUE, Short.MIN_VALUE, Int.MIN_VALUE, Float.POSITIVE_INFINITY,
Long.MAX_VALUE, Double.NEGATIVE_INFINITY)
first.writeToParcel(parcel, 0)
second.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val first2 = readFromParcel<PrimitiveTypes>(parcel)
val second2 = readFromParcel<PrimitiveTypes>(parcel)
assert(first == first2)
assert(second == second2)
}
@@ -0,0 +1,33 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
sealed class Foo : Parcelable {
@Parcelize
data class A(val x: Int) : Foo()
@Parcelize
data class B (val x: String) : Foo()
}
@Parcelize
data class Bar(val a: Foo) : Parcelable
fun box() = parcelTest { parcel ->
val first = Bar(Foo.B("OK"))
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val second = readFromParcel<Bar>(parcel)
assert(first == second)
}
@@ -0,0 +1,25 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
annotation class SerializableLike
@Parcelize @SerializableLike
data class User(val firstName: String, val secondName: String, val age: Int) : Parcelable
fun box() = parcelTest { parcel ->
val user = User("John", "Smith", 20)
user.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val user2 = readFromParcel<User>(parcel)
assert(user == user2)
}
@@ -0,0 +1,71 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.util.*
@Parcelize
data class Data(val a: String, val b: String) : Parcelable
@Parcelize
data class User(val a: SparseIntArray, val b: SparseLongArray, val c: SparseArray<Data>) : Parcelable
fun box() = parcelTest { parcel ->
val user = User(
a = SparseIntArray().apply { put(1, 5); put(100, -1); put(1000, 0) },
b = SparseLongArray().apply { put(3, 2); put(2, 3); put(10, 10) },
c = SparseArray<Data>().apply { put(1, Data("A", "B")); put(10, Data("C", "D")); put(105, Data("E", "")) }
)
user.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val user2 = readFromParcel<User>(parcel)
assert(compareSparseIntArrays(user.a, user2.a))
assert(compareSparseLongArrays(user.b, user2.b))
assert(compareSparseArrays(user.c, user2.c))
}
private fun compareSparseIntArrays(first: SparseIntArray, second: SparseIntArray): Boolean {
if (first === second) return true
if (first.size() != second.size()) return false
for (i in 0 until first.size()) {
if (first.keyAt(i) != second.keyAt(i)) return false
if (first.valueAt(i) != second.valueAt(i)) return false
}
return true
}
private fun compareSparseLongArrays(first: SparseLongArray, second: SparseLongArray): Boolean {
if (first === second) return true
if (first.size() != second.size()) return false
for (i in 0 until first.size()) {
if (first.keyAt(i) != second.keyAt(i)) return false
if (first.valueAt(i) != second.valueAt(i)) return false
}
return true
}
private fun compareSparseArrays(first: SparseArray<*>, second: SparseArray<*>): Boolean {
if (first === second) return true
if (first.size() != second.size()) return false
for (i in 0 until first.size()) {
if (first.keyAt(i) != second.keyAt(i)) return false
if (first.valueAt(i) != second.valueAt(i)) return false
}
return true
}
@@ -0,0 +1,37 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcel
import android.os.Parcelable
import android.util.SparseBooleanArray
@Parcelize
data class User(val a: SparseBooleanArray) : Parcelable
fun box() = parcelTest { parcel ->
val test = User(SparseBooleanArray().apply { put(1, false); put(5, true); put(1000, false) })
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<User>(parcel)
assert(compareSparseBooleanArrays(test.a, test2.a))
}
private fun compareSparseBooleanArrays(first: SparseBooleanArray, second: SparseBooleanArray): Boolean {
if (first === second) return true
if (first.size() != second.size()) return false
for (i in 0 until first.size()) {
if (first.keyAt(i) != second.keyAt(i)) return false
if (first.valueAt(i) != second.valueAt(i)) return false
}
return true
}