1

I'm trying to generate code to declare a class with a @TypeParceler annotation.
I can create a ParameterizedTypeName with:

    val myParcelerClass = ClassName("com.example", "MyParceler")
    val genericTypeParcelerClass = ClassName("kotlinx.android.parcel", "TypeParceler")
    val parameterizedTypeParcelerClass = genericTypeParcelerClass.plusParameter(MyType::class.asClassName()).plusParameter(myParcelerClass)


I'd like to add an annotation with parameterizedTypeParcelerClass using TypeSpec.Builder.addAnnotation() but there isn't an override with a TypeName. Like this:

    val myTargetClass = ClassName("com.example", "MyTarget")
    val classBuilder = TypeSpec.classBuilder(myTargetClass)
            .addAnnotation(parameterizedTypeParcelerClass)

The TypeParceler annotation class is not available to the KotlinPoet code because the Android extension library isn't included here.
Is there a way to do this that I'm missing?

  • This looks like a missing feature, I've opened a Github issue: https://github.com/square/kotlinpoet/issues/881. If you're interested in contributing you're more than welcome to send us a PR! – Egor Feb 25 '20 at 13:41

0 Answers0