Hi-
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()}”)
for (arg in method?.getParameterTypes()) {
println(" Param: ${arg?.getName()}“)
}
for (ann in anns) {
println(” Annotation: ${ann.toString()}")
val fooAnn = ann as? foo
if (fooAnn != null)
println(fooAnn.toString())
}
}
}
}
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?
Thanks!