Parcelable: Add Parcelable functionality to Android Extensions plugin

This commit is contained in:
Yan Zhulanow
2017-06-16 16:52:29 +03:00
parent edd8a0a64e
commit c23bca6afe
47 changed files with 2979 additions and 3 deletions
@@ -0,0 +1,38 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@MagicParcel
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)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,76 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
@MagicParcel
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)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,41 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val first2 = readFromParcel<BoxedTypes>(parcel)
assert(first == first2)
}
@@ -0,0 +1,34 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import android.os.Bundle
@MagicParcel
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)
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,48 @@
// WITH_RUNTIME
// FULL_JDK
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import java.util.*
@MagicParcel
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>
) : 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" }
)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert((first.d as LinkedList<*>).size == 1)
assert((first2.h as HashSet<*>).size == 1)
}
@@ -0,0 +1,24 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,41 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert(first2.a == listOf("A", "B"))
assert(first2.b.size == 3)
}
@@ -0,0 +1,40 @@
// WITH_RUNTIME
// FULL_JDK
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import java.util.*
@MagicParcel
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>
) : 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") }
)
first.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert((first.c as HashMap<*, *>).size == 1)
assert((first2.e as TreeMap<*, *>).size == 1)
}
@@ -0,0 +1,24 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,40 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val first2 = readFromParcel<Test>(parcel)
assert(first == first2)
}
@@ -0,0 +1,33 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
data class Test(
val str1: String,
val str2: String?,
val int1: Int,
val int2: Int?
) : Parcelable
fun box() = parcelTest { parcel ->
val first = Test("John", "Smith", 20, 30)
val second = Test("A", null, 20, null)
first.writeToParcel(parcel, 0)
second.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
val first2 = readFromParcel<Test>(parcel)
val second2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert(second == second2)
}
@@ -0,0 +1,28 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val first2 = readFromParcel<Test>(parcel)
val second2 = readFromParcel<Test>(parcel)
assert(first == first2)
assert(second == second2)
}
@@ -0,0 +1,38 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val first2 = readFromParcel<PrimitiveTypes>(parcel)
val second2 = readFromParcel<PrimitiveTypes>(parcel)
assert(first == first2)
assert(second == second2)
}
@@ -0,0 +1,22 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
@MagicParcel
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)
val user2 = readFromParcel<User>(parcel)
assert(user == user2)
}
@@ -0,0 +1,70 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import android.util.*
@MagicParcel
data class Data(val a: String, val b: String) : Parcelable
@MagicParcel
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)
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,36 @@
// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import android.util.SparseBooleanArray
@MagicParcel
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)
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
}