-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parcelize: Handle class hierarchies of Parcelers (KT-46567)
(cherry picked from commit bf7db84)
- Loading branch information
Steven Schäfer
authored and
Space
committed
May 19, 2021
1 parent
87b2cc1
commit f0f2b92
Showing
11 changed files
with
293 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...-extensions-compiler/test/org/jetbrains/kotlin/android/parcel/ParcelBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...xtensions-compiler/test/org/jetbrains/kotlin/android/parcel/ParcelIrBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
plugins/android-extensions/android-extensions-compiler/testData/parcel/box/kt46567.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// WITH_RUNTIME | ||
|
||
@file:JvmName("TestKt") | ||
package test | ||
|
||
import kotlinx.android.parcel.* | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import java.util.Arrays | ||
|
||
/** | ||
* Generic pair parceler | ||
* Create concrete object to use (see below) | ||
*/ | ||
open class PairParceler<F: Any, S: Any>(private val firstParceler: Parceler<F>, private val secondParceler: Parceler<S>): Parceler<Pair<F, S>> { | ||
/** | ||
* Reads the [T] instance state from the [parcel], constructs the new [T] instance and returns it. | ||
*/ | ||
override fun create(parcel: Parcel): Pair<F, S> = | ||
firstParceler.create(parcel) to secondParceler.create(parcel) | ||
|
||
/** | ||
* Writes the [T] instance state to the [parcel]. | ||
*/ | ||
override fun Pair<F, S>.write(parcel: Parcel, flags: Int) { | ||
with(firstParceler) { this@write.first.write(parcel, 0) } | ||
with(secondParceler) { this@write.second.write(parcel, 0) } | ||
} | ||
} | ||
|
||
object IntParceler: Parceler<Int> { | ||
/** | ||
* Reads the [T] instance state from the [parcel], constructs the new [T] instance and returns it. | ||
*/ | ||
override fun create(parcel: Parcel): Int = parcel.readInt() | ||
|
||
/** | ||
* Writes the [T] instance state to the [parcel]. | ||
*/ | ||
override fun Int.write(parcel: Parcel, flags: Int) { | ||
parcel.writeInt(this) | ||
} | ||
} | ||
|
||
/** | ||
* [Int] to [Int] pair parceler | ||
*/ | ||
object IntToIntParceler: PairParceler<Int, Int>(IntParceler, IntParceler) | ||
|
||
@Parcelize | ||
@TypeParceler<Pair<Int, Int>, IntToIntParceler> | ||
class A(val pair: Pair<Int, Int>): Parcelable | ||
|
||
fun box() = parcelTest { parcel -> | ||
val a1 = A(1 to 2) | ||
a1.writeToParcel(parcel, 0) | ||
|
||
val bytes = parcel.marshall() | ||
parcel.unmarshall(bytes, 0, bytes.size) | ||
parcel.setDataPosition(0) | ||
|
||
val a2 = readFromParcel<A>(parcel) | ||
assert(a1.pair == a2.pair) | ||
} |
45 changes: 45 additions & 0 deletions
45
...ns/android-extensions/android-extensions-compiler/testData/parcel/box/newArrayParceler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// WITH_RUNTIME | ||
// IGNORE_BACKEND: JVM | ||
|
||
@file:JvmName("TestKt") | ||
package test | ||
|
||
import kotlinx.android.parcel.* | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
|
||
abstract class UserParceler : Parceler<User> { | ||
override fun User.write(parcel: Parcel, flags: Int) { | ||
parcel.writeString(name) | ||
} | ||
|
||
override fun newArray(size: Int): Array<User> { | ||
return Array(size + 1) { User(null) } | ||
} | ||
} | ||
|
||
@Parcelize | ||
class User(val name: String?) : Parcelable { | ||
companion object : UserParceler() { | ||
override fun create(parcel: Parcel) = User(parcel.readString()) | ||
} | ||
} | ||
|
||
fun box() = parcelTest { parcel -> | ||
val user = User("John") | ||
val user2 = User("Joe") | ||
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 == 3) | ||
assert(result[0].name == user.name) | ||
assert(result[1].name == user2.name) | ||
assert(result[2].name == null) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,4 +167,4 @@ public final class test/Foo : java/lang/Object, android/os/Parcelable { | |
RETURN | ||
LABEL (L1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
plugins/parcelize/parcelize-compiler/testData/box/kt46567.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// WITH_RUNTIME | ||
|
||
@file:JvmName("TestKt") | ||
package test | ||
|
||
import kotlinx.parcelize.* | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import java.util.Arrays | ||
|
||
/** | ||
* Generic pair parceler | ||
* Create concrete object to use (see below) | ||
*/ | ||
open class PairParceler<F: Any, S: Any>(private val firstParceler: Parceler<F>, private val secondParceler: Parceler<S>): Parceler<Pair<F, S>> { | ||
/** | ||
* Reads the [T] instance state from the [parcel], constructs the new [T] instance and returns it. | ||
*/ | ||
override fun create(parcel: Parcel): Pair<F, S> = | ||
firstParceler.create(parcel) to secondParceler.create(parcel) | ||
|
||
/** | ||
* Writes the [T] instance state to the [parcel]. | ||
*/ | ||
override fun Pair<F, S>.write(parcel: Parcel, flags: Int) { | ||
with(firstParceler) { this@write.first.write(parcel, 0) } | ||
with(secondParceler) { this@write.second.write(parcel, 0) } | ||
} | ||
} | ||
|
||
object IntParceler: Parceler<Int> { | ||
/** | ||
* Reads the [T] instance state from the [parcel], constructs the new [T] instance and returns it. | ||
*/ | ||
override fun create(parcel: Parcel): Int = parcel.readInt() | ||
|
||
/** | ||
* Writes the [T] instance state to the [parcel]. | ||
*/ | ||
override fun Int.write(parcel: Parcel, flags: Int) { | ||
parcel.writeInt(this) | ||
} | ||
} | ||
|
||
/** | ||
* [Int] to [Int] pair parceler | ||
*/ | ||
object IntToIntParceler: PairParceler<Int, Int>(IntParceler, IntParceler) | ||
|
||
@Parcelize | ||
@TypeParceler<Pair<Int, Int>, IntToIntParceler> | ||
class A(val pair: Pair<Int, Int>): Parcelable | ||
|
||
fun box() = parcelTest { parcel -> | ||
val a1 = A(1 to 2) | ||
a1.writeToParcel(parcel, 0) | ||
|
||
val bytes = parcel.marshall() | ||
parcel.unmarshall(bytes, 0, bytes.size) | ||
parcel.setDataPosition(0) | ||
|
||
val a2 = readFromParcel<A>(parcel) | ||
assert(a1.pair == a2.pair) | ||
} |
45 changes: 45 additions & 0 deletions
45
plugins/parcelize/parcelize-compiler/testData/box/newArrayParceler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// WITH_RUNTIME | ||
// IGNORE_BACKEND: JVM | ||
|
||
@file:JvmName("TestKt") | ||
package test | ||
|
||
import kotlinx.parcelize.* | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
|
||
abstract class UserParceler : Parceler<User> { | ||
override fun User.write(parcel: Parcel, flags: Int) { | ||
parcel.writeString(name) | ||
} | ||
|
||
override fun newArray(size: Int): Array<User> { | ||
return Array(size + 1) { User(null) } | ||
} | ||
} | ||
|
||
@Parcelize | ||
class User(val name: String?) : Parcelable { | ||
companion object : UserParceler() { | ||
override fun create(parcel: Parcel) = User(parcel.readString()) | ||
} | ||
} | ||
|
||
fun box() = parcelTest { parcel -> | ||
val user = User("John") | ||
val user2 = User("Joe") | ||
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 == 3) | ||
assert(result[0].name == user.name) | ||
assert(result[1].name == user2.name) | ||
assert(result[2].name == null) | ||
} |
Oops, something went wrong.