# Custom compiler annotations

**URL:** <https://discuss.kotlinlang.org/t/custom-compiler-annotations/218>\
**Category:** Uncategorized\
**Created:** [July 3, 2015, 5:10am UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218 "2015-07-03T05:10:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcnor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arcnor/32/1457_2.png) [@arcnor](https://discuss.kotlinlang.org/u/arcnor)\
**Post date:** [July 3, 2015, 5:10am UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218/1 "2015-07-03T05:10:58Z")

</div>

Hi,

Is there a way of creating a custom annotation like `data`? I.e. having an annotation that generates some code and my own code can use, and (as bonus) that IDEA won’t mark as invalid?

If that’s not possible, does accessing Kotlin annotations at runtime have the same performance implications as with Java? (which uses reflection, which means it’s bad in places like Android)

Thanks!

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [July 3, 2015, 12:39pm UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218/2 "2015-07-03T12:39:08Z")

</div>

Annotations are just standard JVM entities and doesn't differ much from those generated by Java compiler. As of now, there is no special facility to create custom code transformations or generators in the language. You can use [annotation processing](http://blog.jetbrains.com/kotlin/2015/05/kapt-annotation-processing-for-kotlin/) to generate some code based on annotations. Or you can write compiler plugin, which is hard and unsupported, but may be much more powerful.

---

<div class="post-metadata">

**Author:** ![arcnor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arcnor/32/1457_2.png) [@arcnor](https://discuss.kotlinlang.org/u/arcnor)\
**Post date:** [July 3, 2015, 12:44pm UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218/3 "2015-07-03T12:44:50Z")

</div>

Thank you!

Is there any planned support to create our own “compiler” annotations? If I understand it correctly, using that “kapt” tool won’t make IDEA pick anything that it generates because it needs a separate step.

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [July 3, 2015, 12:48pm UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218/4 "2015-07-03T12:48:37Z")

</div>

No, we don't have any specific plans right now. You can take a look at plugin to generate Android properties from .xml, [https://github.com/JetBrains/kotlin/tree/master/plugins](https://github.com/JetBrains/kotlin/tree/master/plugins)

Out of curiosity, what kind of code generation do you have in mind?

---

<div class="post-metadata">

**Author:** ![arcnor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arcnor/32/1457_2.png) [@arcnor](https://discuss.kotlinlang.org/u/arcnor)\
**Post date:** [July 3, 2015, 12:52pm UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218/5 "2015-07-03T12:52:22Z")

</div>

Thanks, will take a look.

Right now, it might be a bit overkill, but I’ve created a few annotations that I use to mark some classes and store metadata about them (things like “speed” or “altitude”). At runtime, I get the annotations using reflection and get those values. It will be nice if I can instead get the values using getters, like the `data` annotation does.

I can also obviously move all this annotation metadata to normal fields, but the definition of the metadata was very nice to read, at least in Java.

Thank you for your responses 🙂

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [July 3, 2015, 1:02pm UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218/6 "2015-07-03T13:02:51Z")

</div>

You can try to use companion objects instead. Something like this:

&nbsp;

```kotlin
data class Metadata(val speed : Double, val altitude: Double)
class LandRover {
  companion object {
  val metadata = Metadata(120.0, 0.0)
  }
}
fun use() {
  println(LandRover.metadata.altitude)
}
```

---

<div class="post-metadata">

**Author:** ![arcnor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arcnor/32/1457_2.png) [@arcnor](https://discuss.kotlinlang.org/u/arcnor)\
**Post date:** [July 3, 2015, 1:05pm UTC](https://discuss.kotlinlang.org/t/custom-compiler-annotations/218/7 "2015-07-03T13:05:07Z")

</div>

Well, that's more verbose than the annotations, but interesting nonetheless :). I'll read about those companion objects to see what they can do, thanks!
