Files
kotlin-fork/plugins/kotlinx-serialization/testData/boxIr/serialInfo.kt
T
Leonid Startsev f1b1837f40 Rework SerialInfo$Impl annotation implementation to be available in FIR:
- IR plugin does not use it anymore,
regular annotation instantiation feature is used
(insertion of IrConstructorCall(annotationCtorSymbol)).

- IR plugin still generates `SerialInfo$Impl` class to be compatible
with previously compiled code & libraries.

- Custom implementation over descriptors in IR plugin is removed;
plugin now delegates to the existing lowering with some tweaks.

- $Impl descriptor is removed from FE 1.0 because it is no longer needed
for IR plugin; it shouldn't be exposed to users so FIR doesn't need it either.

- Since language lowering is now used, it is possible to correctly instantiate
SerialInfo annotations with default values even if they are from other modules
and even if they were not processed by the plugin.

#KT-48733 Fixed
Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1574
2022-10-17 16:10:42 +00:00

32 lines
1.7 KiB
Kotlin
Vendored

// WITH_STDLIB
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.descriptors.*
@SerialInfo
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
annotation class MyId(val id: Int, val type: String = "foo")
@Serializable
@MyId(10)
class Foo(@MyId(20) val i: Int)
fun box(): String {
val desc = Foo.serializer().descriptor
val classId = desc.annotations.filterIsInstance<MyId>().single()
val propId = desc.getElementAnnotations(0).filterIsInstance<MyId>().single().id
if (classId.id != 10) return "Incorrect class annotation: ${classId}"
if (classId.type != "foo") return "Incorrect default argument: ${classId}"
if (!classId::class.java.toString().contains("annotationImpl")) return "Backend doesn't use annotation instantiation: ${classId::class}"
if (propId != 20) return "Incorrect propery annotation: $propId"
val implClassJava = Class.forName("MyId\$Impl")
if (implClassJava.toString() != "class MyId\$Impl") return "Old annotation implementations are not preserved for compatibility"
val ctorStr = implClassJava.constructors.toList().toString()
if (!ctorStr.contains("public MyId\$Impl(int,java.lang.String)")) return "Compatibility impl does not contain correct constructor: $ctorStr"
val methodsStr = implClassJava.methods.toList().toString()
if (!methodsStr.contains("public final int MyId\$Impl.id()")) return "Compatibility impl does not contain correct methods: $methodsStr"
if (!methodsStr.contains("public final java.lang.String MyId\$Impl.type()")) return "Compatibility impl does not contain correct methods: $methodsStr"
return "OK"
}