Skip to content

Commit 67e0e61

Browse files
Fix Kotlin Serialization
1 parent 75d92f6 commit 67e0e61

7 files changed

Lines changed: 638 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Fixed
1212

13+
- Kotlin Serialization
14+
1315
### Removed
1416

1517
### Updated

kopy-compiler/main/kotlin/com/javiersc/kotlin/kopy/compiler/fir/generation/FirKopyDeclarationGenerationExtension.kt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import com.javiersc.kotlin.kopy.compiler.kopyFunctionUpdateEachClassId
2525
import com.javiersc.kotlin.kopy.compiler.kopyOptInClassId
2626
import com.javiersc.kotlin.kopy.compiler.measureExecution
2727
import com.javiersc.kotlin.kopy.compiler.measureKey
28+
import com.javiersc.kotlin.kopy.compiler.serializableAnnotationClassId
2829
import com.javiersc.kotlin.kopy.compiler.setName
30+
import com.javiersc.kotlin.kopy.compiler.transientAnnotationClassId
2931
import com.javiersc.kotlin.kopy.compiler.underscoreAtomicName
3032
import com.javiersc.kotlin.kopy.compiler.updateEachName
3133
import com.javiersc.kotlin.kopy.compiler.updateName
@@ -130,16 +132,17 @@ internal class FirKopyDeclarationGenerationExtension(
130132

131133
val atomicProperty: FirProperty =
132134
createMemberProperty(
133-
owner = context.owner,
134-
key = Key,
135-
name = callableId.callableName,
136-
returnType = atomicRefType,
137-
config = {
138-
status { isOverride = false }
139-
modality = Modality.FINAL
140-
visibility = atomicVisibility
141-
},
142-
)
135+
owner = context.owner,
136+
key = Key,
137+
name = callableId.callableName,
138+
returnType = atomicRefType,
139+
config = {
140+
status { isOverride = false }
141+
modality = Modality.FINAL
142+
visibility = atomicVisibility
143+
},
144+
)
145+
.apply { addTransientAnnotationIfOwnedBySerializable(owner = owner) }
143146
return listOf(atomicProperty.symbol)
144147
}
145148

@@ -423,6 +426,17 @@ internal class FirKopyDeclarationGenerationExtension(
423426
?.toFirTypeRef()
424427
?.let(::createFirAnnotation)
425428

429+
private fun FirProperty.addTransientAnnotationIfOwnedBySerializable(owner: FirClassSymbol<*>) {
430+
val isSerializable: Boolean = owner.hasAnnotation(serializableAnnotationClassId, session)
431+
if (isSerializable) {
432+
val replacedAnnotations: List<FirAnnotation> = buildList {
433+
addAll(annotations)
434+
createAnnotation(classId = transientAnnotationClassId)?.let(::add)
435+
}
436+
replaceAnnotations(replacedAnnotations)
437+
}
438+
}
439+
426440
private fun calculateVisibility(classSymbol: FirClassSymbol<*>): Visibility {
427441
val visibility: Visibility =
428442
classSymbol.primaryConstructorSymbol(session)?.visibility ?: return Visibilities.Public

kopy-compiler/main/kotlin/com/javiersc/kotlin/kopy/compiler/values.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ private const val iterable: String = "kotlin.collections.Iterable"
2121
private const val list: String = "kotlin.collections.List"
2222
private const val map: String = "kotlin.collections.map"
2323
private const val also: String = "kotlin.also"
24+
private const val serializableAnnotation: String = "kotlinx.serialization.Serializable"
25+
private const val transientAnnotation: String = "kotlinx.serialization.Transient"
2426

2527
internal val kopyFqName: FqName = kopy.toFqName()
2628
internal val kopyFunctionCopyFqName: FqName = kopyFunctionCopy.toFqName()
@@ -39,6 +41,8 @@ internal val kopyFunctionUpdateEachClassId: ClassId = kopyFunctionUpdateEach.toC
3941
internal val atomicReferenceClassId: ClassId = atomicReference.toClassId()
4042
internal val iterableClassId: ClassId = iterable.toClassId()
4143
internal val listClassId: ClassId = list.toClassId()
44+
internal val serializableAnnotationClassId: ClassId = serializableAnnotation.toClassId()
45+
internal val transientAnnotationClassId: ClassId = transientAnnotation.toClassId()
4246

4347
internal val underscoreAtomicName: Name = "_atomic".toName()
4448
internal val loadName: Name = "load".toName()

0 commit comments

Comments
 (0)