@@ -29,15 +29,15 @@ import org.jetbrains.kotlin.parcelize.serializers.ParcelizeExtensionBase
29
29
abstract class ParcelizeIrTransformerBase (
30
30
protected val context : IrPluginContext ,
31
31
protected val androidSymbols : AndroidSymbols ,
32
- protected val parcelizeAnnotations : List <FqName >
32
+ protected val parcelizeAnnotations : List <FqName >,
33
33
) : ParcelizeExtensionBase, IrElementVisitorVoid {
34
34
private val irFactory: IrFactory = IrFactoryImpl
35
35
36
36
protected val deferredOperations = mutableListOf< () -> Unit > ()
37
37
protected fun defer (block : () -> Unit ) = deferredOperations.add(block)
38
38
39
- protected fun IrSimpleFunction.generateDescribeContentsBody (parcelableProperties : List <ParcelableProperty ? >) {
40
- val flags = if (parcelableProperties.any { it != null && it .field.type.containsFileDescriptors }) 1 else 0
39
+ protected fun IrSimpleFunction.generateDescribeContentsBody (parcelableProperties : List <ParcelableProperty >) {
40
+ val flags = if (parcelableProperties.any { it.field.type.containsFileDescriptors }) 1 else 0
41
41
body = context.createIrBuilder(symbol).run {
42
42
irExprBody(irInt(flags))
43
43
}
@@ -46,10 +46,10 @@ abstract class ParcelizeIrTransformerBase(
46
46
protected fun IrSimpleFunction.generateWriteToParcelBody (
47
47
irClass : IrClass ,
48
48
parcelerObject : IrClass ? ,
49
- parcelableProperties : List <ParcelableProperty ? >,
49
+ parcelableProperties : List <ParcelableProperty >,
50
50
receiverParameter : IrValueParameter ,
51
51
parcelParameter : IrValueParameter ,
52
- flagsParameter : IrValueParameter
52
+ flagsParameter : IrValueParameter ,
53
53
) {
54
54
body = androidSymbols.createBuilder(symbol).run {
55
55
irBlockBody {
@@ -59,14 +59,12 @@ abstract class ParcelizeIrTransformerBase(
59
59
60
60
parcelableProperties.isNotEmpty() ->
61
61
for (property in parcelableProperties) {
62
- if (property != null ) {
63
- + writeParcelWith(
64
- property.parceler,
65
- parcelParameter,
66
- flagsParameter,
67
- irGetField(irGet(receiverParameter), property.field)
68
- )
69
- }
62
+ + writeParcelWith(
63
+ property.parceler,
64
+ parcelParameter,
65
+ flagsParameter,
66
+ irGetField(irGet(receiverParameter), property.field)
67
+ )
70
68
}
71
69
72
70
else ->
@@ -81,7 +79,7 @@ abstract class ParcelizeIrTransformerBase(
81
79
}
82
80
}
83
81
84
- protected fun generateCreator (declaration : IrClass , parcelerObject : IrClass ? , parcelableProperties : List <ParcelableProperty ? >) {
82
+ protected fun generateCreator (declaration : IrClass , parcelerObject : IrClass ? , parcelableProperties : List <ParcelableProperty >) {
85
83
// Since the `CREATOR` object cannot refer to the type parameters of the parcelable class we use a star projected type
86
84
val declarationType = declaration.symbol.starProjectedType
87
85
val creatorType = androidSymbols.androidOsParcelableCreator.typeWith(declarationType)
@@ -139,10 +137,8 @@ abstract class ParcelizeIrTransformerBase(
139
137
140
138
parcelableProperties.isNotEmpty() ->
141
139
irCall(declaration.primaryConstructor!! ).apply {
142
- for ((index, property) in parcelableProperties.withIndex()) {
143
- if (property != null ) {
144
- putValueArgument(index, readParcelWith(property.parceler, parcelParameter))
145
- }
140
+ for (property in parcelableProperties) {
141
+ putValueArgument(property.index, readParcelWith(property.parceler, parcelParameter))
146
142
}
147
143
}
148
144
@@ -171,19 +167,19 @@ abstract class ParcelizeIrTransformerBase(
171
167
serializerFactory.get(defaultType, parcelizeType = defaultType, strict = true , toplevel = true , scope = getParcelerScope())
172
168
}
173
169
174
- protected class ParcelableProperty (val field : IrField , parcelerThunk : () -> IrParcelSerializer ) {
170
+ protected class ParcelableProperty (val field : IrField , val index : Int , parcelerThunk : () -> IrParcelSerializer ) {
175
171
val parceler by lazy(parcelerThunk)
176
172
}
177
173
178
174
private val serializerFactory = IrParcelSerializerFactory (androidSymbols, parcelizeAnnotations)
179
175
180
- protected val IrClass .parcelableProperties: List <ParcelableProperty ? >
176
+ protected val IrClass .parcelableProperties: List <ParcelableProperty >
181
177
get() {
182
178
if (kind != ClassKind .CLASS ) return emptyList()
183
179
184
180
val constructor = primaryConstructor ? : return emptyList()
185
181
val topLevelScope = getParcelerScope()
186
- return constructor .valueParameters.map { parameter ->
182
+ return constructor .valueParameters.mapIndexedNotNull { index, parameter ->
187
183
val property = properties.firstOrNull { it.name == parameter.name }
188
184
if (property == null || property.hasAnyAnnotation(IGNORED_ON_PARCEL_FQ_NAMES )) {
189
185
null
@@ -193,7 +189,7 @@ abstract class ParcelizeIrTransformerBase(
193
189
if (backingField == null ) {
194
190
null
195
191
} else {
196
- ParcelableProperty (backingField) {
192
+ ParcelableProperty (backingField, index ) {
197
193
serializerFactory.get(parameter.type, parcelizeType = defaultType, scope = localScope)
198
194
}
199
195
}
0 commit comments