# Can't add annotation by compiler plugin

**URL:** <https://discuss.kotlinlang.org/t/cant-add-annotation-by-compiler-plugin/25099>\
**Category:** Support\
**Created:** [June 25, 2022, 4:45pm UTC](https://discuss.kotlinlang.org/t/cant-add-annotation-by-compiler-plugin/25099 "2022-06-25T16:45:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmitryb.dev](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@dmitryb.dev](https://discuss.kotlinlang.org/u/dmitryb.dev)\
**Post date:** [June 25, 2022, 4:45pm UTC](https://discuss.kotlinlang.org/t/cant-add-annotation-by-compiler-plugin/25099/1 "2022-06-25T16:45:03Z")

</div>

I’m trying to create kotlin compiler plugin that will parse all the doc comments and generate @Schema annotation for them. So what I’m doing now:

```auto
@Suppress("UNCHECKED_CAST")
class OpenApiCommentsGenerationTransformer(
    private val pluginContext: IrPluginContext,
    private val msgs: MessageCollector,
) : IrElementTransformerVoidWithContext() {
    @ObsoleteDescriptorBasedAPI
    override fun visitClassNew(declaration: IrClass): IrStatement {
        val schema = IrSingleStatementBuilder(pluginContext, Scope(declaration.symbol), -1, -1).build {
            // schema generated here
        }

        declaration.annotations += schema
        return super.visitClassNew(declaration)
    }
}

```

But this works only when class already has some annotation. E.g. with this class

```auto
@MyAnn
class TestObj

```

I get next bytecode generated:

```auto
@comments.to.openapi.MyAnn @io.swagger.v3.oas.annotations.media.Schema public final class TestObj

```

So both annotations are present. But if I have next declaration

```auto
class TestObj

```

I’m getting:

```auto
public final class TestObj

```

`declaration.dump` shows that Schema annotation is generated in both cases. But for some reason it’s not written to the result `.class` file. The same story for properties.
