Reflecting on Method Annotations?



I’ve been playing around with Kotlin and tried writing some code that uses method annotations. I’m running into some difficulty when trying to reflect on the annotations to get usable runtime information about them. Consider this example code:

annotation class foo(name : String)

class Test() {
  foo(“Bob”) fun hello(input : String) {
  println(“hello: ${input}”)

fun main(args : Array<String>) {
  val test = Test()
  for (method in test.javaClass.getMethods()) {
  val anns = method?.getAnnotations() as Array<Annotation>
  if (!anns.isEmpty()) {
           println(“Method: {method?.getName()}") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;for (arg in method?.getParameterTypes()) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;println("&nbsp;&nbsp;Param: {arg?.getName()}”)
           for (ann in anns) {
           println("  Annotation: ${ann.toString()}")
           val fooAnn = ann as? foo
           if (fooAnn != null)

The output is:

Method: hello
  Param: java.lang.String
  Annotation: @jet.runtime.typeinfo.JetMethod(propertyType=, typeParameters=, nullableReturnType=false, kind=0, returnType=V, returnTypeProjections=[])

As you can see, the annotation is coming back as a jet.runtime.typeinfo.JetMethod object. Based on my experience with Java reflection, I’d expect it to be a foo object. I’m having trouble understanding how I can use reflection to get runtime information about annotations. Any suggestions?



I think the problem is that the retention policy for the annotation you defined is not set to RUNTIME.


Ah, okay. So the JetMethod annotation wasn't the one I was looking for. Makes sense. I'm actually more familiar with .NET attributes, so not retaining annotations at runtime seems like an odd default, but that's a JVM issue.

So I set the annotation to be retained at runtime, but I’m still not getting access to the name property. Is this because I haven’t explicitly declared it as public (due to


It seems related to this issue, but may be of different nature. Could you report your case to the tracker? Thanks