# How to get the class's annotation by reflection

**URL:** <https://discuss.kotlinlang.org/t/how-to-get-the-classs-annotation-by-reflection/423>\
**Category:** Uncategorized\
**Created:** [October 27, 2014, 8:46am UTC](https://discuss.kotlinlang.org/t/how-to-get-the-classs-annotation-by-reflection/423 "2014-10-27T08:46:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssiori](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@ssiori](https://discuss.kotlinlang.org/u/ssiori)\
**Post date:** [October 27, 2014, 8:46am UTC](https://discuss.kotlinlang.org/t/how-to-get-the-classs-annotation-by-reflection/423/1 "2014-10-27T08:46:11Z")

</div>

&nbsp;

```kotlin
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Retention
import kotlin.reflect.KFunction1
Retention(RetentionPolicy.RUNTIME)
annotation class special(val why: String)
open class Model(val str: String) {
  open fun print() {
  println(str)
  }
}
special(“Mode 1 annotation”)
open class Model1(str: String) : Model(str)
special(“Mode 2 annotation”)
open class Model2(str: String) : Model(str)
class Factory(val func: KFunction1<String, Model>) {
  fun gen(): Model? {
  &nbsp;&nbsp;// Can't get the Model's special annotation, how to get it?
  &nbsp;&nbsp;val str = func.javaClass.getAnnotation(javaClass&lt;special&gt;())?.why?:"error: $func"

  &nbsp;&nbsp;return func.invoke(str)

  }
}
fun main(args: Array<String>) {
  Factory(::Model1).gen()?.print()
  Factory(::Model2).gen()?.print()
}
```

---

<div class="post-metadata">

**Author:** ![udalov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/udalov/32/5679_2.png) [@udalov](https://discuss.kotlinlang.org/u/udalov)\
**Post date:** [October 27, 2014, 9:02am UTC](https://discuss.kotlinlang.org/t/how-to-get-the-classs-annotation-by-reflection/423/2 "2014-10-27T09:02:19Z")

</div>

You're trying to load annotations of an anonymous declaration synthesized for `::ModelN` expression, instead of the desired class.

Right now I can only suggest you additionally to pass an instance of `Class` you’re creating to `Factory`. In the future `func` will be a `KConstructor` and it will be possible to obtain this information from it alone.

---

<div class="post-metadata">

**Author:** ![ssiori](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@ssiori](https://discuss.kotlinlang.org/u/ssiori)\
**Post date:** [October 27, 2014, 10:25am UTC](https://discuss.kotlinlang.org/t/how-to-get-the-classs-annotation-by-reflection/423/3 "2014-10-27T10:25:23Z")

</div>

&nbsp;

```kotlin
class Factory(val cls: Class<out Model>) {
  fun gen(): Model? {
  &nbsp;&nbsp;val str = cls.getAnnotation(javaClass&lt;special&gt;())?.why ?: "error: ${cls}"

  &nbsp;&nbsp;return cls.getConstructor(javaClass&lt;String&gt;()).newInstance(str)

  }
}
fun main(args: Array<String>) {
  Factory(javaClass<Model1>()).gen()?.print()
  Factory(javaClass<Model2>()).gen()?.print()
}
```

I see, Is there a better way?
