Parcelable: Use Parcel methods for reading-writing primitive types (KT-20020)

This commit is contained in:
Yan Zhulanow
2017-09-02 00:01:20 +03:00
committed by Yan Zhulanow
parent a03c03c427
commit 89c5f78a8e
2 changed files with 34 additions and 0 deletions
@@ -64,6 +64,14 @@ interface ParcelSerializer {
fun strict() = strict && !type.annotations.hasAnnotation(RAWVALUE_ANNOTATION_FQNAME)
return when {
asmType.descriptor == "[I"
|| asmType.descriptor == "[Z"
|| asmType.descriptor == "[B"
|| asmType.descriptor == "[C"
|| asmType.descriptor == "[D"
|| asmType.descriptor == "[F"
|| asmType.descriptor == "[L" -> PrimitiveArrayParcelSerializer(asmType)
asmType.sort == Type.ARRAY -> {
val elementType = type.builtIns.getArrayElementType(type)
val elementSerializer = get(elementType, typeMapper.mapTypeSafe(elementType), context, strict = strict())
@@ -494,6 +494,32 @@ internal class NullAwareParcelSerializerWrapper(val delegate: ParcelSerializer)
}
}
internal class PrimitiveArrayParcelSerializer(
override val asmType: Type
) : ParcelSerializer {
private val methodNameBase = when (asmType.elementType) {
Type.INT_TYPE -> "Int"
Type.BOOLEAN_TYPE -> "Boolean"
Type.BYTE_TYPE -> "Byte"
Type.CHAR_TYPE -> "Char"
Type.DOUBLE_TYPE -> "Double"
Type.FLOAT_TYPE -> "Float"
Type.LONG_TYPE -> "Long"
else -> error("Unsupported type ${asmType.elementType.descriptor}")
}
private val writeMethod = Method("write${methodNameBase}Array", "(${asmType.descriptor})V")
private val createArrayMethod = Method("create${methodNameBase}Array", "()${asmType.descriptor}")
override fun writeValue(v: InstructionAdapter) {
v.invokevirtual(PARCEL_TYPE.internalName, writeMethod.name, writeMethod.signature, false)
}
override fun readValue(v: InstructionAdapter) {
v.invokevirtual(PARCEL_TYPE.internalName, createArrayMethod.name, createArrayMethod.signature, false)
}
}
/** write...() and get...() methods in Android should support passing `null` values. */
internal class NullCompliantObjectParcelSerializer(
override val asmType: Type,