I am using idea 15 eap with kotlin 0.13.1514 and I am trying to get the type of a KMutableProperty.returnType. The return type is an instance of KType and I can not get any type information out of it.
Is it possible to get this information or do I need to use java reflection? There have been some reflection changes recently, so maybe this is still beging worked on?
The API for accessing types is still work in progress. At the moment you can obtain the java.lang.reflect.Type object using the "javaType" extension property located in package "kotlin.reflect.jvm":
val prop = MyClass::foo
val javaType = prop.returnType.javaType
when (javaType) {
is Class<*> -> println("class: $javaType")
is ParameterizedType -> println("parameterized type: $javaType")
...
}
Thx, I have a follow up question. It looks like getting the annotations from class member (KProperty) doesn’t work yet, is that correct?
Take this example:
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FIELD)
annotation class Annot()
class Test(@Annot val test: String)
fun main(args: Array<String>) {
val cls = Test::class
for (member in cls.members) {
println(“Member: $member”)
for (ann in member.annotations) {
println(“Annotation: $ann”)
}
}
}
Outputs:
Member: val nl.astraeus.db.anntest.Test.test: kotlin.String
Member: fun kotlin.Any.equals(kotlin.Any?): kotlin.Boolean
Member: fun kotlin.Any.hashCode(): kotlin.Int
Member: fun kotlin.Any.toString(): kotlin.String